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

IO object used in requests data #744

Open
Zhong-Zendesk opened this issue Dec 12, 2024 · 1 comment
Open

IO object used in requests data #744

Zhong-Zendesk opened this issue Dec 12, 2024 · 1 comment

Comments

@Zhong-Zendesk
Copy link

Describe the bug

I have an edge case where I had to use BytesIO as the data for requests, in production environment, the IO is consumed after the first request, and all subsequent requests read '' as a result. I wanted to reproduce this behavior/issue in tests (for regression tests). However, there seems to be no way to reproduce this behavior.

Additional context

No response

Version of responses

0.25.3

Steps to Reproduce

# reproduction_test.py
import io

import pytest_responses
import requests


def bad_io():
    data = io.BytesIO(b"some binary data")
    first = requests.post("https://httpbin.org/post", data=data)
    last = requests.post("https://httpbin.org/post", data=data)
    return first, last

def test_with_responses(responses):
    responses.add(responses.POST, "https://httpbin.org/post")
    bad_io()
    assert responses.calls[0].request.body.getvalue() == b"some binary data"
    assert responses.calls[1].request.body.getvalue() == b""

if __name__ == "__main__":
    import pprint
    first_repsonse, last_response = bad_io()
   

    pprint.pprint(first_repsonse.json()["data"])
    pprint.pprint(last_response.json()["data"])

compare the difference in body/data between
python reproduction_test.py
vs
pytest reproduction_test.py

Expected Result

Test passes - the IO object is read/consumed after the first request
The second request body is ``

Actual Result

Test fails.
The second request got the same payload as the initial request.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Dec 12, 2024
@markstory
Copy link
Member

Currently responses will read and buffer any IO streams that are passed to it, which is why you're not able to reproduce the scenario you're looking to emulate. I don't have a good idea on how we could support both buffered IO objects and un-buffered ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants