Support generators for Response.content
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
requests-mock |
Confirmed
|
Wishlist
|
Unassigned |
Bug Description
For testing of Responses with chunked data, or other mid-read fiddling, it would be helpful if `content` was able to be a generator.
i.e.
def raise_read_
yield 'Fake read data'.encode(
raise requests.
...
requests_
Currently this is caught in `create_response`'s type checking
if content and not isinstance(content, six.binary_type):
> raise TypeError('Content should be binary data')
E TypeError: Content should be binary data
The way I found that worked was to set body=BytesIOWit
where BytesIOWithRaise is a basic BytesIO with .read() that emits some chunks before raising an exception. Solution at https:/
Or is there another better way to do this?
That's probably the best way to do it with the current code - but that's only because i've never had to use requests with chunks so i'm not sure what it is supposed to look like. Unfortunately you also have to inherit that closed hack.
The reason that type check exists is content and text are represented as bytes and unicode in a requests.Response respectively. In python 2 this makes no difference, in python 3 you need to be a bit more careful about assigning the right thing. So I added the type check so that you couldn't get yourself into the wrong situation making compatibility difficult.
I'd be happy to add generators/ iterators to that, with the requirement that they look very similar to how you would use it in requests itself (that's just a design philosophy of the library). If you're willing to contribute that we should be able to do a release pretty quickly, otherwise i'll mark it for next release and I'll have a go myself in the next couple of weeks.