Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests in test_requests #2677

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Orenoid
Copy link
Contributor

@Orenoid Orenoid commented Aug 31, 2024

Summary

Add tests for some of the uncovered branches in starlette.requests, related to this issue

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly. (It seems no documentation needs update)

Comment on lines -428 to +456
# (" = b ; ; = ; c = ; ", {"": "b", "c": ""}),
(" = b ; ; = ; c = ; ", {"": "b", "c": ""}),
Copy link
Contributor Author

@Orenoid Orenoid Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing this, when the key and value both are empty, this pair should be ignored.

I don't know why this parameter was commented out before. However, to meet the condition where both the key and value are empty, this parameter with an empty chunk ("; ;") is what we need.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may have been on purpose? When was introduced?

Copy link
Contributor Author

@Orenoid Orenoid Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. The test and cookie_parser were introduced in this PR.
It looks like these tests were inspired by tornado test suite ( relevant commit ).
And I didn't see any discussion related to this specific parameter. It was just commented out at the beginning.

My thought is that enabling the parameter shouldn't be a problem. cookie_parser does support this case.

Copy link
Contributor Author

@Orenoid Orenoid Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just did more searching in Tornado's code and found this. They did have tested the case.

@Orenoid Orenoid changed the title test: add tests in test_requests test: add tests in test_requests Aug 31, 2024
tests/test_requests.py Outdated Show resolved Hide resolved
Comment on lines +31 to +56
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"},
}
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which lines are this trying to cover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we add pragma: no branch to these lines too?

@Orenoid
Copy link
Contributor Author

Orenoid commented Sep 7, 2024

@Kludex I'm sorry but may I ask what I should do with this PR now, as well as this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants