From ef2eb3d267079030bca8399639690014edf22bb1 Mon Sep 17 00:00:00 2001 From: Andrey Velichkevich Date: Wed, 23 Mar 2022 13:33:40 +0000 Subject: [PATCH] Fix error handler in simple extension examples --- examples/simple/README.md | 4 ++-- examples/simple/simple_ext1/application.py | 2 +- examples/simple/simple_ext1/handlers.py | 5 +++-- examples/simple/simple_ext2/handlers.py | 2 +- examples/simple/tests/test_handlers.py | 15 +++++++++++++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/examples/simple/README.md b/examples/simple/README.md index f41eb5bb29..2652c2da4f 100644 --- a/examples/simple/README.md +++ b/examples/simple/README.md @@ -15,7 +15,7 @@ git clone https://github.com/jupyter/jupyter_server && \ pip install -e .[test] ``` -**OPTIONAL** If you want to build the Typescript code, you need [npm](https://www.npmjs.com) on your local environement. Compiled javascript is provided as artifact in this repository, so this Typescript build step is optional. The Typescript source and configuration have been taken from https://github.com/markellekelly/jupyter-server-example. +**OPTIONAL** If you want to build the Typescript code, you need [npm](https://www.npmjs.com) on your local environment. Compiled javascript is provided as artifact in this repository, so this Typescript build step is optional. The Typescript source and configuration have been taken from https://github.com/markellekelly/jupyter-server-example. ```bash npm install && \ @@ -88,7 +88,7 @@ The following command starts both the `simple_ext1` and `simple_ext2` extensions jupyter server --ServerApp.jpserver_extensions="{'simple_ext1': True, 'simple_ext2': True}" ``` -Check that the previous `Extension 1` content is still available ant that you can also render `Extension 2` Server content in your browser. +Check that the previous `Extension 1` content is still available and that you can also render `Extension 2` Server content in your browser. ```bash # HTML static page. diff --git a/examples/simple/simple_ext1/application.py b/examples/simple/simple_ext1/application.py index 91e734f871..6508fbe3f5 100644 --- a/examples/simple/simple_ext1/application.py +++ b/examples/simple/simple_ext1/application.py @@ -46,7 +46,7 @@ def initialize_handlers(self): (r"/{}/template1/(.*)$".format(self.name), TemplateHandler), (r"/{}/redirect".format(self.name), RedirectHandler), (r"/{}/typescript/?".format(self.name), TypescriptHandler), - (r"/{}/(.*)", ErrorHandler), + (r"/{}/(.*)".format(self.name), ErrorHandler), ] ) diff --git a/examples/simple/simple_ext1/handlers.py b/examples/simple/simple_ext1/handlers.py index 1f412e8500..f30f16654e 100644 --- a/examples/simple/simple_ext1/handlers.py +++ b/examples/simple/simple_ext1/handlers.py @@ -42,10 +42,11 @@ def get(self): class TemplateHandler(BaseTemplateHandler): def get(self, path): - """Optionaly, you can print(self.get_template('simple1.html'))""" + """Optionally, you can print(self.get_template('simple1.html'))""" self.write(self.render_template("simple1.html", path=path)) class ErrorHandler(BaseTemplateHandler): def get(self, path): - self.write(self.render_template("error.html", path=path)) + # write_error renders template from error.html file. + self.write_error(400) diff --git a/examples/simple/simple_ext2/handlers.py b/examples/simple/simple_ext2/handlers.py index 2e37fe87c6..355afbfd20 100644 --- a/examples/simple/simple_ext2/handlers.py +++ b/examples/simple/simple_ext2/handlers.py @@ -31,4 +31,4 @@ def get(self, path): class ErrorHandler(BaseTemplateHandler): def get(self, path): - self.write(self.render_template("error.html")) + self.write_error(400) diff --git a/examples/simple/tests/test_handlers.py b/examples/simple/tests/test_handlers.py index 7c4cb69b29..887a189f74 100644 --- a/examples/simple/tests/test_handlers.py +++ b/examples/simple/tests/test_handlers.py @@ -11,10 +11,21 @@ def jp_server_config(jp_template_dir): async def test_handler_default(jp_fetch): r = await jp_fetch("simple_ext1/default", method="GET") assert r.code == 200 - print(r.body.decode()) assert r.body.decode().index("Hello Simple 1 - I am the default...") > -1 async def test_handler_template(jp_fetch): - r = await jp_fetch("simple_ext1/template1/test", method="GET") + path = "/custom/path" + r = await jp_fetch("simple_ext1/template1/{}".format(path), method="GET") assert r.code == 200 + assert r.body.decode().index("Path: {}".format(path)) > -1 + + +async def test_handler_typescript(jp_fetch): + r = await jp_fetch("simple_ext1/typescript", method="GET") + assert r.code == 200 + + +async def test_handler_error(jp_fetch): + r = await jp_fetch("simple_ext1/nope", method="GET") + assert r.body.decode().index("400 : Bad Request") > -1