jamielennox / requests-mock

Mocked responses for the requests library
https://requests-mock.readthedocs.io
Apache License 2.0
442 stars 71 forks source link

Support generators for Response.content #23

Open jamielennox opened 6 years ago

jamielennox commented 6 years ago

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?

Launchpad Details: #LP1669458 John Vandenberg - 2017-03-02 14:52:32 +0000

jamielennox commented 6 years ago

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.

Launchpad Details: #LPC Jamie Lennox - 2017-03-03 06:37:14 +0000