Exemple #1
0
    url_adapter = Mock()
    rule = Rule('/', ['GET'], 'index')
    url_adapter.match.return_value = (rule, {'arg': 'value'})
    app.create_url_adapter = lambda *_: url_adapter  # type: ignore
    request = Request('GET', 'http', '/', CIMultiDict())
    RequestContext(app, request)
    assert request.url_rule == rule
    assert request.view_args == {'arg': 'value'}


@pytest.mark.parametrize(
    'exception_type, exception_instance',
    [
        (MethodNotAllowed, MethodNotAllowed(['GET'])),
        (NotFound, NotFound()),
        (RedirectRequired, RedirectRequired('/')),
    ],
)
def test_request_context_matching_error(
    exception_type: Exception,
    exception_instance: Exception,
) -> None:
    app = Quart(__name__)
    url_adapter = Mock()
    url_adapter.match.side_effect = exception_instance
    app.create_url_adapter = lambda *_: url_adapter  # type: ignore
    request = Request('GET', 'http', '/', CIMultiDict())
    RequestContext(app, request)
    assert isinstance(request.routing_exception,
                      exception_type)  # type: ignore
Exemple #2
0
def test_redirect_required() -> None:
    error = RedirectRequired('/redirect')
    assert '/redirect' in error.get_response().headers['Location']
Exemple #3
0
    url_adapter.match.return_value = (rule, {"arg": "value"})
    app.create_url_adapter = lambda *_: url_adapter  # type: ignore
    request = Request(
        "GET", "http", "/", b"", CIMultiDict(), "", "1.1", send_push_promise=no_op_push
    )
    RequestContext(app, request)
    assert request.url_rule == rule
    assert request.view_args == {"arg": "value"}


@pytest.mark.parametrize(
    "exception_type, exception_instance",
    [
        (MethodNotAllowed, MethodNotAllowed(["GET"])),
        (NotFound, NotFound()),
        (RedirectRequired, RedirectRequired("/")),
    ],
)
def test_request_context_matching_error(
    exception_type: Exception, exception_instance: Exception
) -> None:
    app = Quart(__name__)
    url_adapter = Mock()
    url_adapter.match.side_effect = exception_instance
    app.create_url_adapter = lambda *_: url_adapter  # type: ignore
    request = Request(
        "GET", "http", "/", b"", CIMultiDict(), "", "1.1", send_push_promise=no_op_push
    )
    RequestContext(app, request)
    assert isinstance(request.routing_exception, exception_type)  # type: ignore
Exemple #4
0
def test_redirect_required() -> None:
    error = RedirectRequired("/redirect")
    assert "/redirect" in error.get_response().headers["Location"]