Skip to content

Commit

Permalink
test: pragma no branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Orenoid committed Sep 22, 2024
1 parent f3e2813 commit 4258aa8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
6 changes: 3 additions & 3 deletions starlette/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def app(self) -> typing.Any:

@property
def url(self) -> URL:
if not hasattr(self, "_url"):
if not hasattr(self, "_url"): # pragma: no branch
self._url = URL(scope=self.scope)
return self._url

Expand Down Expand Up @@ -122,7 +122,7 @@ def headers(self) -> Headers:

@property
def query_params(self) -> QueryParams:
if not hasattr(self, "_query_params"):
if not hasattr(self, "_query_params"): # pragma: no branch
self._query_params = QueryParams(self.scope["query_string"])
return self._query_params

Expand Down Expand Up @@ -237,7 +237,7 @@ async def body(self) -> bytes:
return self._body

async def json(self) -> typing.Any:
if not hasattr(self, "_json"):
if not hasattr(self, "_json"): # pragma: no branch
body = await self.body()
self._json = json.loads(body)
return self._json
Expand Down
28 changes: 0 additions & 28 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,6 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
assert response.json() == {"method": "GET", "url": "https://example.org:123/"}


def test_request_lazy_load_property(test_client_factory: TestClientFactory) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None:
request = Request(scope, receive)
assert not hasattr(request, "_url")
assert not hasattr(request, "_query_params")
assert not hasattr(request, "_json")
# trigger lazy loading
_, _, _ = request.url, request.query_params, await request.json()
assert hasattr(request, "_url")
assert hasattr(request, "_query_params")
assert hasattr(request, "_json")
data = {
"url": str(request.url),
"query_params": dict(request.query_params),
"json": await request.json(),
}
response = JSONResponse(data)
await response(scope, receive, send)

client = test_client_factory(app)
response = client.post("/42?foo=bar", json={"baz": "qux"})
assert response.json() == {
"url": "http://testserver/42?foo=bar",
"query_params": {"foo": "bar"},
"json": {"baz": "qux"},
}


def test_request_query_params(test_client_factory: TestClientFactory) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None:
request = Request(scope, receive)
Expand Down

0 comments on commit 4258aa8

Please sign in to comment.