Skip to content

Commit 2b42b90

Browse files
kevin-batesmbektas
andauthored
Ensure content-type properly reflects gateway kernelspec resources (#1219)
Co-authored-by: Mehmet Bektas <[email protected]>
1 parent ce8e7fc commit 2b42b90

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

jupyter_server/base/handlers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,9 @@ def update_api_activity(self):
773773
def finish(self, *args, **kwargs):
774774
"""Finish an API response."""
775775
self.update_api_activity()
776-
self.set_header("Content-Type", "application/json")
776+
# Allow caller to indicate content-type...
777+
set_content_type = kwargs.pop("set_content_type", "application/json")
778+
self.set_header("Content-Type", set_content_type)
777779
return super().finish(*args, **kwargs)
778780

779781
def options(self, *args, **kwargs):

jupyter_server/gateway/handlers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import mimetypes
77
import os
88
import random
9-
from typing import cast
9+
from typing import Optional, cast
1010

1111
from jupyter_client.session import Session
1212
from tornado import web
@@ -281,6 +281,7 @@ class GatewayResourceHandler(APIHandler):
281281
@web.authenticated
282282
async def get(self, kernel_name, path, include_body=True):
283283
"""Get a gateway resource by name and path."""
284+
mimetype: Optional[str] = None
284285
ksm = self.kernel_spec_manager
285286
kernel_spec_res = await ksm.get_kernel_spec_resource(kernel_name, path)
286287
if kernel_spec_res is None:
@@ -290,8 +291,7 @@ async def get(self, kernel_name, path, include_body=True):
290291
)
291292
else:
292293
mimetype = mimetypes.guess_type(path)[0] or "text/plain"
293-
self.set_header("Content-Type", mimetype)
294-
self.finish(kernel_spec_res)
294+
self.finish(kernel_spec_res, set_content_type=mimetype)
295295

296296

297297
from ..services.kernels.handlers import _kernel_id_regex

tests/test_gateway.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,10 @@ async def test_gateway_get_named_kernelspec(init_gateway, jp_fetch):
422422
kspec_foo = json.loads(r.body.decode("utf-8"))
423423
assert kspec_foo.get("name") == "kspec_foo"
424424

425-
r = await jp_fetch("kernelspecs", "kspec_foo", "hi", method="GET")
425+
r = await jp_fetch("kernelspecs", "kspec_foo", "logo-64x64.png", method="GET")
426426
assert r.code == 200
427427
assert r.body == b"foo"
428+
assert r.headers["content-type"] == "image/png"
428429

429430
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
430431
await jp_fetch("api", "kernelspecs", "no_such_spec", method="GET")

0 commit comments

Comments
 (0)