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

Allow plain integers as HTTP status codes #1406

Closed
wants to merge 2 commits into from

Conversation

Rosuav
Copy link

@Rosuav Rosuav commented Oct 6, 2023

Since an IntEnum is largely equivalent to the corresponding integer, allow HTTP status codes to be specified as (eg) 200 rather than http.HTTPStatus.OK.

@aaugustin
Copy link
Member

Quick review notes:

  • Test needed
  • No documentation needed
  • What about other implementations?

@Rosuav
Copy link
Author

Rosuav commented Oct 7, 2023

Added a test, but either I'm running the tests wrongly, or there are other failures. Can someone check please? I'm getting failures in eleven tests, mostly ImportError: cannot import name 'CloseCode' from 'websockets.frames', when running tests on the main branch.

Other implementations? If you mean the fact that I put this into the legacy/ directory, that seems to be where this happens, regardless of how it's called upon. My original code (where I ran into this problem) uses async with websockets.serve(..., process_request=...) and has that function asynchronously return the three-tuple.

@aaugustin
Copy link
Member

Thank you for the test case :-)

My comment about "other implementations" was a note to myself to check the Sans-I/O and the threading implementation because they're probably vulnerable to the same issue.

@aaugustin
Copy link
Member

Other implementations: actually, websockets already contains similar code in the Sans-I/O implementation.

This supports aligning the legacy implementation to this behavior, like you've done.

@aaugustin
Copy link
Member

The test that you added passes without code changes. The conversion from int to HTTPStatus is happening here:

self.status = http.HTTPStatus(status)

@aaugustin
Copy link
Member

And there is already a test for this behavior:

@with_server(create_protocol=ProcessRequestReturningIntProtocol)
def test_process_request_returns_int_status(self):
response = self.loop.run_until_complete(self.make_http_request("/__health__/"))
with contextlib.closing(response):
self.assertEqual(response.code, 200)
self.assertEqual(response.read(), b"OK\n")

@aaugustin aaugustin closed this Oct 21, 2023
aaugustin added a commit that referenced this pull request Oct 21, 2023
The code and tests already support it but the types didn't reflect it.

Refs #1406.
@Rosuav
Copy link
Author

Rosuav commented Oct 21, 2023

Are you sure it all works? I wasn't running into issues with a type checker, but with an AttributeError when it tried to get some piece of information from the enum (which wasn't on the plain int).

@aaugustin
Copy link
Member

I didn't work in the last release. It works in the release I made on Saturday.

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