-
Notifications
You must be signed in to change notification settings - Fork 31
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
How to mock and test request with content passed as byte iterator #148
Comments
Hello, The documentation is already explaining how to stream chunks If something is not working in the provided example, feel free to report it. Thanks |
Hi @Colin-b, thanks for reaching out. I'am aware of the documentation, although the docs covers the case when mocked url produces the stream, which is not what I'm trying to achieve. Following code passes fine async def test_httpx_put(httpx_mock):
httpx_mock.add_response(method="PUT", url="http://put", content=b"world", match_content=b"hello")
async with httpx.AsyncClient() as client:
res = await client.put("http://put", content=b"hello")
assert res.text == "world" and checks that I call http://put with "hello" content. when I change the way of sending the data to async iterator as follows async def aiter_bytes():
yield b"h"
yield b"e"
yield b"l"
yield b"l"
yield b"o"
@pytest.mark.asyncio
async def test_httpx_put_iter(httpx_mock):
httpx_mock.add_response(method="PUT", url="http://put", content=b"world", match_content=b"hello")
async with httpx.AsyncClient() as client:
res = await client.put("http://put", content=aiter_bytes())
assert res.text == "world" the execution fails with error
comming from The code should work, based on httpx documentation you can pass content as Binary content to include in the body of the request, as bytes or a byte iterator. The content is if self.content is not None:
return await request.aread() == self.content to compare the content. Should I open new issue or we can re-open this one? Thank you! |
My bad, I read this too fast. Indeed this looks like a bug ! Thanks for reporting, I'll try to find some time |
Fixed in version 0.31.1 available on pypi |
Hello team,
I have
httpx
code that passescontent
to request using byte iterator. When trying the test code usingpytest_httpx
andmatch_content
on mocked request, I get assertion error and theaiter_bytes
is never called. Here's what I'm trying...and the exception when runnning the test
When I remove the
match_content
parameter, the test passes, but theaiter_bytes
is never called. Is there anything I'm missing? I'm not sure whether this ispytest_httpx
orhttpx
issue, although when I run the code withoutpytest
in real program, it works and sends the data properly.Thank you,
Adam
The text was updated successfully, but these errors were encountered: