-
Notifications
You must be signed in to change notification settings - Fork 9
Ignore content type parameters when matching it #235
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
Changes from all commits
897dfe2
05c52d5
a8df553
e2e30a9
8783aa1
a59b8d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,7 +213,7 @@ async def __call__( | |
| codec_name = protocol.codec_name_from_content_type( | ||
| headers.get("content-type", ""), stream=not is_unary | ||
| ) | ||
| codec = self._codecs.get(codec_name.lower()) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also noticed a couple of spots of random lowercasing and consolidated them, with some cleanup to WSGI |
||
| codec = self._codecs.get(codec_name) | ||
| if not codec: | ||
| raise HTTPException( | ||
| HTTPStatus.UNSUPPORTED_MEDIA_TYPE, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
| from pyqwest import Client, Request, SyncClient, SyncRequest, SyncTransport, Transport | ||
| from pyqwest.testing import ASGITransport, WSGITransport | ||
|
|
||
| from connectrpc.codec import proto_json_codec | ||
|
|
||
| from .haberdasher_connect import ( | ||
| Haberdasher, | ||
| HaberdasherASGIApplication, | ||
| HaberdasherClient, | ||
| HaberdasherClientSync, | ||
| HaberdasherSync, | ||
| HaberdasherWSGIApplication, | ||
| ) | ||
| from .haberdasher_pb2 import Hat, Size | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pyqwest._pyqwest import Response, SyncResponse | ||
|
|
||
| _charset_content_type_cases = [ | ||
| "application/json", | ||
| "application/json; charset=utf-8", | ||
| "application/json; charset=UTF-8", | ||
| "application/json;charset=utf-8", | ||
| "application/json; charset=utf-8", | ||
| "application/json; charset=utf-8; version=1", | ||
| "application/JSON", | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("header", _charset_content_type_cases) | ||
| def test_json_charset_content_type(header: str) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it also be good to flex streaming in these tests? I see we have
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Almost thought it's too difficult to be worth it but realized I could get by with a normal client and transport middleware to force content-type |
||
| class HeadersHaberdasherSync(HaberdasherSync): | ||
| def make_hat(self, request, ctx): | ||
| return Hat(size=2) | ||
|
|
||
| transport = WSGITransport(HaberdasherWSGIApplication(HeadersHaberdasherSync())) | ||
|
|
||
| client = SyncClient(transport=transport) | ||
|
|
||
| res = client.post( | ||
| "http://localhost/connectrpc.example.Haberdasher/MakeHat", | ||
| content=b"{}", | ||
| headers={"content-type": header}, | ||
| ) | ||
| assert res.status == 200 | ||
| assert res.json() == {"size": 2} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("header", _charset_content_type_cases) | ||
| async def test_json_charset_content_type_async(header: str) -> None: | ||
| class HeadersHaberdasher(Haberdasher): | ||
| async def make_hat(self, request, ctx): | ||
| return Hat(size=2) | ||
|
|
||
| transport = ASGITransport(HaberdasherASGIApplication(HeadersHaberdasher())) | ||
|
|
||
| client = Client(transport=transport) | ||
|
|
||
| res = await client.post( | ||
| "http://localhost/connectrpc.example.Haberdasher/MakeHat", | ||
| content=b"{}", | ||
| headers={"content-type": header}, | ||
| ) | ||
| assert res.status == 200 | ||
| assert res.json() == {"size": 2} | ||
|
|
||
|
|
||
| _streaming_charset_content_type_cases = [ | ||
| "application/connect+" + h.split("/")[1] for h in _charset_content_type_cases | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("header", _streaming_charset_content_type_cases) | ||
| def test_json_charset_content_type_stream(header: str) -> None: | ||
| class HeadersHaberdasherSync(HaberdasherSync): | ||
| def make_similar_hats(self, request, ctx): | ||
| yield Hat(size=2) | ||
| yield Hat(size=3) | ||
|
|
||
| # Difficult to parse an HTTP streaming response so override the header | ||
| # with a real client's transport instead. | ||
| class HeaderTransport(SyncTransport): | ||
| def __init__(self, delegate: SyncTransport): | ||
| self._delegate = delegate | ||
|
|
||
| def execute_sync(self, request: SyncRequest) -> SyncResponse: | ||
| request.headers["content-type"] = header | ||
| return self._delegate.execute_sync(request) | ||
|
|
||
| transport = HeaderTransport( | ||
| WSGITransport(HaberdasherWSGIApplication(HeadersHaberdasherSync())) | ||
| ) | ||
|
|
||
| client = HaberdasherClientSync( | ||
| address="http://localhost", | ||
| codec=proto_json_codec(), | ||
| http_client=SyncClient(transport=transport), | ||
| ) | ||
|
|
||
| hats = list(client.make_similar_hats(Size(inches=2))) | ||
| assert hats == [Hat(size=2), Hat(size=3)] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("header", _streaming_charset_content_type_cases) | ||
| async def test_json_charset_content_type_stream_async(header: str) -> None: | ||
| class HeadersHaberdasher(Haberdasher): | ||
| async def make_similar_hats(self, request, ctx): | ||
| yield Hat(size=2) | ||
| yield Hat(size=3) | ||
|
|
||
| # Difficult to parse an HTTP streaming response so override the header | ||
| # with a real client's transport instead. | ||
| class HeaderTransport(Transport): | ||
| def __init__(self, delegate: Transport): | ||
| self._delegate = delegate | ||
|
|
||
| async def execute(self, request: Request) -> Response: | ||
| request.headers["content-type"] = header | ||
| return await self._delegate.execute(request) | ||
|
|
||
| transport = HeaderTransport( | ||
| ASGITransport(HaberdasherASGIApplication(HeadersHaberdasher())) | ||
| ) | ||
|
|
||
| client = HaberdasherClient( | ||
| address="http://localhost", | ||
| codec=proto_json_codec(), | ||
| http_client=Client(transport=transport), | ||
| ) | ||
|
|
||
| hats = [] | ||
| async for hat in client.make_similar_hats(Size(inches=2)): | ||
| hats.append(hat) | ||
| assert hats == [Hat(size=2), Hat(size=3)] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit that we're double-normalizing the content type between here and
codec_name_from_content_typebelow, but no real suggestions on a refactor — probably fine to leave.