Skip to content

Commit

Permalink
Merge branch 'templates-request' of github.com:alex-oleshkevich/starl…
Browse files Browse the repository at this point in the history
…ette into templates-request
  • Loading branch information
alex-oleshkevich committed Jul 10, 2023
2 parents 1a2a387 + 0e9f58d commit ab6a689
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ combine-as-imports = true
[tool.mypy]
disallow_untyped_defs = true
ignore_missing_imports = true
no_implicit_optional = true
show_error_codes = true

[[tool.mypy.overrides]]
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

# Testing
black==23.3.0
coverage==7.2.5
coverage==7.2.7
importlib-metadata==6.6.0
mypy==1.3.0
ruff==0.0.263
typing_extensions==4.5.0
mypy==1.4.1
ruff==0.0.275
typing_extensions==4.7.0
types-contextvars==2.4.7.2
types-PyYAML==6.0.12.10
types-dataclasses==0.6.6
Expand All @@ -16,7 +16,7 @@ trio==0.21.0

# Documentation
mkdocs==1.4.3
mkdocs-material==9.1.15
mkdocs-material==9.1.17
mkautodoc==0.2.0

# Packaging
Expand Down
6 changes: 6 additions & 0 deletions starlette/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ def __init__(
"See more about it on https://www.starlette.io/lifespan/.",
DeprecationWarning,
)
if lifespan:
warnings.warn(
"The `lifespan` parameter cannot be used with `on_startup` or "
"`on_shutdown`. Both `on_startup` and `on_shutdown` will be "
"ignored."
)

if lifespan is None:
self.lifespan_context: Lifespan = _DefaultLifespan(self)
Expand Down
7 changes: 5 additions & 2 deletions starlette/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def TemplateResponse(
args[0], str
): # the first argument is template name (old style)
warnings.warn(
"Argument 1 of TemplateResponse must be a Request instance.",
"The `name` is not the first parameter anymore. "
"The first parameter should be the `Request` instance.\n"
'Replace `TemplateResponse(name, {"request": request})` by `TemplateResponse(request, name)`.', # noqa: E501
DeprecationWarning,
)

Expand Down Expand Up @@ -203,7 +205,8 @@ def TemplateResponse(
else: # all arguments are kwargs
if "request" not in kwargs:
warnings.warn(
"TemplateResponse requires `request` keyword argument.",
"The `TemplateResponse` now requires the `request` argument.\n"
'Replace `TemplateResponse(name, {"context": context})` by `TemplateResponse(request, name)`.", # noqa: E501
DeprecationWarning,
)
if "request" not in kwargs.get("context", {}):
Expand Down
3 changes: 2 additions & 1 deletion starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ def __init__(
backend_options: typing.Optional[typing.Dict[str, typing.Any]] = None,
cookies: httpx._client.CookieTypes = None,
headers: typing.Dict[str, str] = None,
follow_redirects: bool = True,
) -> None:
self.async_backend = _AsyncBackend(
backend=backend, backend_options=backend_options or {}
Expand Down Expand Up @@ -409,7 +410,7 @@ def __init__(
base_url=base_url,
headers=headers,
transport=transport,
follow_redirects=True,
follow_redirects=follow_redirects,
cookies=cookies,
)

Expand Down
44 changes: 44 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,50 @@ async def run_shutdown():
assert shutdown_complete


def test_lifespan_with_on_events(test_client_factory: typing.Callable[..., TestClient]):
lifespan_called = False
startup_called = False
shutdown_called = False

@contextlib.asynccontextmanager
async def lifespan(app: Starlette):
nonlocal lifespan_called
lifespan_called = True
yield

# We do not expected, neither of run_startup nor run_shutdown to be called
# we thus mark them as #pragma: no cover, to fulfill test coverage
def run_startup(): # pragma: no cover
nonlocal startup_called
startup_called = True

def run_shutdown(): # pragma: no cover
nonlocal shutdown_called
shutdown_called = True

with pytest.warns(
UserWarning,
match=(
"The `lifespan` parameter cannot be used with `on_startup` or `on_shutdown`." # noqa: E501
),
):
app = Router(
on_startup=[run_startup], on_shutdown=[run_shutdown], lifespan=lifespan
)

assert not lifespan_called
assert not startup_called
assert not shutdown_called

# Triggers the lifespan events
with test_client_factory(app):
...

assert lifespan_called
assert not startup_called
assert not shutdown_called


def test_lifespan_sync(test_client_factory):
startup_complete = False
shutdown_complete = False
Expand Down
25 changes: 24 additions & 1 deletion tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.responses import JSONResponse, Response
from starlette.responses import JSONResponse, RedirectResponse, Response
from starlette.routing import Route
from starlette.testclient import TestClient
from starlette.websockets import WebSocket, WebSocketDisconnect
Expand Down Expand Up @@ -319,3 +319,26 @@ async def app(scope, receive, send):
response = client.get("/")
cookie_set = len(response.cookies) == 1
assert cookie_set == ok


def test_forward_follow_redirects(test_client_factory):
async def app(scope, receive, send):
if "/ok" in scope["path"]:
response = Response("ok")
else:
response = RedirectResponse("/ok")
await response(scope, receive, send)

client = test_client_factory(app, follow_redirects=True)
response = client.get("/")
assert response.status_code == 200


def test_forward_nofollow_redirects(test_client_factory):
async def app(scope, receive, send):
response = RedirectResponse("/ok")
await response(scope, receive, send)

client = test_client_factory(app, follow_redirects=False)
response = client.get("/")
assert response.status_code == 307

0 comments on commit ab6a689

Please sign in to comment.