예제 #1
0
    def test_response_transparently_decrypts_wrong_deflate(self):
        c = zlib_compressobj(wbits=-zlib.MAX_WBITS)
        body = c.compress(b'this is test data')
        body += c.flush()
        body_len = ('%s' % len(body)).encode('ascii')

        headers = {
            b'content-encoding': [b'deflate'],
            b'content-length': [body_len]
        }
        d = DummySocket()
        d._buffer = BytesIO(body)
        r = HTTP11Response(200, 'OK', headers, d, None)

        assert r.read() == b'this is test data'
예제 #2
0
    def test_expect_close_reads_call_close_callback(self):
        connection = mock.MagicMock()

        d = DummySocket()
        r = HTTP11Response(200, 'OK', {b'connection': [b'close']}, d,
                           connection)
        d._buffer = BytesIO(b'hello there sir')

        assert r.read(5) == b'hello'
        assert r.read(6) == b' there'
        assert r.read(8) == b' sir'
        assert r.read(9) == b''

        assert r._sock is None
        assert connection.close.call_count == 1
예제 #3
0
    def test_chunked_normal_read(self):
        d = DummySocket()
        r = HTTP11Response(200, 'OK', {b'transfer-encoding': [b'chunked']}, d,
                           None)

        data = (b'4\r\nwell\r\n'
                b'4\r\nwell\r\n'
                b'4\r\nwhat\r\n'
                b'4\r\nhave\r\n'
                b'2\r\nwe\r\n'
                b'a\r\nhereabouts\r\n'
                b'0\r\n\r\n')
        d._buffer = BytesIO(data)

        assert r.read() == b'wellwellwhathavewehereabouts'
예제 #4
0
    def test_chunked_reads_dont_call_close_callback(self):
        connection = mock.MagicMock()
        headers = {b'transfer-encoding': [b'chunked']}

        d = DummySocket()
        r = HTTP11Response(200, 'OK', headers, d, connection)

        data = (b'4\r\nwell\r\n'
                b'4\r\nwell\r\n'
                b'4\r\nwhat\r\n'
                b'4\r\nhave\r\n'
                b'2\r\nwe\r\n'
                b'a\r\nhereabouts\r\n'
                b'0\r\n\r\n')
        d._buffer = BytesIO(data)
        list(r.read_chunked())

        assert r._sock is None
        assert connection.close.call_count == 0
예제 #5
0
    def test_basic_chunked_read(self):
        d = DummySocket()
        r = HTTP11Response(200, 'OK', {b'transfer-encoding': [b'chunked']}, d,
                           None)

        data = (b'4\r\nwell\r\n'
                b'4\r\nwell\r\n'
                b'4\r\nwhat\r\n'
                b'4\r\nhave\r\n'
                b'2\r\nwe\r\n'
                b'a\r\nhereabouts\r\n'
                b'0\r\n\r\n')
        d._buffer = BytesIO(data)
        results = [b'well', b'well', b'what', b'have', b'we', b'hereabouts']

        for c1, c2 in zip(results, r.read_chunked()):
            assert c1 == c2

        assert not list(r.read_chunked())
예제 #6
0
    def test_compressed_bounded_read_expect_close(self):
        headers = {b'connection': [b'close'], b'content-encoding': [b'gzip']}

        c = zlib_compressobj(wbits=24)
        body = c.compress(b'hello there sir')
        body += c.flush()

        d = DummySocket()
        r = HTTP11Response(200, 'OK', headers, d, None)
        d._buffer = BytesIO(body)

        response = b''
        while True:
            # 12 is magic here: it's the smallest read that actually
            # decompresses immediately.
            chunk = r.read(15)
            if not chunk:
                break

            response += chunk

        assert response == b'hello there sir'

        assert r._sock is None
예제 #7
0
    def test_response_transparently_decrypts_chunked_gzip(self):
        d = DummySocket()
        headers = {
            b'content-encoding': [b'gzip'],
            b'transfer-encoding': [b'chunked'],
        }
        r = HTTP11Response(200, 'OK', headers, d, None)

        c = zlib_compressobj(wbits=24)
        body = c.compress(b'this is test data')
        body += c.flush()

        data = b''
        for index in range(0, len(body), 2):
            data += b'2\r\n' + body[index:index + 2] + b'\r\n'

        data += b'0\r\n\r\n'
        d._buffer = BytesIO(data)

        received_body = b''
        for chunk in r.read_chunked():
            received_body += chunk

        assert received_body == b'this is test data'
예제 #8
0
    def test_chunked_read_aborts_early(self):
        r = HTTP11Response(200, 'OK', {b'transfer-encoding': [b'chunked']},
                           None, None)

        assert not list(r.read_chunked())
예제 #9
0
    def test_chunked_read_of_non_chunked(self):
        r = HTTP11Response(200, 'OK', {b'content-length': [b'0']}, None, None)

        with pytest.raises(ChunkedDecodeError):
            list(r.read_chunked())
예제 #10
0
    def test_read_expect_close(self):
        d = DummySocket()
        r = HTTP11Response(200, 'OK', {b'connection': [b'close']}, d, None)

        assert r.read() == b''
예제 #11
0
    def test_aborted_reads(self):
        d = DummySocket()
        r = HTTP11Response(200, 'OK', {b'content-length': [b'15']}, d, None)

        with pytest.raises(ConnectionResetError):
            r.read()
예제 #12
0
    def test_short_circuit_read(self):
        r = HTTP11Response(200, 'OK', {b'content-length': [b'0']}, None, None)

        assert r.read() == b''