Skip to content

Commit

Permalink
Fix before request (#449)
Browse files Browse the repository at this point in the history
* added xfail

When Redirect.to_response is returned by the before request hook, it raises ImproperlyConfiguredException

* fix before_request hook

* 1.16.2

Co-authored-by: infohash <[email protected]>
  • Loading branch information
Goldziher and infohash authored Sep 2, 2022
1 parent ab8a917 commit b89ebea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

[1.16.2]

- fix `before_requiest` regression causing it to not handle returned responses from the hook.

[1.16.1]

[1.16.0]

[1.15.0]

- `examples/` directory and tests for complete documentation examples
Expand Down
30 changes: 9 additions & 21 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "starlite"
version = "1.16.1"
version = "1.16.2"
description = "Light-weight and flexible ASGI API Framework"
authors = ["Na'aman Hirschfeld <[email protected]>"]
maintainers = ["Na'aman Hirschfeld <[email protected]>", "Peter Schutt <[email protected]>", "Cody Fincher <[email protected]>"]
Expand Down
7 changes: 5 additions & 2 deletions starlite/routes/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast

from anyio.to_thread import run_sync
from starlette.responses import Response as StarletteResponse
from starlette.routing import get_name

from starlite.connection import Request
from starlite.controller import Controller
from starlite.enums import ScopeType
from starlite.exceptions import ImproperlyConfiguredException
from starlite.response import Response
from starlite.routes.base import BaseRoute
from starlite.signature import get_signature_model
from starlite.utils import is_async_callable

if TYPE_CHECKING:
from pydantic.typing import AnyCallable
from starlette.responses import Response as StarletteResponse

from starlite.handlers.http import HTTPRouteHandler
from starlite.kwargs import KwargsModel
from starlite.response import Response
from starlite.types import Method, Receive, Scope, Send


Expand Down Expand Up @@ -145,6 +145,9 @@ async def _call_handler_function(
route_handler=route_handler, parameter_model=parameter_model, request=request
)

if isinstance(response_data, (Response, StarletteResponse)):
return response_data

return await route_handler.to_response(
app=scope["app"],
data=response_data,
Expand Down
25 changes: 23 additions & 2 deletions tests/handlers/http/test_to_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from json import loads
from pathlib import Path
from time import sleep
from typing import TYPE_CHECKING, Any, AsyncIterator, Generator, Iterator
from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, Generator, Iterator

import pytest
from pydantic import ValidationError
Expand All @@ -15,7 +15,7 @@
)
from starlette.responses import Response as StarletteResponse
from starlette.responses import StreamingResponse
from starlette.status import HTTP_200_OK
from starlette.status import HTTP_200_OK, HTTP_308_PERMANENT_REDIRECT

from starlite import (
Cookie,
Expand All @@ -24,6 +24,7 @@
HTTPRoute,
MediaType,
Redirect,
Request,
Response,
ResponseHeader,
Stream,
Expand Down Expand Up @@ -141,6 +142,26 @@ def test_function() -> Redirect:
assert response.background == background_task


def test_to_response_returning_redirect_response_from_redirect() -> None:
@get(path="/proxy")
def proxy_handler() -> Dict[str, str]:
return {"message": "redirected by before request hook"}

def before_request_hook_handler(request: Request) -> RedirectResponse:
return Redirect(path="/proxy").to_response(
headers={}, media_type="application/json", status_code=HTTP_308_PERMANENT_REDIRECT, app=request.app
)

@get(path="/test", before_request=before_request_hook_handler)
def redirect_handler() -> None:
raise AssertionError("this endpoint should not be reached")

with create_test_client(route_handlers=[redirect_handler, proxy_handler]) as client:
response = client.get("/test")
assert response.status_code == HTTP_200_OK
assert response.json() == {"message": "redirected by before request hook"}


@pytest.mark.asyncio()
async def test_to_response_returning_file_response() -> None:
current_file_path = Path(__file__).resolve()
Expand Down

0 comments on commit b89ebea

Please sign in to comment.