Exemplo n.º 1
0
def test_uses_stop_unless_rules():
    queue = []
    logger = HttpLogger(
        queue=queue, rules="!response_header:blahblahblah! stop_unless !.*!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 0

    queue = []
    logger = HttpLogger(queue=queue, rules="!response_body! stop_unless !.*!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!response_body! stop_unless !.*World.*!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!response_body! stop_unless !.*blahblahblah.*!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 0
Exemplo n.º 2
0
def test_uses_remove_rules():
    queue = []
    logger = HttpLogger(queue=queue, rules="!.*! remove")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 0

    queue = []
    logger = HttpLogger(queue=queue, rules="!request_body! remove")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' not in queue[0]
    assert '["response_body",' in queue[0]

    queue = []
    logger = HttpLogger(queue=queue, rules="!response_body! remove")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' in queue[0]
    assert '["response_body",' not in queue[0]

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!request_body|response_body! remove")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' not in queue[0]
    assert '["response_body",' not in queue[0]

    queue = []
    logger = HttpLogger(queue=queue, rules="!request_header:.*! remove")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' in queue[0]
    assert '["request_header:' not in queue[0]
    assert '["response_body",' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!request_header:abc! remove\n!response_body! remove")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' in queue[0]
    assert '["request_header:' in queue[0]
    assert '["request_header:abc' not in queue[0]
    assert '["response_body",' not in queue[0]
Exemplo n.º 3
0
def test_formats_response_with_empty_body():
    queue = []
    logger = HttpLogger(queue=queue, rules="include debug")
    HttpMessage.send(
        logger,
        request=mock_request(),
        response=mock_response_with_html(),
        response_body="",
    )
    assert len(queue) == 1
    msg = queue[0]
    assert parseable(msg) is True
    assert '["response_code","200"]' in msg
    assert '["response_header:content-type","text/html; charset=utf-8"]' in msg
    assert "response_body" not in msg
Exemplo n.º 4
0
def test_uses_sample_rules():
    queue = []

    try:
        HttpLogger(queue=queue, rules="sample 10\nsample 99")
        assert False is True
    except Exception as e:
        assert str(e) == "Multiple sample rules"

    logger = HttpLogger(queue=queue, rules="sample 10")
    for i in range(1, 101):
        HttpMessage.send(
            logger,
            request=mock_request_with_json2(),
            response=mock_response_with_html(),
        )
    assert 2 <= len(queue) <= 20
Exemplo n.º 5
0
def test_uses_replace_rules_with_complex_expressions():
    queue = []
    logger = HttpLogger(
        queue=queue,
        rules=
        "/response_body/ replace /[a-zA-Z0-9.!#$%&’*+\\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)/, /[email protected]/",
    )
    HttpMessage.send(
        logger,
        request=mock_request_with_json2(),
        response=mock_response_with_html(),
        response_body=MOCK_HTML.replace("World", "*****@*****.**"),
    )
    assert len(queue) == 1
    assert '["response_body","<html>Hello [email protected]!</html>"],' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="/response_body/ replace /[0-9\\.\\-\\/]{9,}/, /xyxy/")
    HttpMessage.send(
        logger,
        request=mock_request_with_json2(),
        response=mock_response_with_html(),
        response_body=MOCK_HTML.replace("World", "123-45-1343"),
    )
    assert len(queue) == 1
    assert '["response_body","<html>Hello xyxy!</html>"],' in queue[0]

    # todo this specific case not working
    # queue = []
    # logger = HttpLogger(queue=queue, rules="!response_body! replace !World!, !<b>\\0</b>!")
    # HttpMessage.send(logger, request=mock_request_with_json2(), response=mock_response_with_html())
    # assert len(queue) == 1
    # assert "[\"response_body\",\"<html>Hello <b>World</b>!</html>\"]," in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue, rules="!response_body! replace !(World)!, !<b>\\1</b>!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["response_body","<html>Hello <b>World</b>!</html>"],' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules=
        "!response_body! replace !<input([^>]*)>([^<]*)</input>!, !<input\\1></input>!",
    )
    HttpMessage.send(
        logger,
        request=mock_request_with_json2(),
        response=mock_response_with_html(),
        response_body=MOCK_HTML5,
    )
    assert len(queue) == 1
    assert (
        '["response_body","<html>\\n<input type=\\"hidden\\"></input>\\n<input class=\'foo\' type=\\"hidden\\"></input>\\n</html>"],'
        in queue[0])
Exemplo n.º 6
0
def test_uses_replace_rules():
    queue = []
    logger = HttpLogger(
        queue=queue, rules="!response_body! replace !blahblahblah!, !ZZZZZ!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert "World" in queue[0]
    assert "ZZZZZ" not in queue[0]

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!response_body! replace !World!, !Mundo!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["response_body","<html>Hello Mundo!</html>"],' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!request_body|response_body! replace !^.*!, !ZZZZZ!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body","ZZZZZ"' in queue[0]
    assert '["response_body","ZZZZZ"' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules=
        "!request_body! replace !^.*!, !QQ!\n!response_body! replace !^.*!, !SS!",
    )
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body","QQ"' in queue[0]
    assert '["response_body","SS"' in queue[0]

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!response_body! replace !World!, !!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["response_body","<html>Hello !</html>"],' in queue[0]

    queue = []
    logger = HttpLogger(queue=queue, rules="!response_body! replace !.*!, !!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["response_body",' not in queue[0]

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!response_body! replace !World!, !Z!")
    HttpMessage.send(
        logger,
        request=mock_request_with_json2(),
        response=mock_response_with_html(),
        response_body=MOCK_HTML3,
    )
    assert len(queue) == 1
    assert '["response_body","<html>1 Z 2 Z Red Z Blue Z!</html>"],' in queue[
        0]

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!response_body! replace !World!, !Z!")
    HttpMessage.send(
        logger,
        request=mock_request_with_json2(),
        response=mock_response_with_html(),
        response_body=MOCK_HTML4,
    )
    assert len(queue) == 1
    assert ('["response_body","<html>1 Z\\n2 Z\\nRed Z \\nBlue Z!\\n</html>"],'
            in queue[0])
Exemplo n.º 7
0
def test_uses_remove_unless_found_rules():
    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!response_header:blahblahblah! remove_unless_found !.*!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1

    queue = []
    logger = HttpLogger(queue=queue,
                        rules="!.*! remove_unless_found !blahblahblah!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 0

    queue = []
    logger = HttpLogger(
        queue=queue, rules="!request_body! remove_unless_found !blahblahblah!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' not in queue[0]
    assert '["response_body",' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!response_body! remove_unless_found !blahblahblah!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' in queue[0]
    assert '["response_body",' not in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!response_body|request_body! remove_unless_found !World!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' not in queue[0]
    assert '["response_body",' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!response_body|request_body! remove_unless_found !.*World.*!",
    )
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' not in queue[0]
    assert '["response_body",' in queue[0]

    queue = []
    logger = HttpLogger(
        queue=queue,
        rules="!response_body|request_body! remove_unless_found !.*!")
    HttpMessage.send(logger,
                     request=mock_request_with_json2(),
                     response=mock_response_with_html())
    assert len(queue) == 1
    assert '["request_body",' in queue[0]
    assert '["response_body",' in queue[0]