Support generators for Response.content

Bug #1669458 reported by John Vandenberg
6
This bug affects 1 person
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_timeout(*args):
    yield 'Fake read data'.encode('utf8')
    raise requests.exceptions.ReadTimeout('Fake read timeout')

...
requests_mocker.get(mock_url, content=raise_read_timeout)

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=BytesIOWithRaise()
where BytesIOWithRaise is a basic BytesIO with .read() that emits some chunks before raising an exception. Solution at https://github.com/coala/coala/pull/3833

Or is there another better way to do this?

Revision history for this message
Jamie Lennox (jamielennox) wrote :

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.

Changed in requests-mock:
importance: Undecided → Wishlist
milestone: none → 1.4
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.