def test_bytes_body_compressed(): test_input = b'Test body\n' r = Response(version='1.5', status_code=Status.EX_OK, message='EX_OK', headers=[Compress()], body=test_input) result = bytes(r).split(b'\r\n', 3)[-1] assert result == zlib.compress(test_input)
def test_headers_set_item(): header1 = Compress() h = SpamcHeaders() h[header1.field_name()] = header1 assert h[header1.field_name()] is header1
def test_headers_get_item(): header1 = Compress() h = SpamcHeaders(headers=[header1]) result = h[header1.field_name()] assert result is header1
def test_headers_instantiate_list(): h = SpamcHeaders(headers=[Compress(), ContentLength(length=42)]) assert 'h' in locals()
def test_field_name(self): compress = Compress() assert compress.field_name() == 'Compress'
def test_value(self): compress = Compress() assert compress.zlib == True
def test_repr(self): compress = Compress() assert repr(compress) == 'Compress()'
import pytest from aiospamc.headers import Compress, ContentLength, XHeader from aiospamc.requests import Request def test_request_instantiates(): request = Request('TEST') assert 'request' in locals() @pytest.mark.parametrize( 'verb,body,headers', [('TEST', None, []), ('TEST', None, [XHeader('X-Tests-Head', 'Tests value')]), ('TEST', 'Test body\n', [ContentLength(length=10)]), ('TEST', 'Test body\n', [ContentLength(length=10), Compress()])]) def test_request_bytes(verb, body, headers): request = Request(verb=verb, body=body, headers=headers) assert bytes(request).startswith(verb.encode()) assert bytes(b'SPAMC/1.5\r\n') in bytes(request) assert all(bytes(header) in bytes(request) for header in headers) if body: if any(isinstance(header, Compress) for header in headers): assert bytes(request).endswith(zlib.compress(body.encode())) else: assert bytes(request).endswith(body.encode())
def test_instantiates(self): compress = Compress() assert 'compress' in locals()
# OSFileException, CantCreateException, IOErrorException, TemporaryFailureException, # ProtocolException, NoPermissionException, ConfigException, TimeoutException) from aiospamc.headers import Compress from aiospamc.responses import Response, Status def test_response_instantiates(): response = Response('1.5', Status.EX_OK, 'EX_OK') assert 'response' in locals() @pytest.mark.parametrize('version,status,message,body,headers', [ ('1.5', Status.EX_OK, 'EX_OK', None, []), ('1.5', Status.EX_OK, 'EX_OK', 'Test body\n', []), ('1.5', Status.EX_OK, 'EX_OK', 'Test body\n', [Compress()]), ]) def test_response_bytes(version, status, message, body, headers): response = Response(version=version, status_code=status, message=message, body=body, headers=headers) assert bytes(response).startswith(b'SPAMD/%b' % version.encode()) assert b' %d ' % status.value in bytes(response) assert b' %b\r\n' % message.encode() in bytes(response) assert all(bytes(header) in bytes(response) for header in headers) if body: if any(isinstance(header, Compress) for header in headers): assert bytes(response).endswith(zlib.compress(body.encode()))
def test_common_decompress_when_header_removed(): req_resp_base = RequestResponseBase(body='Test body\n', headers=(Compress(),)) req_resp_base.delete_header('Compress') assert req_resp_base._compressed_body is None
def test_common_compressed_when_header_added(): req_resp_base = RequestResponseBase('Test body\n') req_resp_base.add_header(Compress()) assert zlib.decompress(req_resp_base._compressed_body) == b'Test body\n'
assert hasattr(req_resp_base, 'body') def test_common_instantiates_with_headers(): req_resp_base = RequestResponseBase(body=None, headers=(ContentLength(length=0),)) assert isinstance(req_resp_base._headers['Content-length'], ContentLength) @pytest.mark.parametrize('test_input,headers,expected', [ (b'Test body\n', [], 'Test body\n'), (b'', [], None), (zlib.compress('Test body\n'.encode()), [Compress()], 'Test body\n') ]) def test_common_parse_body(test_input, headers, expected): body = RequestResponseBase._decode_body(test_input, headers) assert body == expected def test_common_content_length_added(): req_resp_base = RequestResponseBase() req_resp_base.body = 'Test body\n' assert req_resp_base._headers['Content-length'] def test_common_compressed_when_header_added():
async def wrapper(cls, request): if cls.compress and cls.body: cls.logger.debug('Added Compress header to request (%s)', id(request)) request.add_header(Compress()) return await func(cls, request)
def test_headers_len(): headers = [Compress(), ContentLength(length=10)] h = SpamcHeaders(headers=headers) assert len(h) == 2
def test_bytes(self): compress = Compress() assert bytes(compress) == b'Compress: zlib\r\n'
def test_bytes_body_compressed(): test_input = b'Test body\n' r = Request(verb='TEST', headers=[Compress()], body=test_input) result = bytes(r).split(b'\r\n', 3)[-1] assert result == zlib.compress(test_input)
def test_value(): compress = Compress() assert compress.zlib is True