Allow for redundant accesses of same endpoint

This commit is contained in:
Kevin Bates 2020-10-14 14:57:57 -07:00
parent ac50d2e530
commit 7778dc3202
No known key found for this signature in database
GPG Key ID: ADCCD5840EE5145F
2 changed files with 19 additions and 4 deletions

View File

@ -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)

View File

@ -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)