Skip to content

Commit 2a5f8d2

Browse files
committed
add token auth to kernel websocket test
1 parent 75a0d71 commit 2a5f8d2

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

notebook/services/kernels/tests/test_kernels_api.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import json
44
import time
55

6-
import requests
7-
from tornado.websocket import websocket_connect
6+
from tornado.httpclient import HTTPRequest
87
from tornado.ioloop import IOLoop
8+
from tornado.websocket import websocket_connect
99

1010
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
1111

@@ -14,8 +14,10 @@
1414

1515
class KernelAPI(object):
1616
"""Wrapper for kernel REST API requests"""
17-
def __init__(self, request):
17+
def __init__(self, request, base_url, headers):
1818
self.request = request
19+
self.base_url = base_url
20+
self.headers = headers
1921

2022
def _req(self, verb, path, body=None):
2123
response = self.request(verb,
@@ -51,16 +53,21 @@ def restart(self, id):
5153

5254
def websocket(self, id):
5355
loop = IOLoop()
54-
f = websocket_connect(url_path_join(
55-
self.base_url.replace('http', 'ws', 1), 'api/kernels', id, 'channels'),
56-
io_loop=loop)
56+
req = HTTPRequest(
57+
url_path_join(self.base_url.replace('http', 'ws', 1), 'api/kernels', id, 'channels'),
58+
headers=self.headers,
59+
)
60+
f = websocket_connect(req, io_loop=loop)
5761
return loop.run_sync(lambda : f)
5862

5963

6064
class KernelAPITest(NotebookTestBase):
6165
"""Test the kernels web service API"""
6266
def setUp(self):
63-
self.kern_api = KernelAPI(self.request)
67+
self.kern_api = KernelAPI(self.request,
68+
base_url=self.base_url(),
69+
headers=self.auth_headers(),
70+
)
6471

6572
def tearDown(self):
6673
for k in self.kern_api.list().json():

notebook/tests/launchnotebook.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,22 @@ def wait_until_dead(cls):
7272
raise TimeoutError("Undead notebook server")
7373

7474
@classmethod
75-
def request(self, verb, path, **kwargs):
75+
def auth_headers(cls):
76+
headers = {}
77+
if cls.token:
78+
headers['Authorization'] = 'token %s' % cls.token
79+
return headers
80+
81+
@classmethod
82+
def request(cls, verb, path, **kwargs):
7683
"""Send a request to my server
7784
7885
with authentication and everything.
7986
"""
8087
headers = kwargs.setdefault('headers', {})
81-
# kwargs.setdefault('allow_redirects', False)
82-
headers.setdefault('Authorization', 'token %s' % self.token)
88+
headers.update(cls.auth_headers())
8389
response = requests.request(verb,
84-
url_path_join(self.base_url(), path),
90+
url_path_join(cls.base_url(), path),
8591
**kwargs)
8692
return response
8793

0 commit comments

Comments
 (0)