示例#1
0
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/"
示例#2
0
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")
示例#3
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')}),
    )
示例#4
0
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')
示例#5
0
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()
示例#6
0
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/"
示例#7
0
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()
示例#8
0
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/"
示例#9
0
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], {}))
示例#10
0
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")
示例#11
0
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'
示例#12
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)
示例#13
0
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')
示例#14
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")
示例#15
0
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
    }))
示例#16
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')
示例#17
0
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
    }))
示例#18
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")
        }),
    )
示例#19
0
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/'
示例#20
0
def test_build_error(basic_map: Map) -> None:
    basic_map.add(Rule("/values/<int:x>/", {"GET"}, "values"))
    adapter = basic_map.bind(False, "")
    with pytest.raises(BuildError) as error:
        adapter.build("bob")
    assert "No endpoint found" in str(error.value)
    with pytest.raises(BuildError) as error:
        adapter.build("values", values={"y": 2})
    assert "do not match" in str(error.value)
    with pytest.raises(BuildError) as error:
        adapter.build("values", method="POST")
    assert "not one of" in str(error.value)
示例#21
0
def test_build_error(basic_map: Map) -> None:
    basic_map.add(Rule('/values/<int:x>/', {'GET'}, 'values'))
    adapter = basic_map.bind('http', '')
    with pytest.raises(BuildError) as error:
        adapter.build('bob')
    assert 'No endpoint found' in str(error)
    with pytest.raises(BuildError) as error:
        adapter.build('values', values={'y': 2})
    assert 'do not match' in str(error)
    with pytest.raises(BuildError) as error:
        adapter.build('values', method='POST')
    assert 'not one of' in str(error)
示例#22
0
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"
示例#23
0
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")
示例#24
0
def test_redirect_url_host() -> None:
    map_ = Map(host_matching=True)
    map_.add(Rule('/path/', {'GET'}, 'branch', host='quart.com'))
    map_.add(Rule('/path/', {'GET'}, 'branch', host='flask.com'))
    _test_match_redirect(map_,
                         '/path',
                         'GET',
                         'http://quart.com/path/',
                         host='quart.com')
    _test_match_redirect(map_,
                         '/path',
                         'GET',
                         'http://flask.com/path/',
                         host='flask.com')
示例#25
0
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_
示例#26
0
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_
示例#27
0
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(False, "")
    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/"
示例#28
0
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'}),
    )
示例#29
0
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)
示例#30
0
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], {}))