예제 #1
0
 def test_decode_bad_data(self):
     fp = BytesIO(b'\x00' * 10)
     with pytest.raises(DecodeError):
         HTTPResponse(fp, headers={'content-encoding': 'deflate'})
예제 #2
0
    def test_deflate_streaming_tell_intermediate_point(self):
        # Ensure that ``tell()`` returns the correct number of bytes when
        # part-way through streaming compressed content.
        import zlib

        NUMBER_OF_READS = 10

        class MockCompressedDataReading(BytesIO):
            """
            A ByteIO-like reader returning ``payload`` in ``NUMBER_OF_READS``
            calls to ``read``.
            """
            def __init__(self, payload, payload_part_size):
                self.payloads = [
                    payload[i * payload_part_size:(i + 1) * payload_part_size]
                    for i in range(NUMBER_OF_READS + 1)
                ]
                self.consumed = 0

                assert b"".join(self.payloads) == payload

            def read(self, _):
                # Amount is unused.
                if len(self.payloads) > 0:
                    payload = self.payloads.pop(0)
                    self.consumed += len(payload)
                    return payload
                return b""

            def __iter__(self):
                return self

            def __next__(self):
                if not self.payloads:
                    raise StopIteration()
                return self.read(None)

            next = __next__

        uncompressed_data = zlib.decompress(ZLIB_PAYLOAD)

        payload_part_size = len(ZLIB_PAYLOAD) // NUMBER_OF_READS
        fp = MockCompressedDataReading(ZLIB_PAYLOAD, payload_part_size)
        resp = HTTPResponse(fp,
                            headers={'content-encoding': 'deflate'},
                            preload_content=False)
        parts = []
        stream = resp.stream(1)

        for part in stream:
            parts.append(part)
            assert resp.tell() == fp.consumed

        end_of_stream = resp.tell()

        with pytest.raises(StopIteration):
            next(stream)

        # Check that the payload is equal to the uncompressed data
        payload = b"".join(parts)
        assert uncompressed_data == payload

        # Check that the end of the stream is in the correct place
        assert len(ZLIB_PAYLOAD) == end_of_stream
예제 #3
0
 def test_get_case_insensitive_headers(self):
     headers = {'host': 'example.com'}
     r = HTTPResponse(headers=headers)
     assert r.headers.get('host') == 'example.com'
     assert r.headers.get('Host') == 'example.com'
예제 #4
0
    def test__iter__(self, payload, expected_stream):
        actual_stream = []
        for chunk in HTTPResponse(BytesIO(payload), preload_content=False):
            actual_stream.append(chunk)

        assert actual_stream == expected_stream
예제 #5
0
 def test_body_blob(self):
     resp = HTTPResponse(b'foo')
     assert resp.data == b'foo'
     assert resp.closed
예제 #6
0
 def test_read_not_chunked_response_as_chunks(self):
     fp = BytesIO(b"foo")
     resp = HTTPResponse(fp, preload_content=False)
     r = resp.read_chunked()
     with pytest.raises(ResponseNotChunked):
         next(r)
예제 #7
0
 def test_get_case_insensitive_headers(self):
     headers = {"host": "example.com"}
     r = HTTPResponse(headers=headers)
     assert r.headers.get("host") == "example.com"
     assert r.headers.get("Host") == "example.com"
예제 #8
0
 def test_read_not_chunked_response_as_chunks(self):
     fp = BytesIO(b'foo')
     resp = HTTPResponse(fp, preload_content=False)
     r = resp.read_chunked()
     self.assertRaises(ResponseNotChunked, next, r)
예제 #9
0
 def test_none(self):
     r = HTTPResponse(None)
     self.assertEqual(r.data, None)
예제 #10
0
 def test_cache_content(self):
     r = HTTPResponse('foo')
     self.assertEqual(r.data, 'foo')
     self.assertEqual(r._body, 'foo')
예제 #11
0
 def test_default(self):
     r = HTTPResponse()
     self.assertEqual(r.data, None)
예제 #12
0
    def test_empty_stream(self):
        fp = BytesIO(b'')
        resp = HTTPResponse(fp, preload_content=False)
        stream = resp.stream(2, decode_content=False)

        self.assertRaises(StopIteration, next, stream)
예제 #13
0
 def test_getheader(self):
     headers = {'host': 'example.com'}
     r = HTTPResponse(headers=headers)
     self.assertEqual(r.getheader('host'), 'example.com')
예제 #14
0
 def test_body_blob(self):
     resp = HTTPResponse(b'foo')
     self.assertEqual(resp.data, b'foo')
     self.assertTrue(resp.closed)
예제 #15
0
 def test_cache_content(self):
     r = HTTPResponse("foo")
     assert r.data == "foo"
     assert r._body == "foo"
예제 #16
0
 def test_get_case_insensitive_headers(self):
     headers = {'host': 'example.com'}
     r = HTTPResponse(headers=headers)
     self.assertEqual(r.headers.get('host'), 'example.com')
     self.assertEqual(r.headers.get('Host'), 'example.com')
예제 #17
0
 def test_default(self):
     r = HTTPResponse()
     assert r.data is None
예제 #18
0
    def test_decode_brotli(self):
        data = brotli.compress(b"foo")

        fp = BytesIO(data)
        r = HTTPResponse(fp, headers={"content-encoding": "br"})
        assert r.data == b"foo"
예제 #19
0
 def test_none(self):
     r = HTTPResponse(None)
     assert r.data is None
예제 #20
0
 def test_decode_brotli_error(self):
     fp = BytesIO(b"foo")
     with pytest.raises(DecodeError):
         HTTPResponse(fp, headers={"content-encoding": "br"})
예제 #21
0
 def test_geturl(self):
     fp = BytesIO(b"")
     request_url = "https://example.com"
     resp = HTTPResponse(fp, request_url=request_url)
     assert resp.geturl() == request_url
예제 #22
0
 def test_body_blob(self):
     resp = HTTPResponse(b"foo")
     assert resp.data == b"foo"
     assert resp.closed
예제 #23
0
 def test_decode_bad_data(self):
     fp = BytesIO(b"\x00" * 10)
     with pytest.raises(DecodeError):
         HTTPResponse(fp, headers={"content-encoding": "deflate"})
예제 #24
0
 def test_getheader(self):
     headers = {"host": "example.com"}
     r = HTTPResponse(headers=headers)
     assert r.getheader("host") == "example.com"
예제 #25
0
 def test_closed_streaming(self):
     fp = BytesIO(b'foo')
     resp = HTTPResponse(fp, preload_content=False)
     resp.close()
     with pytest.raises(StopIteration):
         next(resp.stream())
예제 #26
0
 def test_length_no_header(self):
     fp = BytesIO(b"12345")
     resp = HTTPResponse(fp, preload_content=False)
     assert resp.length_remaining is None
예제 #27
0
 def test_getheader(self):
     headers = {'host': 'example.com'}
     r = HTTPResponse(headers=headers)
     assert r.getheader('host') == 'example.com'
예제 #28
0
    def test_length_w_valid_header(self):
        headers = {"content-length": "5"}
        fp = BytesIO(b"12345")

        resp = HTTPResponse(fp, headers=headers, preload_content=False)
        assert resp.length_remaining == 5
예제 #29
0
 def test_cache_content(self):
     r = HTTPResponse('foo')
     assert r.data == 'foo'
     assert r._body == 'foo'
예제 #30
0
 def test_decode_gzip_error(self):
     fp = BytesIO(b'foo')
     with pytest.raises(DecodeError):
         HTTPResponse(fp, headers={'content-encoding': 'gzip'})