コード例 #1
0
def test_extra():
    info = MagicMock()
    logger = type('Logger', (), {'info': info})
    logger = AccessLogger(logger, None)
    request = MagicMock()
    request.method = 'GET'
    request.headers = {'Foo': 'Bar'}
    request.path_qs = '/foobar?v=1'
    request._read_bytes = b'testing'
    response = MagicMock()
    response.status = 500
    response.body_length = 100
    response.headers = {'Foo': 'Spam'}
    response.text = 'testing'
    logger.log(request, response, 0.15)
    assert info.call_count == 1
    assert info.call_args[1]['extra'] == {
        'details': {
            'request_duration_ms': 150.0,
            'request_headers': {
                'Foo': 'Bar',
            },
            'request_body': b'testing',
            'request_size': '7B',
            'response_headers': {
                'Foo': 'Spam',
            },
            'response_body': 'testing',
        }
    }
コード例 #2
0
def test_aiohttp_debugtoolbar():
    info = MagicMock()
    logger = type('Logger', (), {'info': info})
    logger = AccessLogger(logger, None)
    request = MagicMock()
    request.method = 'GET'
    request.path_qs = '/_debugtoolbar/whatever'
    response = MagicMock()
    response.status = 200
    response.body_length = 100
    logger.log(request, response, 0.15)
    assert info.call_count == 1
    log = json.loads(info.call_args[0][0])
    time = log.pop('time')
    assert re.fullmatch(r'\[\d\d:\d\d:\d\d\]', time)
    assert log == {
        'prefix': '●',
        'msg': 'GET /_debugtoolbar/whatever 200 100B 150ms',
        'dim': True,
    }
コード例 #3
0
def test_aiohttp_std():
    info = MagicMock()
    logger_type = type("Logger", (), {"info": info})
    logger = AccessLogger(logger_type(), "")
    request = MagicMock()
    request.method = 'GET'
    request.path_qs = '/foobar?v=1'
    response = MagicMock()
    response.status = 200
    response.body_length = 100
    logger.log(request, response, 0.15)
    assert info.call_count == 1
    log = json.loads(info.call_args[0][0])
    time = log.pop('time')
    assert re.fullmatch(r'\[\d\d:\d\d:\d\d\]', time)
    assert log == {
        'prefix': '●',
        'msg': 'GET /foobar?v=1 200 100B 150ms',
        'dim': False,
    }