-
Notifications
You must be signed in to change notification settings - Fork 43
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
Fix compatibility with httpx 0.28.0 #278
Conversation
respx/mocks.py
Outdated
request.method.decode("ascii").upper() | ||
if isinstance(request.method, bytes) |
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.
Should a DeprecationWarning
be raised for bytes so that codebases can be updated and this support can eventually be dropped?
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.
Yes, I'd expect respx to follow the same argument conventions as httpx, and the project shouldn't try and maintain accepting bytes here if httpx has dropped support.
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.
Should the minimum HTTPX version also be bumped to match?
Line 44 in 366dd0b
install_requires=["httpx>=0.21.0"], |
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.
Suggested as ndhansen#1.
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.
Should the minimum HTTPX version also be bumped to match?
I don’t think so, the library should still be compatible with older versions of httpx (they used to accept bytes for the method
kwarg).
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.
The bytes come from httpcore, and we’re only getting bytes because we’re hooking the mock after httpcore
issued the request. So there’s nothing users can do about this warning.
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.
I agree @mjpieters .. respx has always aligned with httpx and dropped support for old api's.
So, yes @hugovk, I think we should bump the required version of httpx .. respx is still compatible with older versions of httpx by using older versions of respx 😉
Commit that changes |
Quite keen on this as well, anything possible to help with getting this over the line? |
I needed to add this, but of course it is not backwards compatible: --- respx-0.21.1.orig/tests/test_api.py
+++ respx-0.21.1/tests/test_api.py
@@ -214,7 +214,7 @@ async def test_content_variants(client,
{"X-Foo": "bar"},
{
"Content-Type": "application/json",
- "Content-Length": "14",
+ "Content-Length": "13",
"X-Foo": "bar",
},
),
@@ -223,7 +223,7 @@ async def test_content_variants(client,
{"Content-Type": "application/json; charset=utf-8", "X-Foo": "bar"},
{
"Content-Type": "application/json; charset=utf-8",
- "Content-Length": "14",
+ "Content-Length": "13",
"X-Foo": "bar",
},
),
@@ -322,19 +322,19 @@ async def test_callable_content(client):
assert request.called is True
assert async_response.status_code == 200
assert async_response.text == "hello world."
- assert request.calls[-1][0].content == b'{"x": "."}'
+ assert request.calls[-1][0].content == b'{"x":"."}'
respx_mock.reset()
sync_response = httpx.post("https://foo.bar/jonas/", json={"x": "!"})
assert request.called is True
assert sync_response.status_code == 200
assert sync_response.text == "hello jonas!"
- assert request.calls[-1][0].content == b'{"x": "!"}'
+ assert request.calls[-1][0].content == b'{"x":"!"}'
async def test_request_callback(client):
def callback(request, name):
- if request.url.host == "foo.bar" and request.content == b'{"foo": "bar"}':
+ if request.url.host == "foo.bar" and request.content == b'{"foo":"bar"}':
return respx.MockResponse(
202,
headers={"X-Foo": "bar"}, |
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.
Looking good! Just remove the import changes in patterns.py
I have no problem with this .. only tests |
Done! Thanks for taking a look! The tests are failing on |
We really need this fix. Can someone help? I want to avoid hardcoding my dependency to |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #278 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 2914 2915 +1
Branches 192 192
=========================================
+ Hits 2914 2915 +1 ☔ View full report in Codecov by Sentry. |
Version |
Prior to httpx version
0.28.0
, request methods were implicitly converted from bytes to strings. In version0.28.0
that is no longer the case.Since httpx request objects expect strings for the method field, and httpcore request objects enforce bytes, I've copied the conversion from bytes to strings from the previous httpx version in to respx.
I didn't add tests because there are no explicit tests on the
to_httpx_request
method, but I can if you'd like.Fixes #277.