getsentry / getsentry/responses

IO object used in requests data

Open
#744 1 comment 0 reactions 0 assignees View on GitHub
Improvement
Dominant language
Python
Stars
4.3k
Forks
378
Avg merge
4d 1h
Merged PRs (30d)
1

Description

### 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

```python
# 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.