sanic/tests/test_routes.py
Adam Hopkins bc08383acd
Merge in main to current-release (#2254)
* Remove unnecessary import in test_constants.py, which also fixes an error on win (#2180)

Co-authored-by: Adam Hopkins <admhpkns@gmail.com>

* Manually reset the buffer when streaming request body (#2183)

* Remove Duplicated Dependencies and PEP 517 Support (#2173)

* Remove duplicated dependencies

* Specify setuptools as the tool for generating distribution (PEP 517)

* Add `isort` to `dev_require`

* manage all dependencies in setup.py

* Execute `make pretty`

* Set usedevelop to true (revert previous change)

* Fix the handling of the end of a chunked request. (#2188)

* Fix the handling of the end of a chunked request.

* Avoid hardcoding final chunk header size.

* Add some unit tests for pipeline body reading

* Decode bytes for json serialization

Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Resolve regressions in exceptions (#2181)

* Update sanic-routing to fix path issues plus lookahead / lookbehind support (#2178)

* Update sanic-routing to fix path issues plus lookahead / lookbehind support

* Update setup.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>

* style(app,blueprints): add some type hints (#2196)

* style(app,blueprints): add some type hints

* style(app): option is Any

* style(blueprints): url prefix default value is ``""``

* style(app): backward compatible

* style(app): backward compatible

* style(blueprints): defult is None

* style(app): apply code style (black)

* Update some CC config (#2199)

* Update README.rst

* raise exception for `_static_request_handler` unknown exception; add test with custom error (#2195)

Co-authored-by: n.feofanov <n.feofanov@visionlabs.ru>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>

* Change dumps to AnyStr (#2193)

* HTTP tests (#2194)

* Fix issues with after request handling in HTTP pipelining (#2201)

* Clean up after a request is complete, before the next pipelined request.

* Limit the size of request body consumed after handler has finished.

* Linter error.

* Add unit test re: bad headers

Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Update CHANGELOG

* Log remote address if available (#2207)

* Log remote address if available

* Add tests

* Fix testing version

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Fixed for handling exceptions of asgi app call. (#2211)

@cansarigol3megawatt Thanks for looking into this and getting the quick turnaround on this. I will 🍒 pick this into the 21.6 branch and get it out a little later tonight.

* Signals Integration (#2160)

* Update some tests

* Resolve #2122 route decorator returning tuple

* Use rc sanic-routing version

* Update unit tests to <:str>

* Minimal working version with some signals implemented

* Add more http signals

* Update ASGI and change listeners to signals

* Allow for dynamic ODE signals

* Allow signals to be stacked

* Begin tests

* Prioritize match_info on keyword argument injection

* WIP on tests

* Compat with signals

* Work through some test coverage

* Passing tests

* Post linting

* Setup proper resets

* coverage reporting

* Fixes from vltr comments

* clear delayed tasks

* Fix bad test

* rm pycache

* uncomment windows tests (#2214)

* Add convenience methods to BP groups (#2209)

* Fix bug where ws exceptions not being logged (#2213)

* Fix bug where ws exceptions not being logged

* Fix t\est

* Style: add type hints (#2217)

* style(routes): add_route argument, return typing

* style(listeners): typing

* style(views): typing as_view

* style(routes): change type hint

* style(listeners): change type hint

* style(routes): change type hint

* add some more types

* Change as_view typing

* Add some cleaner type annotations

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Add default messages to SanicExceptions (#2216)

* Add default messages to SanicExceptions

* Cleaner exception message setting

* Copy Blueprints Implementation (#2184)

* Accept header parsing (#2200)

* Add some tests

* docstring

* Add accept matching

* Add some more tests on matching

* Add matching flags for wildcards

* Add mathing controls to accept

* Limit uvicorn 14 in testing

* Add convenience for annotated handlers (#2225)

* Split HttpProtocol parts into base SanicProtocol and HTTPProtocol subclass (#2229)

* Split HttpProtocol parts into base SanicProtocol and HTTPProtocol subclass.

* lint fixes

* re-black server.py

* Move server.py into its own module (#2230)

* Move server.py into its own module

* Change monkeypatch path on test_logging.py

* Blueprint specific exception handlers (#2208)

* Call abort() on sockets after close() to prevent dangling sockets (#2231)

* Add ability to return Falsey but not-None from handlers (#2236)

* Adds Blueprint Group exception decorator (#2238)

* Add exception decorator

* Added tests

* Fix line too long

* Static DIR and FILE resource types (#2244)

* Explicit static directive for serving file or dir


Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com>
Co-authored-by: anbuhckr <miki.suhendra@gmail.com>

* Close HTTP loop when connection task cancelled (#2245)

* Terminate loop when no transport exists

* Add log when closing HTTP loop because of shutdown

* Add unit test

* New websockets (#2158)

* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work.

* Update sanic/websocket.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Update sanic/websocket.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Update sanic/websocket.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* wip, update websockets code to new Sans/IO API

* Refactored new websockets impl into own modules
Incorporated other suggestions made by team

* Another round of work on the new websockets impl
* Added websocket_timeout support (matching previous/legacy support)
* Lots more comments
* Incorporated suggested changes from previous round of review
* Changed RuntimeError usage to ServerError
* Changed SanicException usage to ServerError
* Removed some redundant asserts
* Change remaining asserts to ServerErrors
* Fixed some timeout handling issues
* Fixed websocket.close() handling, and made it more robust
* Made auto_close task smarter and more error-resilient
* Made fail_connection routine smarter and more error-resilient

* Further new websockets impl fixes
* Update compatibility with Websockets v10
* Track server connection state in a more precise way
* Try to handle the shutdown process more gracefully
* Add a new end_connection() helper, to use as an alterative to close() or fail_connection()
* Kill the auto-close task and keepalive-timeout task when sanic is shutdown
* Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation.

* Change a warning message to debug level
Remove default values for deprecated websocket parameters

* Fix flake8 errors

* Fix a couple of missed failing tests

* remove websocket bench from examples

* Integrate suggestions from code reviews
Use Optional[T] instead of union[T,None]
Fix mypy type logic errors
change "is not None" to truthy checks where appropriate
change "is None" to falsy checks were appropriate
Add more debug logging when debug mode is on
Change to using sanic.logger for debug logging rather than error_logger.

* Fix long line lengths of debug messages
Add some new debug messages when websocket IO is paused and unpaused for flow control
Fix websocket example to use app.static()

* remove unused import in websocket example app

* re-run isort after Flake8 fixes

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>

* Account for BP with exception handler but no routes (#2246)

* Don't log "enabled" if auto-reload disabled (#2247)

Fixes #2240

Co-authored-by: Adam Hopkins <admhpkns@gmail.com>

* Smarter auto fallback (#2162)

* Smarter auto fallback

* remove config from blueprints

* Add tests for error formatting

* Add check for proper format

* Fix some tests

* Add some tests

* docstring

* Add accept matching

* Add some more tests on matching

* Fix contains bug, earlier return on MediaType eq

* Add matching flags for wildcards

* Add mathing controls to accept

* Cleanup dev cruft

* Add cleanup and resolve OSError relating to test implementation

* Fix test

* Fix some typos

* Some fixes to the new Websockets impl (#2248)

* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work.

* Update sanic/websocket.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Update sanic/websocket.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* Update sanic/websocket.py

Co-authored-by: Adam Hopkins <adam@amhopkins.com>

* wip, update websockets code to new Sans/IO API

* Refactored new websockets impl into own modules
Incorporated other suggestions made by team

* Another round of work on the new websockets impl
* Added websocket_timeout support (matching previous/legacy support)
* Lots more comments
* Incorporated suggested changes from previous round of review
* Changed RuntimeError usage to ServerError
* Changed SanicException usage to ServerError
* Removed some redundant asserts
* Change remaining asserts to ServerErrors
* Fixed some timeout handling issues
* Fixed websocket.close() handling, and made it more robust
* Made auto_close task smarter and more error-resilient
* Made fail_connection routine smarter and more error-resilient

* Further new websockets impl fixes
* Update compatibility with Websockets v10
* Track server connection state in a more precise way
* Try to handle the shutdown process more gracefully
* Add a new end_connection() helper, to use as an alterative to close() or fail_connection()
* Kill the auto-close task and keepalive-timeout task when sanic is shutdown
* Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation.

* Change a warning message to debug level
Remove default values for deprecated websocket parameters

* Fix flake8 errors

* Fix a couple of missed failing tests

* remove websocket bench from examples

* Integrate suggestions from code reviews
Use Optional[T] instead of union[T,None]
Fix mypy type logic errors
change "is not None" to truthy checks where appropriate
change "is None" to falsy checks were appropriate
Add more debug logging when debug mode is on
Change to using sanic.logger for debug logging rather than error_logger.

* Fix long line lengths of debug messages
Add some new debug messages when websocket IO is paused and unpaused for flow control
Fix websocket example to use app.static()

* remove unused import in websocket example app

* re-run isort after Flake8 fixes

* Some fixes to the new Websockets impl
Will throw WebsocketClosed exception instead of ServerException now when attempting to read or write to closed websocket, this makes it easier to catch
The various ws.recv() methods now have the ability to raise CancelledError into your websocket handler
Fix a niche close-socket negotiation bug
Fix bug where http protocol thought the websocket never sent any response.
Allow data to still send in some cases after websocket enters CLOSING state.
Fix some badly formatted and badly placed comments

* allow eof_received to send back data too, if the connection is in CLOSING state

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
Co-authored-by: Adam Hopkins <admhpkns@gmail.com>

* 21.9 release docs (#2218)

* Beging 21.9 release docs

* Add PRs to changelog

* Change deprecation version

* Update logging tests

* Bump version

* Update changelog

* Change dev install command (#2251)

Co-authored-by: Zhiwei <zhi.wei.liang@outlook.com>
Co-authored-by: L. Kärkkäinen <98187+Tronic@users.noreply.github.com>
Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
Co-authored-by: Robert Palmer <robd003@users.noreply.github.com>
Co-authored-by: Ryu JuHeon <saidbysolo@gmail.com>
Co-authored-by: gluhar2006 <49654448+gluhar2006@users.noreply.github.com>
Co-authored-by: n.feofanov <n.feofanov@visionlabs.ru>
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com>
Co-authored-by: Can Sarigol <56863826+cansarigol3megawatt@users.noreply.github.com>
Co-authored-by: Zhiwei <chihwei.public@outlook.com>
Co-authored-by: YongChan Cho <h3236516@gmail.com>
Co-authored-by: Zhiwei <zhiwei@sinatra.ai>
Co-authored-by: Ashley Sommer <ashleysommer@gmail.com>
Co-authored-by: anbuhckr <36891836+anbuhckr@users.noreply.github.com>
Co-authored-by: anbuhckr <miki.suhendra@gmail.com>
2021-10-02 21:55:23 +03:00

1233 lines
33 KiB
Python

import asyncio
import re
from unittest.mock import Mock
import pytest
from sanic_routing.exceptions import (
InvalidUsage,
ParameterNameConflicts,
RouteExists,
)
from sanic_testing.testing import SanicTestClient
from sanic import Blueprint, Sanic
from sanic.constants import HTTP_METHODS
from sanic.exceptions import NotFound, SanicException
from sanic.request import Request
from sanic.response import json, text
@pytest.mark.parametrize(
"path,headers,expected",
(
# app base
(b"/", {}, 200),
(b"/", {"host": "maybe.com"}, 200),
(b"/host", {"host": "matching.com"}, 200),
(b"/host", {"host": "wrong.com"}, 404),
# app strict_slashes default
(b"/without", {}, 200),
(b"/without/", {}, 200),
(b"/with", {}, 200),
(b"/with/", {}, 200),
# app strict_slashes off - expressly
(b"/expwithout", {}, 200),
(b"/expwithout/", {}, 200),
(b"/expwith", {}, 200),
(b"/expwith/", {}, 200),
# app strict_slashes on
(b"/without/strict", {}, 200),
(b"/without/strict/", {}, 404),
(b"/with/strict", {}, 404),
(b"/with/strict/", {}, 200),
# bp1 base
(b"/bp1", {}, 200),
(b"/bp1", {"host": "maybe.com"}, 200),
(b"/bp1/host", {"host": "matching.com"}, 200), # BROKEN ON MASTER
(b"/bp1/host", {"host": "wrong.com"}, 404),
# bp1 strict_slashes default
(b"/bp1/without", {}, 200),
(b"/bp1/without/", {}, 200),
(b"/bp1/with", {}, 200),
(b"/bp1/with/", {}, 200),
# bp1 strict_slashes off - expressly
(b"/bp1/expwithout", {}, 200),
(b"/bp1/expwithout/", {}, 200),
(b"/bp1/expwith", {}, 200),
(b"/bp1/expwith/", {}, 200),
# bp1 strict_slashes on
(b"/bp1/without/strict", {}, 200),
(b"/bp1/without/strict/", {}, 404),
(b"/bp1/with/strict", {}, 404),
(b"/bp1/with/strict/", {}, 200),
# bp2 base
(b"/bp2/", {}, 200),
(b"/bp2/", {"host": "maybe.com"}, 200),
(b"/bp2/host", {"host": "matching.com"}, 200), # BROKEN ON MASTER
(b"/bp2/host", {"host": "wrong.com"}, 404),
# bp2 strict_slashes default
(b"/bp2/without", {}, 200),
(b"/bp2/without/", {}, 404),
(b"/bp2/with", {}, 404),
(b"/bp2/with/", {}, 200),
# # bp2 strict_slashes off - expressly
(b"/bp2/expwithout", {}, 200),
(b"/bp2/expwithout/", {}, 200),
(b"/bp2/expwith", {}, 200),
(b"/bp2/expwith/", {}, 200),
# # bp2 strict_slashes on
(b"/bp2/without/strict", {}, 200),
(b"/bp2/without/strict/", {}, 404),
(b"/bp2/with/strict", {}, 404),
(b"/bp2/with/strict/", {}, 200),
# bp3 base
(b"/bp3", {}, 200),
(b"/bp3", {"host": "maybe.com"}, 200),
(b"/bp3/host", {"host": "matching.com"}, 200), # BROKEN ON MASTER
(b"/bp3/host", {"host": "wrong.com"}, 404),
# bp3 strict_slashes default
(b"/bp3/without", {}, 200),
(b"/bp3/without/", {}, 200),
(b"/bp3/with", {}, 200),
(b"/bp3/with/", {}, 200),
# bp3 strict_slashes off - expressly
(b"/bp3/expwithout", {}, 200),
(b"/bp3/expwithout/", {}, 200),
(b"/bp3/expwith", {}, 200),
(b"/bp3/expwith/", {}, 200),
# bp3 strict_slashes on
(b"/bp3/without/strict", {}, 200),
(b"/bp3/without/strict/", {}, 404),
(b"/bp3/with/strict", {}, 404),
(b"/bp3/with/strict/", {}, 200),
# bp4 base
(b"/bp4", {}, 404),
(b"/bp4", {"host": "maybe.com"}, 200),
(b"/bp4/host", {"host": "matching.com"}, 200), # BROKEN ON MASTER
(b"/bp4/host", {"host": "wrong.com"}, 404),
# bp4 strict_slashes default
(b"/bp4/without", {}, 404),
(b"/bp4/without/", {}, 404),
(b"/bp4/with", {}, 404),
(b"/bp4/with/", {}, 404),
# bp4 strict_slashes off - expressly
(b"/bp4/expwithout", {}, 404),
(b"/bp4/expwithout/", {}, 404),
(b"/bp4/expwith", {}, 404),
(b"/bp4/expwith/", {}, 404),
# bp4 strict_slashes on
(b"/bp4/without/strict", {}, 404),
(b"/bp4/without/strict/", {}, 404),
(b"/bp4/with/strict", {}, 404),
(b"/bp4/with/strict/", {}, 404),
),
)
def test_matching(path, headers, expected):
app = Sanic("dev")
bp1 = Blueprint("bp1", url_prefix="/bp1")
bp2 = Blueprint("bp2", url_prefix="/bp2", strict_slashes=True)
bp3 = Blueprint("bp3", url_prefix="/bp3", strict_slashes=False)
bp4 = Blueprint("bp4", url_prefix="/bp4", host="maybe.com")
def handler(request):
return text("Hello!")
defs = (
("/", None, None),
("/host", None, "matching.com"),
("/without", None, None),
("/with/", None, None),
("/expwithout", False, None),
("/expwith/", False, None),
("/without/strict", True, None),
("/with/strict/", True, None),
)
for uri, strict_slashes, host in defs:
params = {"uri": uri}
if strict_slashes is not None:
params["strict_slashes"] = strict_slashes
if host is not None:
params["host"] = host
app.route(**params)(handler)
bp1.route(**params)(handler)
bp2.route(**params)(handler)
bp3.route(**params)(handler)
bp4.route(**params)(handler)
app.blueprint(bp1)
app.blueprint(bp2)
app.blueprint(bp3)
app.blueprint(bp4)
app.router.finalize()
request = Request(path, headers, None, "GET", None, app)
try:
app.router.get(
request.path, request.method, request.headers.get("host")
)
except NotFound:
response = 404
except Exception:
response = 500
else:
response = 200
assert response == expected
# # ------------------------------------------------------------ #
# # UTF-8
# # ------------------------------------------------------------ #
@pytest.mark.parametrize("method", HTTP_METHODS)
def test_versioned_routes_get(app, method):
method = method.lower()
func = getattr(app, method)
if callable(func):
@func(f"/{method}", version=1)
def handler(request):
return text("OK")
else:
raise Exception(f"Method: {method} is not callable")
client_method = getattr(app.test_client, method)
request, response = client_method(f"/v1/{method}")
assert response.status == 200
def test_shorthand_routes_get(app):
@app.get("/get")
def handler(request):
return text("OK")
request, response = app.test_client.get("/get")
assert response.text == "OK"
request, response = app.test_client.post("/get")
assert response.status == 405
def test_shorthand_routes_multiple(app):
@app.get("/get")
def get_handler(request):
return text("OK")
@app.options("/get")
def options_handler(request):
return text("")
request, response = app.test_client.get("/get/")
assert response.status == 200
assert response.text == "OK"
request, response = app.test_client.options("/get/")
assert response.status == 200
def test_route_strict_slash(app):
@app.get("/get", strict_slashes=True)
def handler1(request):
return text("OK")
@app.post("/post/", strict_slashes=True)
def handler2(request):
return text("OK")
request, response = app.test_client.get("/get")
assert response.text == "OK"
request, response = app.test_client.get("/get/")
assert response.status == 404
request, response = app.test_client.post("/post/")
assert response.text == "OK"
request, response = app.test_client.post("/post")
assert response.status == 404
def test_route_invalid_parameter_syntax(app):
with pytest.raises(ValueError):
@app.get("/get/<:str>", strict_slashes=True)
def handler(request):
return text("OK")
request, response = app.test_client.get("/get")
def test_route_strict_slash_default_value():
app = Sanic("test_route_strict_slash", strict_slashes=True)
@app.get("/get")
def handler(request):
return text("OK")
request, response = app.test_client.get("/get/")
assert response.status == 404
def test_route_strict_slash_without_passing_default_value(app):
@app.get("/get")
def handler(request):
return text("OK")
request, response = app.test_client.get("/get/")
assert response.text == "OK"
def test_route_strict_slash_default_value_can_be_overwritten():
app = Sanic("test_route_strict_slash", strict_slashes=True)
@app.get("/get", strict_slashes=False)
def handler(request):
return text("OK")
request, response = app.test_client.get("/get/")
assert response.text == "OK"
def test_route_slashes_overload(app):
@app.get("/hello/")
def handler_get(request):
return text("OK")
@app.post("/hello/")
def handler_post(request):
return text("OK")
request, response = app.test_client.get("/hello")
assert response.text == "OK"
request, response = app.test_client.get("/hello/")
assert response.text == "OK"
request, response = app.test_client.post("/hello")
assert response.text == "OK"
request, response = app.test_client.post("/hello/")
assert response.text == "OK"
def test_route_optional_slash(app):
@app.get("/get")
def handler(request):
return text("OK")
request, response = app.test_client.get("/get")
assert response.text == "OK"
request, response = app.test_client.get("/get/")
assert response.text == "OK"
def test_route_strict_slashes_set_to_false_and_host_is_a_list(app):
# Part of regression test for issue #1120
test_client = SanicTestClient(app, port=42101)
site1 = f"127.0.0.1:{test_client.port}"
# before fix, this raises a RouteExists error
@app.get("/get", host=[site1, "site2.com"], strict_slashes=False)
def get_handler(request):
return text("OK")
request, response = test_client.get("http://" + site1 + "/get")
assert response.text == "OK"
app.router.finalized = False
@app.post("/post", host=[site1, "site2.com"], strict_slashes=False)
def post_handler(request):
return text("OK")
request, response = test_client.post("http://" + site1 + "/post")
assert response.text == "OK"
app.router.finalized = False
@app.put("/put", host=[site1, "site2.com"], strict_slashes=False)
def put_handler(request):
return text("OK")
request, response = test_client.put("http://" + site1 + "/put")
assert response.text == "OK"
app.router.finalized = False
@app.delete("/delete", host=[site1, "site2.com"], strict_slashes=False)
def delete_handler(request):
return text("OK")
request, response = test_client.delete("http://" + site1 + "/delete")
assert response.text == "OK"
def test_shorthand_routes_post(app):
@app.post("/post")
def handler(request):
return text("OK")
request, response = app.test_client.post("/post")
assert response.text == "OK"
request, response = app.test_client.get("/post")
assert response.status == 405
def test_shorthand_routes_put(app):
@app.put("/put")
def handler(request):
return text("OK")
request, response = app.test_client.put("/put")
assert response.text == "OK"
request, response = app.test_client.get("/put")
assert response.status == 405
def test_shorthand_routes_delete(app):
@app.delete("/delete")
def handler(request):
return text("OK")
request, response = app.test_client.delete("/delete")
assert response.text == "OK"
request, response = app.test_client.get("/delete")
assert response.status == 405
def test_shorthand_routes_patch(app):
@app.patch("/patch")
def handler(request):
return text("OK")
request, response = app.test_client.patch("/patch")
assert response.text == "OK"
request, response = app.test_client.get("/patch")
assert response.status == 405
def test_shorthand_routes_head(app):
@app.head("/head")
def handler(request):
return text("OK")
request, response = app.test_client.head("/head")
assert response.status == 200
request, response = app.test_client.get("/head")
assert response.status == 405
def test_shorthand_routes_options(app):
@app.options("/options")
def handler(request):
return text("OK")
request, response = app.test_client.options("/options")
assert response.status == 200
request, response = app.test_client.get("/options")
assert response.status == 405
def test_static_routes(app):
@app.route("/test")
async def handler1(request):
return text("OK1")
@app.route("/pizazz")
async def handler2(request):
return text("OK2")
request, response = app.test_client.get("/test")
assert response.text == "OK1"
request, response = app.test_client.get("/pizazz")
assert response.text == "OK2"
def test_dynamic_route(app):
results = []
@app.route("/folder/<name>")
async def handler(request, name):
results.append(name)
return text("OK")
app.router.finalize(False)
request, response = app.test_client.get("/folder/test123")
assert response.text == "OK"
assert results[0] == "test123"
def test_dynamic_route_string(app):
results = []
@app.route("/folder/<name:str>")
async def handler(request, name):
results.append(name)
return text("OK")
request, response = app.test_client.get("/folder/test123")
assert response.text == "OK"
assert results[0] == "test123"
request, response = app.test_client.get("/folder/favicon.ico")
assert response.text == "OK"
assert results[1] == "favicon.ico"
def test_dynamic_route_int(app):
results = []
@app.route("/folder/<folder_id:int>")
async def handler(request, folder_id):
results.append(folder_id)
return text("OK")
request, response = app.test_client.get("/folder/12345")
assert response.text == "OK"
assert type(results[0]) is int
request, response = app.test_client.get("/folder/asdf")
assert response.status == 404
def test_dynamic_route_number(app):
results = []
@app.route("/weight/<weight:float>")
async def handler(request, weight):
results.append(weight)
return text("OK")
request, response = app.test_client.get("/weight/12345")
assert response.text == "OK"
assert type(results[0]) is float
request, response = app.test_client.get("/weight/1234.56")
assert response.status == 200
request, response = app.test_client.get("/weight/.12")
assert response.status == 200
request, response = app.test_client.get("/weight/12.")
assert response.status == 200
request, response = app.test_client.get("/weight/1234-56")
assert response.status == 404
request, response = app.test_client.get("/weight/12.34.56")
assert response.status == 404
def test_dynamic_route_regex(app):
@app.route("/folder/<folder_id:[A-Za-z0-9]{0,4}>")
async def handler(request, folder_id):
return text("OK")
request, response = app.test_client.get("/folder/test")
assert response.status == 200
request, response = app.test_client.get("/folder/test1")
assert response.status == 404
request, response = app.test_client.get("/folder/test-123")
assert response.status == 404
request, response = app.test_client.get("/folder/")
assert response.status == 200
def test_dynamic_route_uuid(app):
import uuid
results = []
@app.route("/quirky/<unique_id:uuid>")
async def handler(request, unique_id):
results.append(unique_id)
return text("OK")
url = "/quirky/123e4567-e89b-12d3-a456-426655440000"
request, response = app.test_client.get(url)
assert response.text == "OK"
assert type(results[0]) is uuid.UUID
generated_uuid = uuid.uuid4()
request, response = app.test_client.get(f"/quirky/{generated_uuid}")
assert response.status == 200
request, response = app.test_client.get("/quirky/non-existing")
assert response.status == 404
def test_dynamic_route_path(app):
@app.route("/<path:path>/info")
async def handler(request, path):
return text("OK")
app.router.finalize()
request, response = app.test_client.get("/path/1/info")
assert response.status == 200
request, response = app.test_client.get("/info")
assert response.status == 404
app.router.reset()
@app.route("/<path:path>")
async def handler1(request, path):
return text("OK")
request, response = app.test_client.get("/info")
assert response.status == 200
request, response = app.test_client.get("/whatever/you/set")
assert response.status == 200
def test_dynamic_route_unhashable(app):
@app.route("/folder/<unhashable:[A-Za-z0-9/]+>/end/")
async def handler(request, unhashable):
return text("OK")
request, response = app.test_client.get("/folder/test/asdf/end/")
assert response.status == 200
request, response = app.test_client.get("/folder/test///////end/")
assert response.status == 200
request, response = app.test_client.get("/folder/test/end/")
assert response.status == 200
request, response = app.test_client.get("/folder/test/nope/")
assert response.status == 404
@pytest.mark.parametrize("url", ["/ws", "ws"])
def test_websocket_route(app, url):
ev = asyncio.Event()
@app.websocket(url)
async def handler(request, ws):
assert request.scheme == "ws"
assert ws.subprotocol is None
ev.set()
request, response = app.test_client.websocket(url)
assert response.opened is True
assert ev.is_set()
def test_websocket_route_invalid_handler(app):
with pytest.raises(ValueError) as e:
@app.websocket("/")
async def handler():
...
assert e.match(
r"Required parameter `request` and/or `ws` missing in the "
r"handler\(\) route\?"
)
@pytest.mark.asyncio
@pytest.mark.parametrize("url", ["/ws", "ws"])
async def test_websocket_route_asgi(app, url):
@app.after_server_start
async def setup_ev(app, _):
app.ctx.ev = asyncio.Event()
@app.websocket(url)
async def handler(request, ws):
request.app.ctx.ev.set()
@app.get("/ev")
async def check(request):
return json({"set": request.app.ctx.ev.is_set()})
_, response = await app.asgi_client.websocket(url)
_, response = await app.asgi_client.get("/")
assert response.json["set"]
@pytest.mark.parametrize(
"subprotocols,expected",
(
(["one"], "one"),
(["three", "one"], "one"),
(["tree"], None),
(None, None),
),
)
def test_websocket_route_with_subprotocols(app, subprotocols, expected):
results = []
@app.websocket("/ws", subprotocols=["zero", "one", "two", "three"])
async def handler(request, ws):
nonlocal results
results = ws.subprotocol
assert ws.subprotocol is not None
_, response = SanicTestClient(app).websocket(
"/ws", subprotocols=subprotocols
)
assert response.opened is True
assert results == expected
@pytest.mark.parametrize("strict_slashes", [True, False, None])
def test_add_webscoket_route(app, strict_slashes):
ev = asyncio.Event()
async def handler(request, ws):
assert ws.subprotocol is None
ev.set()
app.add_websocket_route(handler, "/ws", strict_slashes=strict_slashes)
request, response = app.test_client.websocket("/ws")
assert response.opened is True
assert ev.is_set()
def test_add_webscoket_route_with_version(app):
ev = asyncio.Event()
async def handler(request, ws):
assert ws.subprotocol is None
ev.set()
app.add_websocket_route(handler, "/ws", version=1)
request, response = app.test_client.websocket("/v1/ws")
assert response.opened is True
assert ev.is_set()
def test_route_duplicate(app):
with pytest.raises(RouteExists):
@app.route("/test")
async def handler1(request):
pass
@app.route("/test")
async def handler2(request):
pass
with pytest.raises(RouteExists):
@app.route("/test/<dynamic>/")
async def handler3(request, dynamic):
pass
@app.route("/test/<dynamic>/")
async def handler4(request, dynamic):
pass
def test_double_stack_route(app):
@app.route("/test/1")
@app.route("/test/2")
async def handler1(request):
return text("OK")
request, response = app.test_client.get("/test/1")
assert response.status == 200
request, response = app.test_client.get("/test/2")
assert response.status == 200
@pytest.mark.asyncio
async def test_websocket_route_asgi(app):
ev = asyncio.Event()
@app.websocket("/test/1")
@app.websocket("/test/2")
async def handler(request, ws):
ev.set()
request, response = await app.asgi_client.websocket("/test/1")
first_set = ev.is_set()
ev.clear()
request, response = await app.asgi_client.websocket("/test/1")
second_set = ev.is_set()
assert first_set and second_set
def test_method_not_allowed(app):
@app.route("/test", methods=["GET"])
async def handler(request):
return text("OK")
request, response = app.test_client.get("/test")
assert response.status == 200
request, response = app.test_client.post("/test")
assert response.status == 405
@pytest.mark.parametrize("strict_slashes", [True, False, None])
def test_static_add_route(app, strict_slashes):
async def handler1(request):
return text("OK1")
async def handler2(request):
return text("OK2")
app.add_route(handler1, "/test", strict_slashes=strict_slashes)
app.add_route(handler2, "/test2", strict_slashes=strict_slashes)
request, response = app.test_client.get("/test")
assert response.text == "OK1"
request, response = app.test_client.get("/test2")
assert response.text == "OK2"
def test_dynamic_add_route(app):
results = []
async def handler(request, name):
results.append(name)
return text("OK")
app.add_route(handler, "/folder/<name>")
request, response = app.test_client.get("/folder/test123")
assert response.text == "OK"
assert results[0] == "test123"
def test_dynamic_add_route_string(app):
results = []
async def handler(request, name):
results.append(name)
return text("OK")
app.add_route(handler, "/folder/<name:str>")
request, response = app.test_client.get("/folder/test123")
assert response.text == "OK"
assert results[0] == "test123"
request, response = app.test_client.get("/folder/favicon.ico")
assert response.text == "OK"
assert results[1] == "favicon.ico"
def test_dynamic_add_route_int(app):
results = []
async def handler(request, folder_id):
results.append(folder_id)
return text("OK")
app.add_route(handler, "/folder/<folder_id:int>")
request, response = app.test_client.get("/folder/12345")
assert response.text == "OK"
assert type(results[0]) is int
request, response = app.test_client.get("/folder/asdf")
assert response.status == 404
def test_dynamic_add_route_number(app):
results = []
async def handler(request, weight):
results.append(weight)
return text("OK")
app.add_route(handler, "/weight/<weight:float>")
request, response = app.test_client.get("/weight/12345")
assert response.text == "OK"
assert type(results[0]) is float
request, response = app.test_client.get("/weight/1234.56")
assert response.status == 200
request, response = app.test_client.get("/weight/.12")
assert response.status == 200
request, response = app.test_client.get("/weight/12.")
assert response.status == 200
request, response = app.test_client.get("/weight/1234-56")
assert response.status == 404
request, response = app.test_client.get("/weight/12.34.56")
assert response.status == 404
def test_dynamic_add_route_regex(app):
async def handler(request, folder_id):
return text("OK")
app.add_route(handler, "/folder/<folder_id:[A-Za-z0-9]{0,4}>")
request, response = app.test_client.get("/folder/test")
assert response.status == 200
request, response = app.test_client.get("/folder/test1")
assert response.status == 404
request, response = app.test_client.get("/folder/test-123")
assert response.status == 404
request, response = app.test_client.get("/folder/")
assert response.status == 200
def test_dynamic_add_route_unhashable(app):
async def handler(request, unhashable):
return text("OK")
app.add_route(handler, "/folder/<unhashable:[A-Za-z0-9/]+>/end/")
request, response = app.test_client.get("/folder/test/asdf/end/")
assert response.status == 200
request, response = app.test_client.get("/folder/test///////end/")
assert response.status == 200
request, response = app.test_client.get("/folder/test/end/")
assert response.status == 200
request, response = app.test_client.get("/folder/test/nope/")
assert response.status == 404
def test_add_route_duplicate(app):
with pytest.raises(RouteExists):
async def handler1(request):
pass
async def handler2(request):
pass
app.add_route(handler1, "/test")
app.add_route(handler2, "/test")
with pytest.raises(RouteExists):
async def handler1(request, dynamic):
pass
async def handler2(request, dynamic):
pass
app.add_route(handler1, "/test/<dynamic>/")
app.add_route(handler2, "/test/<dynamic>/")
def test_add_route_method_not_allowed(app):
async def handler(request):
return text("OK")
app.add_route(handler, "/test", methods=["GET"])
request, response = app.test_client.get("/test")
assert response.status == 200
request, response = app.test_client.post("/test")
assert response.status == 405
def test_removing_slash(app):
@app.get("/rest/<resource>")
def get(_):
pass
@app.post("/rest/<resource>")
def post(_):
pass
assert len(app.router.routes_all.keys()) == 1
def test_overload_routes(app):
@app.route("/overload", methods=["GET"])
async def handler1(request):
return text("OK1")
@app.route("/overload", methods=["POST", "PUT"])
async def handler2(request):
return text("OK2")
request, response = app.test_client.get("/overload")
assert response.text == "OK1"
request, response = app.test_client.post("/overload")
assert response.text == "OK2"
request, response = app.test_client.put("/overload")
assert response.text == "OK2"
request, response = app.test_client.delete("/overload")
assert response.status == 405
app.router.reset()
with pytest.raises(RouteExists):
@app.route("/overload", methods=["PUT", "DELETE"])
async def handler3(request):
return text("Duplicated")
def test_unmergeable_overload_routes(app):
@app.route("/overload_whole", methods=None)
async def handler1(request):
return text("OK1")
@app.route("/overload_whole", methods=["POST", "PUT"])
async def handler2(request):
return text("OK1")
assert len(app.router.static_routes) == 1
assert len(app.router.static_routes[("overload_whole",)].methods) == 3
request, response = app.test_client.get("/overload_whole")
assert response.text == "OK1"
request, response = app.test_client.post("/overload_whole")
assert response.text == "OK1"
request, response = app.test_client.put("/overload_whole")
assert response.text == "OK1"
app.router.reset()
@app.route("/overload_part", methods=["GET"])
async def handler3(request):
return text("OK1")
with pytest.raises(RouteExists):
@app.route("/overload_part")
async def handler4(request):
return text("Duplicated")
request, response = app.test_client.get("/overload_part")
assert response.text == "OK1"
request, response = app.test_client.post("/overload_part")
assert response.status == 405
def test_unicode_routes(app):
@app.get("/你好")
def handler1(request):
return text("OK1")
request, response = app.test_client.get("/你好")
assert response.text == "OK1"
app.router.reset()
@app.route("/overload/<param>", methods=["GET"], unquote=True)
async def handler2(request, param):
return text("OK2 " + param)
request, response = app.test_client.get("/overload/你好")
assert response.text == "OK2 你好"
def test_uri_with_different_method_and_different_params(app):
@app.route("/ads/<ad_id>", methods=["GET"])
async def ad_get(request, ad_id):
return json({"ad_id": ad_id})
@app.route("/ads/<action>", methods=["POST"])
async def ad_post(request, action):
return json({"action": action})
request, response = app.test_client.get("/ads/1234")
assert response.status == 200
assert response.json == {"ad_id": "1234"}
request, response = app.test_client.post("/ads/post")
assert response.status == 200
assert response.json == {"action": "post"}
def test_uri_with_different_method_and_same_params(app):
@app.route("/ads/<ad_id>", methods=["GET"])
async def ad_get(request, ad_id):
return json({"ad_id": ad_id})
@app.route("/ads/<ad_id>", methods=["POST"])
async def ad_post(request, ad_id):
return json({"ad_id": ad_id})
request, response = app.test_client.get("/ads/1234")
assert response.status == 200
assert response.json == {"ad_id": "1234"}
request, response = app.test_client.post("/ads/post")
assert response.status == 200
assert response.json == {"ad_id": "post"}
def test_route_raise_ParameterNameConflicts(app):
@app.get("/api/v1/<user>/<user>/")
def handler(request, user):
return text("OK")
with pytest.raises(ParameterNameConflicts):
app.router.finalize()
def test_route_invalid_host(app):
host = 321
with pytest.raises(ValueError) as excinfo:
@app.get("/test", host=host)
def handler(request):
return text("pass")
assert str(excinfo.value) == (
"Expected either string or Iterable of " "host strings, not {!r}"
).format(host)
def test_route_with_regex_group(app):
@app.route("/path/to/<ext:file\.(txt)>")
async def handler(request, ext):
return text(ext)
_, response = app.test_client.get("/path/to/file.txt")
assert response.text == "txt"
def test_route_with_regex_named_group(app):
@app.route(r"/path/to/<ext:file\.(?P<ext>txt)>")
async def handler(request, ext):
return text(ext)
_, response = app.test_client.get("/path/to/file.txt")
assert response.text == "txt"
def test_route_with_regex_named_group_invalid(app):
@app.route(r"/path/to/<ext:file\.(?P<wrong>txt)>")
async def handler(request, ext):
return text(ext)
with pytest.raises(InvalidUsage) as e:
app.router.finalize()
assert e.match(
re.escape("Named group (wrong) must match your named parameter (ext)")
)
def test_route_with_regex_group_ambiguous(app):
@app.route("/path/to/<ext:file(?:\.)(txt)>")
async def handler(request, ext):
return text(ext)
with pytest.raises(InvalidUsage) as e:
app.router.finalize()
assert e.match(
re.escape(
"Could not compile pattern file(?:\.)(txt). Try using a named "
"group instead: '(?P<ext>your_matching_group)'"
)
)
def test_route_with_bad_named_param(app):
@app.route("/foo/<__bar__>")
async def handler(request):
return text("...")
with pytest.raises(SanicException):
app.router.finalize()
def test_routes_with_and_without_slash_definitions(app):
bar = Blueprint("bar", url_prefix="bar")
baz = Blueprint("baz", url_prefix="/baz")
fizz = Blueprint("fizz", url_prefix="fizz/")
buzz = Blueprint("buzz", url_prefix="/buzz/")
instances = (
(app, "foo"),
(bar, "bar"),
(baz, "baz"),
(fizz, "fizz"),
(buzz, "buzz"),
)
for instance, term in instances:
route = f"/{term}" if isinstance(instance, Sanic) else ""
@instance.get(route, strict_slashes=True)
def get_without(request):
return text(f"{term}_without")
@instance.get(f"{route}/", strict_slashes=True)
def get_with(request):
return text(f"{term}_with")
@instance.post(route, strict_slashes=True)
def post_without(request):
return text(f"{term}_without")
@instance.post(f"{route}/", strict_slashes=True)
def post_with(request):
return text(f"{term}_with")
app.blueprint(bar)
app.blueprint(baz)
app.blueprint(fizz)
app.blueprint(buzz)
for _, term in instances:
_, response = app.test_client.get(f"/{term}")
assert response.status == 200
assert response.text == f"{term}_without"
_, response = app.test_client.get(f"/{term}/")
assert response.status == 200
assert response.text == f"{term}_with"
_, response = app.test_client.post(f"/{term}")
assert response.status == 200
assert response.text == f"{term}_without"
_, response = app.test_client.post(f"/{term}/")
assert response.status == 200
assert response.text == f"{term}_with"