vmalloc / Flask-Loopback

Library for mocking HTTP requests against an existing Flask application without actual network traffic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when using iter_content

d1618033 opened this issue · comments

mock server code:

    @returned.route("/files/binary")
    def binary_file():
        return send_file(os.path.join(os.path.dirname(__file__), "files", "binary_file"))

client code:

def test_binary_file_using_iter_content(active_app, url):
    resp = requests.get(url.add_path("files/binary"))
    resp.raise_for_status()
    result = b''.join([chunk for chunk in resp.iter_content(chunk_size=1024)])
    assert result == b'hello world'

error:

    def test_binary_file_using_iter_content(active_app, url):
        resp = requests.get(url.add_path("files/binary"))
        resp.raise_for_status()
>       result = b''.join([chunk for chunk in resp.iter_content(chunk_size=1024)])

tests/test_download_file.py:7: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/test_download_file.py:7: in <listcomp>
    result = b''.join([chunk for chunk in resp.iter_content(chunk_size=1024)])
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def generate():
        # Special case for urllib3.
        if hasattr(self.raw, 'stream'):
            try:
                for chunk in self.raw.stream(chunk_size, decode_content=True):
                    yield chunk
            except ProtocolError as e:
                raise ChunkedEncodingError(e)
            except DecodeError as e:
                raise ContentDecodingError(e)
            except ReadTimeoutError as e:
                raise ConnectionError(e)
        else:
            # Standard file-like object.
            while True:
>               chunk = self.raw.read(chunk_size)
E               AttributeError: 'NoneType' object has no attribute 'read'

.env/lib/python3.8/site-packages/requests/models.py:762: AttributeError

I found a similar library to this one called requests-mock which implements the raw object.
https://github.com/jamielennox/requests-mock/blob/master/requests_mock/response.py#L177
Should I copy over the code from there, or do you want to add requests-mock as a dependency?