requests-mock icon indicating copy to clipboard operation
requests-mock copied to clipboard

mocking doesn't drain iterator

Open jamielennox opened this issue 7 years ago • 2 comments

If you pass a generator to data in requests it will iterate over the generator and include that info into the request. This has the result of draining the provided iterator. Currently we don't do this, nor do we read provided files.

It's rare this would be something that an application is testing the result of. The provided case is that swiftclient updates the md5sum as the iterator is read which is currently not happening.

The downside of this is that the body available in last_request is now expended. Again this probably doesn't matter but it's a change in behaviour.

Using itertools.tee we can both drain the generator and maintain the request history - but i'm not sure it's worth it.

Launchpad Details: #LP1616751 Jamie Lennox - 2016-08-25 06:19:58 +0000

jamielennox avatar Feb 27 '18 02:02 jamielennox

Fix proposed to branch: master Review: https://review.openstack.org/360300

Launchpad Details: #LPC OpenStack Infra - 2016-08-25 07:42:07 +0000

jamielennox avatar Feb 27 '18 02:02 jamielennox

Another consequence of this is that last_request.text returns the generator instead of the resulting text, which is a much more common thing people would want to check.

That can be worked around with e.g. bytes(itertools.chain(*mock.last_request.text)).decode('utf8')

OrangeDog avatar Jul 16 '21 15:07 OrangeDog