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)
Exemple #2
0
def test_headers_set_item():
    header1 = Compress()
    h = SpamcHeaders()
    h[header1.field_name()] = header1

    assert h[header1.field_name()] is header1
Exemple #3
0
def test_headers_get_item():
    header1 = Compress()
    h = SpamcHeaders(headers=[header1])
    result = h[header1.field_name()]

    assert result is header1
Exemple #4
0
def test_headers_instantiate_list():
    h = SpamcHeaders(headers=[Compress(), ContentLength(length=42)])

    assert 'h' in locals()
Exemple #5
0
    def test_field_name(self):
        compress = Compress()

        assert compress.field_name() == 'Compress'
Exemple #6
0
    def test_value(self):
        compress = Compress()

        assert compress.zlib == True
Exemple #7
0
    def test_repr(self):
        compress = Compress()

        assert repr(compress) == 'Compress()'
Exemple #8
0
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())
Exemple #9
0
    def test_instantiates(self):
        compress = Compress()

        assert 'compress' in locals()
Exemple #10
0
#                                  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()))
Exemple #11
0
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
Exemple #12
0
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'
Exemple #13
0
    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():
Exemple #14
0
 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)
Exemple #15
0
def test_headers_len():
    headers = [Compress(), ContentLength(length=10)]
    h = SpamcHeaders(headers=headers)

    assert len(h) == 2
Exemple #16
0
    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)
Exemple #18
0
def test_value():
    compress = Compress()

    assert compress.zlib is True