Exemple #1
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()
Exemple #2
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')}),
    )
Exemple #3
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_
Exemple #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")
Exemple #5
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')
Exemple #6
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_
Exemple #7
0
 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/'
Exemple #8
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'
Exemple #9
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")
Exemple #10
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')
Exemple #11
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")
        }),
    )
Exemple #12
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)
Exemple #13
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)
Exemple #14
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"
Exemple #15
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")
Exemple #16
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')
Exemple #17
0
def test_method_not_allowed_error(basic_map: Map) -> None:
    adapter = basic_map.bind_to_request('http', '', 'GET', '/')
    try:
        adapter.match()
    except Exception as error:
        assert isinstance(error, MethodNotAllowed)
        assert error.allowed_methods == {'DELETE', 'POST'}
Exemple #18
0
def test_method_not_allowed_error(basic_map: Map) -> None:
    adapter = basic_map.bind_to_request(False, "", "GET", "/", b"", False, "")
    try:
        adapter.match()
    except Exception as error:
        assert isinstance(error, MethodNotAllowed)
        assert error.allowed_methods == {"DELETE", "POST"}
Exemple #19
0
def _test_no_match(map_: Map,
                   path: str,
                   method: str,
                   root_path: str = "") -> None:
    adapter = map_.bind_to_request(False, "", method, path, b"", False,
                                   root_path)
    with pytest.raises(NotFound):
        adapter.match()
Exemple #20
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'}),
    )
Exemple #21
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/"
Exemple #22
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()
Exemple #23
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/"
Exemple #24
0
def _test_match(
    map_: Map,
    path: str,
    method: str,
    expected: Tuple[Rule, Dict[str, Any]],
    host: str = '',
) -> None:
    adapter = map_.bind_to_request('http', host, method, path)
    assert adapter.match() == expected
Exemple #25
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], {}))
Exemple #26
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], {}))
Exemple #27
0
def _test_match(
    map_: Map,
    path: str,
    method: str,
    expected: Tuple[Rule, Dict[str, Any]],
    host: str = '',
    websocket: bool = False,
) -> None:
    adapter = map_.bind_to_request(False, host, method, path, b'', websocket)
    assert adapter.match() == expected
Exemple #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"
    }))
Exemple #29
0
def _test_match_redirect(map_: Map,
                         path: str,
                         method: str,
                         redirect_path: str,
                         query_string: bytes = b"",
                         host: str = "") -> None:
    adapter = map_.bind_to_request(False, host, method, path, query_string,
                                   False, "")
    with pytest.raises(RedirectRequired) as error:
        adapter.match()
    assert error.value.redirect_path == f"http://{host}{redirect_path}"
Exemple #30
0
def _test_match_redirect(
    map_: Map,
    path: str,
    method: str,
    redirect_path: str,
    query_string: bytes = b'',
    host: str = '',
) -> None:
    adapter = map_.bind_to_request('http', host, method, path, query_string)
    with pytest.raises(RedirectRequired) as error:
        adapter.match()
    assert error.value.redirect_path == redirect_path