예제 #1
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/"
예제 #2
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/'
예제 #3
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'}
예제 #4
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"}
예제 #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_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()
예제 #7
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()
예제 #8
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
예제 #9
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
예제 #10
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}"
예제 #11
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
예제 #12
0
 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/')
예제 #13
0
def _test_no_match(map_: Map, path: str, method: str) -> None:
    adapter = map_.bind_to_request('http', '', method, path)
    with pytest.raises(NotFound):
        adapter.match()
예제 #14
0
def _test_match_redirect(map_: Map, path: str, method: str,
                         redirect_path: str) -> None:
    adapter = map_.bind_to_request('http', '', method, path)
    with pytest.raises(RedirectRequired) as error:
        adapter.match()
    assert error.value.redirect_path == redirect_path
예제 #15
0
 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/")