patrys / httmock

A mocking library for requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with requests and iter_lines

domoritz opened this issue · comments

I am trying to mock a response for requests but httmock breaks iter_lines from requests.

With

import requests
from httmock import urlmatch, HTTMock


@urlmatch(netloc=r'uw.edu')
def local_mock(url, request):
    return {'status_code': 200, 'content': 'Hello, World'}


def main():
    with HTTMock(local_mock):
        print list(requests.get('http://uw.edu').iter_lines())

if __name__ == '__main__':
    main()

I get the following error

Traceback (most recent call last):
  File "mock_bug.py", line 15, in <module>
    main()
  File "mock_bug.py", line 12, in main
    print list(requests.get('http://uw.edu').iter_lines())
  File ".../python2.7/site-packages/requests/models.py", line 648, in iter_lines
    decode_unicode=decode_unicode):
  File ".../python2.7/site-packages/requests/models.py", line 625, in generate
    chunk = self.raw.read(chunk_size)
AttributeError: 'NoneType' object has no attribute 'read'

Running the same code without the mock works fine.

I've fixed this in release 1.2.1. Thanks!