From a391ba1044dce71d761943ac69f7006b87b7251b 2013-10-17 21:09:12 From: Zachary Sailer Date: 2013-10-17 21:09:12 Subject: [PATCH] adding to test_kernels_api.py - Check all HTTP status code responses for requests - Test invalid kernel IDs - Check 'location' key in POST request to kernels --- diff --git a/IPython/html/services/kernels/handlers.py b/IPython/html/services/kernels/handlers.py index 147e2a7..c5bdec1 100644 --- a/IPython/html/services/kernels/handlers.py +++ b/IPython/html/services/kernels/handlers.py @@ -45,7 +45,8 @@ class MainKernelHandler(IPythonHandler): km = self.kernel_manager kernel_id = km.start_kernel() model = km.kernel_model(kernel_id, self.ws_url) - self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) + self.set_header('Location', '{0}api/kernels/{1}'.format(self.base_kernel_url, kernel_id)) + self.set_status(201) self.finish(jsonapi.dumps(model)) @@ -57,6 +58,7 @@ class KernelHandler(IPythonHandler): @json_errors def get(self, kernel_id): km = self.kernel_manager + km._check_kernel_id(kernel_id) model = km.kernel_model(kernel_id, self.ws_url) self.finish(jsonapi.dumps(model)) diff --git a/IPython/html/services/kernels/kernelmanager.py b/IPython/html/services/kernels/kernelmanager.py index 787abd1..3da9399 100644 --- a/IPython/html/services/kernels/kernelmanager.py +++ b/IPython/html/services/kernels/kernelmanager.py @@ -66,16 +66,19 @@ class MappingKernelManager(MultiKernelManager): 'dead', ) else: + self._check_kernel_id(kernel_id) self.log.info("Using existing kernel: %s" % kernel_id) return kernel_id def shutdown_kernel(self, kernel_id, now=False): """Shutdown a kernel by kernel_id""" + self._check_kernel_id(kernel_id) super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now) def kernel_model(self, kernel_id, ws_url): """Return a dictionary of kernel information described in the JSON standard model.""" + self._check_kernel_id(kernel_id) model = {"id":kernel_id, "ws_url": ws_url} return model diff --git a/IPython/html/services/kernels/tests/test_kernels_api.py b/IPython/html/services/kernels/tests/test_kernels_api.py index 894717f..0dc6ccb 100644 --- a/IPython/html/services/kernels/tests/test_kernels_api.py +++ b/IPython/html/services/kernels/tests/test_kernels_api.py @@ -31,24 +31,70 @@ class KernelAPITest(NotebookTestBase): # POST request r = requests.post(self.base_url()) data = r.json() + status = r.status_code + header = r.headers + self.assertIn('location', header) + self.assertEquals(header['location'], '/api/kernels/' + data['id']) + self.assertEquals(status, 201) assert isinstance(data, dict) # GET request r = requests.get(self.base_url()) + status = r.status_code + self.assertEquals(status, 200) assert isinstance(r.json(), list) self.assertEqual(r.json()[0]['id'], data['id']) + + # create another kernel and check that they both are added to the + # list of kernels from a GET request + data2 = self.mkkernel() + assert isinstance(data2, dict) + r = requests.get(self.base_url()) + kernels = r.json() + status = r.status_code + self.assertEquals(status, 200) + assert isinstance(kernels, list) + self.assertEquals(len(kernels), 2) def test_kernel_handler(self): - # GET kernel with id + # GET kernel with given id data = self.mkkernel() url = self.base_url() +'/' + data['id'] r = requests.get(url) - assert isinstance(r.json(), dict) - self.assertIn('id', r.json()) - self.assertEqual(r.json()['id'], data['id']) + data1 = r.json() + status = r.status_code + self.assertEquals(status, 200) + assert isinstance(data1, dict) + self.assertIn('id', data1) + self.assertIn('ws_url', data1) + self.assertEqual(data1['id'], data['id']) + + # Request a bad kernel id and check that a JSON + # message is returned! + bad_id = '111-111-111-111-111' + bad_url = self.base_url() + '/' + bad_id + r = requests.get(bad_url) + status = r.status_code + message = r.json() + self.assertEquals(status, 404) + assert isinstance(message, dict) + self.assertIn('message', message) + self.assertEquals(message['message'], 'Kernel does not exist: ' + bad_id) # DELETE kernel with id r = requests.delete(url) self.assertEqual(r.status_code, 204) r = requests.get(self.base_url()) self.assertEqual(r.json(), []) + + # Request to delete a non-existent kernel id + bad_id = '111-111-111-111-111' + bad_url = self.base_url() + '/' + bad_id + r = requests.delete(bad_url) + status = r.status_code + message = r.json() + self.assertEquals(status, 404) + assert isinstance(message, dict) + self.assertIn('message', message) + self.assertEquals(message['message'], 'Kernel does not exist: ' + bad_id) + \ No newline at end of file diff --git a/IPython/html/services/sessions/handlers.py b/IPython/html/services/sessions/handlers.py index 4db1004..a72ed5e 100644 --- a/IPython/html/services/sessions/handlers.py +++ b/IPython/html/services/sessions/handlers.py @@ -47,15 +47,15 @@ class SessionRootHandler(IPythonHandler): km = self.kernel_manager model = self.get_json_body() if model is None: - raise HTTPError(400, "No JSON data provided") + raise web.HTTPError(400, "No JSON data provided") try: name = model['notebook']['name'] except KeyError: - raise HTTPError(400, "Missing field in JSON data: name") + raise web.HTTPError(400, "Missing field in JSON data: name") try: path = model['notebook']['path'] except KeyError: - raise HTTPError(400, "Missing field in JSON data: path") + raise web.HTTPError(400, "Missing field in JSON data: path") # Check to see if session exists if sm.session_exists(name=name, path=path): model = sm.get_session(name=name, path=path)