def basic_map() -> Map: map_ = Map() map_.add(Rule("/", {"POST"}, "index")) map_.add(Rule("/", {"DELETE"}, "delete_index")) map_.add(Rule("/leaf", {"GET"}, "leaf")) map_.add(Rule("/branch/", {"GET"}, "branch")) return map_
def test_path_converter() -> None: map_ = Map() map_.add(Rule('/', ['GET'], 'index')) map_.add(Rule('/constant', ['GET'], 'constant')) map_.add(Rule('/<int:integer>', ['GET'], 'integer')) map_.add(Rule('/<path:page>', ['GET'], 'page')) map_.add(Rule('/<path:page>/constant', ['GET'], 'page_constant')) map_.add(Rule('/<path:left>/middle/<path:right>', ['GET'], 'double_page')) map_.add(Rule('/<path:left>/middle/<path:right>/constant', ['GET'], 'double_page_constant')) map_.add(Rule('/Colon:<path:name>', ['GET'], 'colon_path')) map_.add(Rule('/Colon:<name>', ['GET'], 'colon_base')) _test_match(map_, '/', 'GET', (map_.endpoints['index'][0], {})) _test_match(map_, '/constant', 'GET', (map_.endpoints['constant'][0], {})) _test_match(map_, '/20', 'GET', (map_.endpoints['integer'][0], {'integer': 20})) _test_match(map_, '/branch/leaf', 'GET', (map_.endpoints['page'][0], {'page': 'branch/leaf'})) _test_match( map_, '/branch/constant', 'GET', (map_.endpoints['page_constant'][0], {'page': 'branch'}), ) _test_match( map_, '/branch/middle/leaf', 'GET', (map_.endpoints['double_page'][0], {'left': 'branch', 'right': 'leaf'}), ) _test_match( map_, '/branch/middle/leaf/constant', 'GET', (map_.endpoints['double_page_constant'][0], {'left': 'branch', 'right': 'leaf'}), ) _test_match( map_, '/Colon:branch', 'GET', (map_.endpoints['colon_base'][0], {'name': 'branch'}), ) _test_match( map_, '/Colon:branch/leaf', 'GET', (map_.endpoints['colon_path'][0], {'name': 'branch/leaf'}), )
def basic_map() -> Map: map_ = Map() map_.add(Rule('/', {'POST'}, 'index')) map_.add(Rule('/', {'DELETE'}, 'delete_index')) map_.add(Rule('/leaf', {'GET'}, 'leaf')) map_.add(Rule('/branch/', {'GET'}, 'branch')) return map_
def test_string_converter_match() -> None: map_ = Map() map_.add(Rule("/<string(length=2):value>", {"GET"}, "string")) _test_match(map_, "/uk", "GET", (map_.endpoints["string"][0], { "value": "uk" })) _test_no_match(map_, "/usa", "GET")
def test_build_external() -> None: map_ = Map() map_.add(Rule('/ws/', {'GET'}, 'websocket', is_websocket=True)) map_.add(Rule('/', {'GET'}, 'index')) adapter = map_.bind(True, 'localhost') adapter.build("websocket") == "wss://localhost/ws/" adapter.build("index") == "https://localhost/"
def test_build_external() -> None: map_ = Map() map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=True)) map_.add(Rule("/", {"GET"}, "index")) adapter = map_.bind(True, "localhost") adapter.build("websocket") == "wss://localhost/ws/" adapter.build("index") == "https://localhost/"
def test_string_converter_match() -> None: map_ = Map() map_.add(Rule('/<string(length=2):value>', {'GET'}, 'string')) _test_match(map_, '/uk', 'GET', (map_.endpoints['string'][0], { 'value': 'uk' })) _test_no_match(map_, '/usa', 'GET')
def test_uuid_converter_match() -> None: map_ = Map() map_.add(Rule('/<uuid:uuid>', ['GET'], 'uuid')) _test_match( map_, '/a8098c1a-f86e-11da-bd1a-00112444be1e', 'GET', (map_.endpoints['uuid'][0], {'uuid': uuid.UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')}), )
def test_websocket_bad_request(websocket: bool) -> None: map_ = Map() map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=websocket)) adapter = map_.bind_to_request(False, "", "GET", "/ws/", b"", not websocket, "") with pytest.raises(BadRequest): adapter.match()
def test_websocket_and_method_not_allowed() -> None: map_ = Map() map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=True)) map_.add(Rule("/ws/", {"POST"}, "post")) adapter = map_.bind_to_request(False, "", "PUT", "/ws/", b"", False, "") with pytest.raises(MethodNotAllowed): adapter.match()
def test_root_path_build() -> None: map_ = Map() map_.add(Rule("/", {"GET"}, "http")) adapter = map_.bind_to_request(False, "", "GET", "/", b"", False, "/rooti") assert adapter.build("http", method="GET") == "/rooti/" # Relative root_path case adapter = map_.bind_to_request(False, "", "GET", "/", b"", False, "rooti") assert adapter.build("http", method="GET") == "/rooti/"
def test_disabled_strict_slashes() -> None: map_ = Map() map_.add(Rule('/foo', {'GET'}, 'foo', strict_slashes=False)) _test_match(map_, '/foo', 'GET', (map_.endpoints['foo'][0], {})) _test_match(map_, '/foo/', 'GET', (map_.endpoints['foo'][0], {})) map_.add(Rule('/bar/', {'GET'}, 'bar', strict_slashes=False)) _test_match(map_, '/bar', 'GET', (map_.endpoints['bar'][0], {})) _test_match(map_, '/bar/', 'GET', (map_.endpoints['bar'][0], {}))
def test_websocket() -> None: map_ = Map() map_.add(Rule("/ws/", {"GET"}, "http")) map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=True)) _test_match(map_, "/ws/", "GET", (map_.endpoints["http"][0], {})) _test_match(map_, "/ws/", "GET", (map_.endpoints["websocket"][0], {}), websocket=True)
def test_host() -> None: map_ = Map(host_matching=True) map_.add(Rule("/", {"GET"}, "index")) map_.add(Rule("/", {"GET"}, "subdomain", host="quart.com")) _test_match(map_, "/", "GET", (map_.endpoints["index"][0], {})) _test_match(map_, "/", "GET", (map_.endpoints["subdomain"][0], {}), host="quart.com")
def test_strict_slashes() -> None: def _test_strict_slashes(map_: Map) -> None: adapter = map_.bind_to_request('http', '', 'POST', '/path/') with pytest.raises(MethodNotAllowed): adapter.match() _test_match_redirect(map_, '/path', 'GET', '/path/') map_ = Map() map_.add(Rule('/path', {'POST'}, 'leaf')) map_.add(Rule('/path/', {'GET'}, 'branch')) # Ensure that the matching is invariant under reveresed order of # addition to a Map. map_reveresed = Map() map_reveresed.add(Rule('/path', {'POST'}, 'leaf')) map_reveresed.add(Rule('/path/', {'GET'}, 'branch')) _test_strict_slashes(map_) _test_strict_slashes(map_reveresed)
def test_value_building() -> None: map_ = Map() map_.add(Rule('/book/<page>', {'GET'}, 'book')) adapter = map_.bind('http', '') assert adapter.build('book', values={'page': 1}) == '/book/1' assert adapter.build('book', values={ 'page': 1, 'line': 12 }) == '/book/1?line=12'
def test_host() -> None: map_ = Map(host_matching=True) map_.add(Rule('/', {'GET'}, 'index')) map_.add(Rule('/', {'GET'}, 'subdomain', host='quart.com')) _test_match(map_, '/', 'GET', (map_.endpoints['index'][0], {})) _test_match(map_, '/', 'GET', (map_.endpoints['subdomain'][0], {}), host='quart.com')
def test_ordering() -> None: map_ = Map() map_.add(Rule('/fixed', ['GET'], 'fixed')) map_.add(Rule('/<path:path>', ['GET'], 'path')) map_.add(Rule('/<path:left>/<path:right>', ['GET'], 'path')) _test_match(map_, '/fixed', 'GET', (map_.endpoints['fixed'][0], {})) _test_match(map_, '/path', 'GET', (map_.endpoints['path'][1], {'path': 'path'})) _test_match( map_, '/left/right', 'GET', (map_.endpoints['path'][0], {'left': 'left', 'right': 'right'}), )
def test_float_converter_match() -> None: map_ = Map() map_.add(Rule('/<float(max=1000.0):value>', {'GET'}, 'max')) map_.add(Rule('/<float:value>', {'GET'}, 'any')) _test_match(map_, '/1001.0', 'GET', (map_.endpoints['any'][0], { 'value': 1001.0 })) _test_match(map_, '/999.0', 'GET', (map_.endpoints['max'][0], { 'value': 999.0 }))
def test_strict_slashes() -> None: def _test_strict_slashes(map_: Map) -> None: adapter = map_.bind_to_request(False, "", "POST", "/path/", b"", False, "") with pytest.raises(MethodNotAllowed): adapter.match() _test_match_redirect(map_, "/path", "GET", "/path/") map_ = Map() map_.add(Rule("/path", {"POST"}, "leaf")) map_.add(Rule("/path/", {"GET"}, "branch")) # Ensure that the matching is invariant under reversed order of # addition to a Map. map_reversed = Map() map_reversed.add(Rule("/path", {"POST"}, "leaf")) map_reversed.add(Rule("/path/", {"GET"}, "branch")) _test_strict_slashes(map_) _test_strict_slashes(map_reversed)
def test_any_converter() -> None: map_ = Map() map_.add(Rule('/<any(about, "left,right", jeff):name>', {'GET'}, 'any')) _test_match(map_, '/about', 'GET', (map_.endpoints['any'][0], { 'name': 'about' })) _test_match(map_, '/left,right', 'GET', (map_.endpoints['any'][0], { 'name': 'left,right' })) _test_no_match(map_, '/other', 'GET')
def test_float_converter_match() -> None: map_ = Map() map_.add(Rule("/<float(max=1000.0):value>", {"GET"}, "max")) map_.add(Rule("/<float:value>", {"GET"}, "any")) _test_match(map_, "/1001.0", "GET", (map_.endpoints["any"][0], { "value": 1001.0 })) _test_match(map_, "/999.0", "GET", (map_.endpoints["max"][0], { "value": 999.0 }))
def test_any_converter() -> None: map_ = Map() map_.add(Rule('/<any(about, "left,right", jeff):name>', {"GET"}, "any")) _test_match(map_, "/about", "GET", (map_.endpoints["any"][0], { "name": "about" })) _test_match(map_, "/left,right", "GET", (map_.endpoints["any"][0], { "name": "left,right" })) _test_no_match(map_, "/other", "GET")
def test_path_converter() -> None: map_ = Map() map_.add(Rule("/", {"GET"}, "index")) map_.add(Rule("/constant", {"GET"}, "constant")) map_.add(Rule("/<int:integer>", {"GET"}, "integer")) map_.add(Rule("/<path:page>", {"GET"}, "page")) map_.add(Rule("/<path:page>/constant", {"GET"}, "page_constant")) map_.add(Rule("/<path:left>/middle/<path:right>", {"GET"}, "double_page")) map_.add( Rule("/<path:left>/middle/<path:right>/constant", {"GET"}, "double_page_constant")) map_.add(Rule("/Colon:<path:name>", {"GET"}, "colon_path")) map_.add(Rule("/Colon:<name>", {"GET"}, "colon_base")) _test_match(map_, "/", "GET", (map_.endpoints["index"][0], {})) _test_match(map_, "/constant", "GET", (map_.endpoints["constant"][0], {})) _test_match(map_, "/20", "GET", (map_.endpoints["integer"][0], { "integer": 20 })) _test_match(map_, "/branch/leaf", "GET", (map_.endpoints["page"][0], { "page": "branch/leaf" })) _test_match(map_, "/branch/constant", "GET", (map_.endpoints["page_constant"][0], { "page": "branch" })) _test_match( map_, "/branch/middle/leaf", "GET", (map_.endpoints["double_page"][0], { "left": "branch", "right": "leaf" }), ) _test_match( map_, "/branch/middle/leaf/constant", "GET", (map_.endpoints["double_page_constant"][0], { "left": "branch", "right": "leaf" }), ) _test_match(map_, "/Colon:branch", "GET", (map_.endpoints["colon_base"][0], { "name": "branch" })) _test_match( map_, "/Colon:branch/leaf", "GET", (map_.endpoints["colon_path"][0], { "name": "branch/leaf" }), )
def test_defaults() -> None: map_ = Map() map_.add(Rule('/book/', ['GET'], 'book', defaults={'page': 1})) map_.add(Rule('/book/<int:page>/', ['GET'], 'book')) _test_match(map_, '/book/', 'GET', (map_.endpoints['book'][0], {'page': 1})) _test_match_redirect(map_, '/book/1/', 'GET', '/book/') _test_match(map_, '/book/2/', 'GET', (map_.endpoints['book'][1], {'page': 2})) adapter = map_.bind('http', '') assert adapter.build('book', method='GET') == '/book/' assert adapter.build('book', method='GET', values={'page': 1}) == '/book/' assert adapter.build('book', method='GET', values={'page': 2}) == '/book/2/'
def test_disabled_strict_slashes() -> None: map_ = Map() map_.add(Rule("/", {"GET"}, "index", strict_slashes=False)) _test_match(map_, "/", "GET", (map_.endpoints["index"][0], {})) _test_match(map_, "//", "GET", (map_.endpoints["index"][0], {})) map_.add(Rule("/foo", {"GET"}, "foo", strict_slashes=False)) _test_match(map_, "/foo", "GET", (map_.endpoints["foo"][0], {})) _test_match(map_, "/foo/", "GET", (map_.endpoints["foo"][0], {})) map_.add(Rule("/bar/", {"GET"}, "bar", strict_slashes=False)) _test_match(map_, "/bar", "GET", (map_.endpoints["bar"][0], {})) _test_match(map_, "/bar/", "GET", (map_.endpoints["bar"][0], {}))
def test_uuid_converter_match() -> None: map_ = Map() map_.add(Rule("/<uuid:uuid>", {"GET"}, "uuid")) _test_match( map_, "/a8098c1a-f86e-11da-bd1a-00112444be1e", "GET", (map_.endpoints["uuid"][0], { "uuid": uuid.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e") }), )
def test_strict_slashes() -> None: def _test_strict_slashes(map_: Map) -> None: adapter = map_.bind_to_request('http', 'localhost', 'POST', '/path/') with pytest.raises(MethodNotAllowed): adapter.match() adapter = map_.bind_to_request('http', 'localhost', 'GET', '/path') try: adapter.match() except RedirectRequired as error: assert error.redirect_path == '/path/' map_ = Map() map_.add(Rule('/path', ['POST'], 'leaf')) map_.add(Rule('/path/', ['GET'], 'branch')) # Ensure that the matching is invariant under reveresed order of # addition to a Map. map_reveresed = Map() map_reveresed.add(Rule('/path', ['POST'], 'leaf')) map_reveresed.add(Rule('/path/', ['GET'], 'branch')) _test_strict_slashes(map_) _test_strict_slashes(map_reveresed)
def test_value_building() -> None: map_ = Map() map_.add(Rule("/book/<page>", {"GET"}, "book")) adapter = map_.bind(False, "") assert adapter.build("book", values={"page": 1}) == "/book/1" assert adapter.build("book", values={ "page": 1, "line": 12 }) == "/book/1?line=12" assert adapter.build("book", values={ "page": 1, "line": [1, 2] }) == "/book/1?line=1&line=2"
def test_root_path_match() -> None: map_ = Map() map_.add(Rule("/", {"GET"}, "http")) _test_no_match(map_, "/", "GET", root_path="/rooti") _test_match(map_, "/rooti/", "GET", (map_.endpoints["http"][0], {}), root_path="/rooti") # Relative root_path case _test_match(map_, "/rooti/", "GET", (map_.endpoints["http"][0], {}), root_path="rooti")