Esempio n. 1
0
def test_bytes_body_compressed():
    test_input = b'Test body\n'
    r = Request(verb='TEST',
                headers={'Compress': CompressValue()},
                body=test_input)
    result = bytes(r).rpartition(b'\r\n')[2]

    assert result == zlib.compress(test_input)
Esempio n. 2
0
def test_headers_instantiate_dict_headers():
    compress_value = CompressValue()
    content_length_value = ContentLengthValue(length=42)
    h = SpamcHeaders(headers={
        'Compress': compress_value,
        'Content-length': content_length_value
    })

    assert h['Compress'] is compress_value
    assert h['Content-length'] is content_length_value
Esempio n. 3
0
def test_compress_bytes():
    c = CompressValue()

    assert bytes(c) == b'zlib'
Esempio n. 4
0
def test_compress_repr():
    c = CompressValue()

    assert repr(c) == 'CompressValue()'
Esempio n. 5
0
def test_compress_str():
    h = CompressValue()

    assert str(h) == 'algorithm={}'.format(repr('zlib'))
Esempio n. 6
0
def test_user_repr():
    u = UserValue(name='username')

    assert repr(u) == 'UserValue(name={})'.format(repr('username'))


def test_user_bytes():
    u = UserValue(name='username')

    assert bytes(u) == b'username'


@pytest.mark.parametrize('test_input', [
    GenericHeaderValue('value'),
    CompressValue(),
    ContentLengthValue(),
    SetOrRemoveValue(ActionOption(local=True, remote=False)),
    MessageClassValue(value=MessageClassOption.ham),
    SpamValue(),
    UserValue()
])
def test_equal(test_input):
    assert test_input == test_input


@pytest.mark.parametrize('test_input', [
    GenericHeaderValue('value'),
    CompressValue(),
    ContentLengthValue(),
    SetOrRemoveValue(ActionOption(local=True, remote=False)),
Esempio n. 7
0
def test_headers_set_item():
    header1 = CompressValue()
    h = SpamcHeaders()
    h['Compress'] = header1

    assert h['Compress'] is header1
Esempio n. 8
0
def test_headers_get_item():
    header1 = CompressValue()
    h = SpamcHeaders(headers={'Compress': header1})
    result = h['Compress']

    assert result is header1
    'True ; 40.0 / NOTATHRESHOLD',
])
def test_parse_spam_value_raises_parseerror(test_input):
    with pytest.raises(ParseError):
        parse_spam_value(test_input)


def test_parse_xheader_success():
    result = parse_generic_header_value('value')

    assert isinstance(result, GenericHeaderValue)
    assert result.value == 'value'


@pytest.mark.parametrize('test_input,header,value', [
    [b'Compress: zlib', 'Compress', CompressValue(algorithm='zlib')],
    [b'Content-length: 42', 'Content-length', ContentLengthValue(length=42)],
    [b'DidRemove: local, remote', 'DidRemove', SetOrRemoveValue(action=ActionOption(local=True, remote=True))],
    [b'DidSet: local, remote', 'DidSet', SetOrRemoveValue(action=ActionOption(local=True, remote=True))],
    [b'Message-class: spam', 'Message-class', MessageClassValue(value=MessageClassOption.spam)],
    [b'Remove: local, remote', 'Remove', SetOrRemoveValue(action=ActionOption(local=True, remote=True))],
    [b'Set: local, remote', 'Set', SetOrRemoveValue(action=ActionOption(local=True, remote=True))],
    [b'Spam: True ; 40 / 20', 'Spam', SpamValue(value=True, score=40, threshold=20)],
    [b'User: username', 'User', UserValue(name='username')],
    [b'XHeader: x value', 'XHeader', GenericHeaderValue('x value')]
])
def test_parse_header_success(test_input, header, value):
    result = parse_header(test_input)

    assert result[0] == header
    assert result[1] == value