Skip to content

Commit 84ba421

Browse files
committed
Create auto-named terminals via /terminals/new
1 parent 7778dc3 commit 84ba421

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

notebook/terminal/handlers.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ class TerminalHandler(IPythonHandler):
1515
"""Render the terminal interface."""
1616
@web.authenticated
1717
def get(self, term_name):
18-
self.write(self.render_template('terminal.html',
19-
ws_path="terminals/websocket/%s" % term_name))
18+
if term_name == 'new':
19+
model = self.terminal_manager.create()
20+
term_name = model['name']
21+
new_path = self.request.path.replace("terminals/new", "terminals/" + term_name)
22+
self.redirect(new_path)
23+
else:
24+
self.write(self.render_template('terminal.html',
25+
ws_path="terminals/websocket/%s" % term_name))
2026

2127

2228
class NewTerminalHandler(IPythonHandler):
2329
"""Renders a new terminal interface using the named argument."""
2430
@web.authenticated
2531
def get(self, term_name):
32+
if term_name == 'new':
33+
raise web.HTTPError(400, "Terminal name 'new' is reserved.")
2634
new_path = self.request.path.replace("new/{}".format(term_name), term_name)
2735
if term_name in self.terminal_manager.terminals:
2836
self.set_header('Location', new_path)

notebook/terminal/tests/test_terminals_api.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,41 @@ def test_create_terminal(self):
6666
self.assertIsInstance(term1, dict)
6767

6868
def test_create_terminal_via_get(self):
69+
# Test creation of terminal via GET against terminals/new/<name>
70+
r = self.term_api._req('GET', 'terminals/new')
71+
self.assertEqual(r.status_code, 200)
72+
73+
r = self.term_api.get('1')
74+
term1 = r.json()
75+
self.assertEqual(r.status_code, 200)
76+
self.assertIsInstance(term1, dict)
77+
self.assertEqual(term1['name'], '1')
78+
79+
# hit the same endpoint a second time and ensure a second named terminal is created
80+
r = self.term_api._req('GET', 'terminals/new')
81+
self.assertEqual(r.status_code, 200)
82+
83+
r = self.term_api.get('2')
84+
term2 = r.json()
85+
self.assertEqual(r.status_code, 200)
86+
self.assertIsInstance(term2, dict)
87+
self.assertEqual(term2['name'], '2')
88+
89+
r = self.term_api.shutdown('2')
90+
self.assertEqual(r.status_code, 204)
91+
92+
# Make sure there is 1 terminal running
93+
terminals = self.term_api.list().json()
94+
self.assertEqual(len(terminals), 1)
95+
96+
r = self.term_api.shutdown('1')
97+
self.assertEqual(r.status_code, 204)
98+
99+
# Make sure there are no terminals are running
100+
terminals = self.term_api.list().json()
101+
self.assertEqual(len(terminals), 0)
102+
103+
def test_create_terminal_with_name(self):
69104
# Test creation of terminal via GET against terminals/new/<name>
70105
r = self.term_api._req('GET', 'terminals/new/foo')
71106
self.assertEqual(r.status_code, 200)
@@ -92,7 +127,11 @@ def test_create_terminal_via_get(self):
92127

93128
# Make sure there are no terminals are running
94129
terminals = self.term_api.list().json()
95-
self.assertEqual(terminals, [])
130+
self.assertEqual(len(terminals), 0)
131+
132+
# hit terminals/new/new and ensure that 400 is raised
133+
with assert_http_error(400):
134+
self.term_api._req('GET', 'terminals/new/new')
96135

97136
def test_terminal_root_handler(self):
98137
# POST request

0 commit comments

Comments
 (0)