Esempio n. 1
0
def test_bytes_headers(x_headers):
    r = Response(version='1.5', status_code=Status.EX_OK, message='EX_OK')
    result = bytes(r).split(b'\r\n')[
        1:-2]  # strip end of headers, body and first line
    expected = [bytes(header).rstrip(b'\r\n') for header in x_headers]

    for header_bytes in result:
        assert header_bytes in expected
Esempio n. 2
0
def test_bytes_body():
    test_input = b'Test body\n'
    r = Response(version='1.5',
                 status_code=Status.EX_OK,
                 message='EX_OK',
                 body=test_input)
    result = bytes(r).rpartition(b'\r\n')[2]

    assert result == test_input
Esempio n. 3
0
def test_bytes_headers(x_headers):
    r = Response(version='1.5',
                 status_code=Status.EX_OK,
                 message='EX_OK',
                 headers=x_headers)
    result = bytes(r).partition(b'\r\n')[2]
    expected = bytes(r.headers)

    assert result.startswith(expected)
    assert result.endswith(b'\r\n\r\n')
Esempio n. 4
0
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)
Esempio n. 5
0
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': 'zlib'},
                 body=test_input)
    result = bytes(r).rpartition(b'\r\n')[2]

    assert result == zlib.compress(test_input)
Esempio n. 6
0
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()))
        else:
            assert bytes(response).endswith(body.encode())
Esempio n. 7
0
def test_response_exception(test_input, expected):
    response = Response(version='1.5', status_code=test_input, message='')
    response.status_code = test_input

    with pytest.raises(expected):
        Client._raise_response_exception(response)
Esempio n. 8
0
def test_response_exception_ok():
    response = Response(version='1.5', status_code=Status.EX_OK, message='')

    assert Client._raise_response_exception(response) is None
Esempio n. 9
0
def spamassassin_response() -> Response:
    body = read_file("sa.txt").encode()
    headers = SpamcHeaders()
    headers["Spam"] = SpamValue(value=True, score=40, threshold=20)
    return Response(headers=headers, body=body)
Esempio n. 10
0
def test_response_instantiates():
    response = Response('1.5', Status.EX_OK, 'EX_OK')

    assert 'response' in locals()
Esempio n. 11
0
def test_raise_for_status_ok():
    r = Response(version='1.5', status_code=Status.EX_OK, message='')

    assert r.raise_for_status() is None
Esempio n. 12
0
def test_str():
    r = Response(status_code=Status.EX_OK)
    result = str(r)

    assert result == '<0 - EX_OK: aiospamc.responses.Response object at {}>'.format(
        id(r))
Esempio n. 13
0
def test_raise_for_status(test_input):
    r = Response(version='1.5', status_code=test_input, message='')

    with pytest.raises(test_input.exception):
        r.raise_for_status()
Esempio n. 14
0
def test_bytes_status():
    r = Response(status_code=999, message='Test message')
    result = bytes(r).partition(b'\r\n')[0]

    assert b'999 Test message' in result
Esempio n. 15
0
def test_init_message():
    r = Response(version='1.5', status_code=Status.EX_OK, message='EX_OK')
    result = bytes(r).split(b'\r\n')[0]

    assert result.endswith(Status.EX_OK.name.encode())
Esempio n. 16
0
def test_init_status_code():
    r = Response(version='1.5', status_code=Status.EX_OK, message='EX_OK')
    result = bytes(r).split(b' ')[1]

    assert result == str(Status.EX_OK.value).encode()
Esempio n. 17
0
def test_response_from_parser_result(response_with_body):
    p = ResponseParser().parse(response_with_body)
    r = Response(**p)

    assert r is not None
Esempio n. 18
0
def test_raise_for_undefined_status():
    r = Response(version='1.5', status_code=999, message='')

    with pytest.raises(ResponseException):
        r.raise_for_status()
Esempio n. 19
0
def test_init_version():
    r = Response(version='4.2', status_code=Status.EX_OK, message='EX_OK')
    result = bytes(r).split(b' ')[0]

    assert result == b'SPAMD/4.2'