diff --git a/notebook/terminal/handlers.py b/notebook/terminal/handlers.py index c949f2000..0bb36df98 100644 --- a/notebook/terminal/handlers.py +++ b/notebook/terminal/handlers.py @@ -3,6 +3,7 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +import json from tornado import web import terminado from notebook._tz import utcnow @@ -19,11 +20,17 @@ class TerminalHandler(IPythonHandler): class NewTerminalHandler(IPythonHandler): - """Render the terminal interface.""" + """Renders a new terminal interface using the named argument.""" @web.authenticated def get(self, term_name): - self.terminal_manager.create_with_name(term_name) new_path = self.request.path.replace("new/{}".format(term_name), term_name) + if term_name in self.terminal_manager.terminals: + self.set_header('Location', new_path) + self.set_status(302) + self.finish(json.dumps(self.terminal_manager.get_terminal_model(term_name))) + return + + self.terminal_manager.create_with_name(term_name) self.redirect(new_path) diff --git a/notebook/terminal/tests/test_terminals_api.py b/notebook/terminal/tests/test_terminals_api.py index fcbbf1bdd..6b808c5c8 100644 --- a/notebook/terminal/tests/test_terminals_api.py +++ b/notebook/terminal/tests/test_terminals_api.py @@ -76,8 +76,16 @@ class TerminalAPITest(NotebookTestBase): self.assertIsInstance(foo_term, dict) self.assertEqual(foo_term['name'], 'foo') - with assert_http_error(409): - self.term_api._req('GET', 'terminals/new/foo') + # hit the same endpoint a second time and ensure 302 with Location is returned + r = self.term_api._req('GET', 'terminals/new/foo') + # Access the "interesting" response from the history + self.assertEqual(len(r.history), 1) + r = r.history[0] + foo_term = r.json() + self.assertEqual(r.status_code, 302) + self.assertEqual(r.headers['Location'], self.url_prefix + "terminals/foo") + self.assertIsInstance(foo_term, dict) + self.assertEqual(foo_term['name'], 'foo') r = self.term_api.shutdown('foo') self.assertEqual(r.status_code, 204)