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

Add tests to test_responses #2656

Merged
merged 7 commits into from
Aug 6, 2024
Merged

Add tests to test_responses #2656

merged 7 commits into from
Aug 6, 2024

Conversation

Orenoid
Copy link
Contributor

@Orenoid Orenoid commented Aug 1, 2024

Summary

Add tests for some of the uncovered branches in starlette.responses, 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 399 to 421
def test_set_cookie_without_optional_keys(
test_client_factory: TestClientFactory,
) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None:
response = Response("Hello, world!", media_type="text/plain")
response.set_cookie(
"mycookie",
"myvalue",
max_age=None,
expires=None,
path=None,
domain=None,
secure=False,
httponly=False,
samesite=None,
)
await response(scope, receive, send)

client = test_client_factory(app)
response = client.get("/")
assert response.headers["set-cookie"] == "mycookie=myvalue"


Copy link
Contributor Author

@Orenoid Orenoid Aug 1, 2024

Choose a reason for hiding this comment

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

https://github.com/encode/starlette/blob/master/starlette/responses.py#L105-L120
This is testing the circumstance where all those conditions are False.

Comment on lines 290 to 302
def test_file_response_with_manually_specified_media_type(
tmpdir: Path, test_client_factory: TestClientFactory
) -> None:
path = os.path.join(tmpdir, "xyz")
content = b"<file content>"
with open(path, "wb") as file:
file.write(content)
# By default, FileResponse will determine the `media_type` based on
# the filename or path, unless a specific `media_type` is provided.
app = FileResponse(path=path, filename="example.png", media_type="image/jpeg")
client: TestClient = test_client_factory(app)
response = client.get("/")
assert response.headers["content-type"] == "image/jpeg"
Copy link
Contributor Author

@Orenoid Orenoid Aug 1, 2024

Choose a reason for hiding this comment

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

Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

Thanks! 🙏

@Kludex Kludex enabled auto-merge (squash) August 3, 2024 12:13
@Orenoid
Copy link
Contributor Author

Orenoid commented Aug 5, 2024

@Kludex Thanks for the approval. I noticed that you updated some logic and variable names. Do we have relevant coding standards? If so, I can follow them while working on the rest of tests in the future.
And one more thing, would you prefer me to add the rest of the tests in separate PRs, with one PR per module, or should I add them with as few PRs as possible?

@Kludex
Copy link
Member

Kludex commented Aug 6, 2024

Do we have relevant coding standards?

We do have a page in our docs, but for the changes I did in this PR, no. I just updated the code to use tmp_path instead of tmpdir. You didn't do anything wrong.

And one more thing, would you prefer me to add the rest of the tests in separate PRs, with one PR per module, or should I add them with as few PRs as possible?

The way you did here is perfect! A couple of tests per PR is good for me.

@Kludex Kludex disabled auto-merge August 6, 2024 07:13
@Kludex Kludex enabled auto-merge (squash) August 6, 2024 07:13
@Kludex Kludex merged commit e46165a into encode:master Aug 6, 2024
5 checks passed
nixroxursox pushed a commit to nixroxursox/starlette that referenced this pull request Sep 30, 2024
* test: add test cases for uncovered branches in starlette.responses

* chore: fix format issue

* Update test

* Remove unused import

* Update tmpdir to tmp_path

---------

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
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