def test_field_name():
    content_length = ContentLength()

    assert content_length.field_name() == 'Content-length'
def test_bytes():
    content_length = ContentLength()

    assert bytes(content_length) == b'Content-length: 0\r\n'
def test_default_value():
    content_length = ContentLength()

    assert content_length.length == 0
def test_user_value():
    content_length = ContentLength(42)

    assert content_length.length == 42
示例#5
0
def test_headers_len():
    headers = [Compress(), ContentLength(length=10)]
    h = SpamcHeaders(headers=headers)

    assert len(h) == 2
def test_repr():
    content_length = ContentLength()

    assert repr(content_length) == 'ContentLength(length=0)'
示例#7
0
    def test_instantiates(self):
        content_length = ContentLength()

        assert 'content_length' in locals()
示例#8
0
def test_headers_instantiate_list():
    h = SpamcHeaders(headers=[Compress(), ContentLength(length=42)])

    assert 'h' in locals()
示例#9
0
def test_common_get_header():
    req_resp_base = RequestResponseBase()
    req_resp_base.add_header(ContentLength(length=0))

    assert isinstance(req_resp_base.get_header('Content-length'), ContentLength)
示例#10
0
def test_common_delete_header():
    req_resp_base = RequestResponseBase(body=None,
                                        headers=(ContentLength(length=0),))
    req_resp_base.delete_header('Content-length')

    assert 'Content-length' not in req_resp_base._headers.keys()
示例#11
0
def test_common_add_header():
    req_resp_base = RequestResponseBase()
    req_resp_base.add_header(ContentLength(length=0))

    assert req_resp_base._headers['Content-length']
示例#12
0
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)
示例#13
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())
示例#14
0
文件: common.py 项目: wevsty/aiospamc
 def _set_content_length(self, body_):
     if isinstance(body_, str):
         self.add_header(ContentLength(len(body_.encode())))
     elif isinstance(body_, bytes):
         self.add_header(ContentLength(len(body_)))
示例#15
0
 def _set_content_length(self, body_):
     self.add_header(ContentLength(len(body_)))