forked from benoitc/gunicorn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_logger.py
91 lines (80 loc) · 3.07 KB
/
test_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import datetime
from types import SimpleNamespace
import pytest
from gunicorn.config import Config
from gunicorn.glogging import Logger
def test_atoms_defaults():
response = SimpleNamespace(
status='200', response_length=1024,
headers=(('Content-Type', 'application/json'),), sent=1024,
)
request = SimpleNamespace(headers=(('Accept', 'application/json'),))
environ = {
'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar',
'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar',
'SERVER_PROTOCOL': 'HTTP/1.1',
}
logger = Logger(Config())
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
assert isinstance(atoms, dict)
assert atoms['r'] == 'GET /my/path?foo=bar HTTP/1.1'
assert atoms['m'] == 'GET'
assert atoms['U'] == '/my/path'
assert atoms['q'] == 'foo=bar'
assert atoms['H'] == 'HTTP/1.1'
assert atoms['b'] == '1024'
assert atoms['B'] == 1024
assert atoms['{accept}i'] == 'application/json'
assert atoms['{content-type}o'] == 'application/json'
def test_atoms_zero_bytes():
response = SimpleNamespace(
status='200', response_length=0,
headers=(('Content-Type', 'application/json'),), sent=0,
)
request = SimpleNamespace(headers=(('Accept', 'application/json'),))
environ = {
'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar',
'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar',
'SERVER_PROTOCOL': 'HTTP/1.1',
}
logger = Logger(Config())
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
assert atoms['b'] == '0'
assert atoms['B'] == 0
@pytest.mark.parametrize('auth', [
# auth type is case in-sensitive
'Basic YnJrMHY6',
'basic YnJrMHY6',
'BASIC YnJrMHY6',
])
def test_get_username_from_basic_auth_header(auth):
request = SimpleNamespace(headers=())
response = SimpleNamespace(
status='200', response_length=1024, sent=1024,
headers=(('Content-Type', 'text/plain'),),
)
environ = {
'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar',
'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar',
'SERVER_PROTOCOL': 'HTTP/1.1',
'HTTP_AUTHORIZATION': auth,
}
logger = Logger(Config())
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
assert atoms['u'] == 'brk0v'
def test_get_username_handles_malformed_basic_auth_header():
"""Should catch a malformed auth header"""
request = SimpleNamespace(headers=())
response = SimpleNamespace(
status='200', response_length=1024, sent=1024,
headers=(('Content-Type', 'text/plain'),),
)
environ = {
'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar',
'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar',
'SERVER_PROTOCOL': 'HTTP/1.1',
'HTTP_AUTHORIZATION': 'Basic ixsTtkKzIpVTncfQjbBcnoRNoDfbnaXG',
}
logger = Logger(Config())
atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1))
assert atoms['u'] == '-'