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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
import os
from pathlib import Path
import re
import time
import pytest
from unit.applications.proto import ApplicationProto
from unit.applications.lang.python import ApplicationPython
from unit.option import option
client = ApplicationProto()
client_python = ApplicationPython()
@pytest.fixture(autouse=True)
def setup_method_fixture():
assert 'success' in client.conf(
{
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [{"action": {"return": 200}}],
},
), 'configure routes'
def set_format(format):
assert 'success' in client.conf(
{
'path': f'{option.temp_dir}/access.log',
'format': format,
},
'access_log',
), 'access_log format'
def test_variables_dollar():
assert 'success' in client.conf("301", 'routes/0/action/return')
def check_dollar(location, expect):
assert 'success' in client.conf(
f'"{location}"',
'routes/0/action/location',
)
assert client.get()['headers']['Location'] == expect
check_dollar(
'https://${host}${uri}path${dollar}dollar',
'https://localhost/path$dollar',
)
check_dollar('path$dollar${dollar}', 'path$$')
def test_variables_request_time(wait_for_record):
set_format('$uri $request_time')
sock = client.http(b'', raw=True, no_recv=True)
time.sleep(1)
assert client.get(url='/r_time_1', sock=sock)['status'] == 200
assert wait_for_record(r'\/r_time_1 0\.\d{3}', 'access.log') is not None
sock = client.http(
b"""G""",
no_recv=True,
raw=True,
)
time.sleep(2)
client.http(
b"""ET /r_time_2 HTTP/1.1
Host: localhost
Connection: close
""",
sock=sock,
raw=True,
)
assert wait_for_record(r'\/r_time_2 [1-9]\.\d{3}', 'access.log') is not None
def test_variables_method(search_in_file, wait_for_record):
set_format('$method')
reg = r'^GET$'
assert search_in_file(reg, 'access.log') is None
assert client.get()['status'] == 200
assert wait_for_record(reg, 'access.log') is not None, 'method GET'
reg = r'^POST$'
assert search_in_file(reg, 'access.log') is None
assert client.post()['status'] == 200
assert wait_for_record(reg, 'access.log') is not None, 'method POST'
def test_variables_request_uri(search_in_file, wait_for_record):
set_format('$request_uri')
def check_request_uri(req_uri):
reg = fr'^{re.escape(req_uri)}$'
assert search_in_file(reg, 'access.log') is None
assert client.get(url=req_uri)['status'] == 200
assert wait_for_record(reg, 'access.log') is not None
check_request_uri('/3')
check_request_uri('/4*')
check_request_uri('/4%2A')
check_request_uri('/9?q#a')
def test_variables_uri(search_in_file, wait_for_record):
set_format('$uri')
def check_uri(uri, expect=None):
expect = uri if expect is None else expect
reg = fr'^{re.escape(expect)}$'
assert search_in_file(reg, 'access.log') is None
assert client.get(url=uri)['status'] == 200
assert wait_for_record(reg, 'access.log') is not None
check_uri('/3')
check_uri('/4*')
check_uri('/5%2A', '/5*')
check_uri('/9?q#a', '/9')
def test_variables_uri_no_cache(temp_dir):
os.makedirs(f'{temp_dir}/foo/bar')
Path(f'{temp_dir}/foo/bar/index.html').write_text('index')
assert 'success' in client.conf(
{
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [
{
"action": {
"rewrite": "/foo${uri}/",
"share": f'{temp_dir}$uri',
}
}
],
}
)
assert client.get(url='/bar')['status'] == 200
def test_variables_host(search_in_file, wait_for_record):
set_format('$host')
def check_host(host, expect=None):
expect = host if expect is None else expect
reg = fr'^{re.escape(expect)}$'
assert search_in_file(reg, 'access.log') is None
assert (
client.get(headers={'Host': host, 'Connection': 'close'})['status']
== 200
)
assert wait_for_record(reg, 'access.log') is not None
check_host('localhost')
check_host('localhost1.', 'localhost1')
check_host('localhost2:7080', 'localhost2')
check_host('.localhost')
check_host('www.localhost')
def test_variables_remote_addr(search_in_file, wait_for_record):
set_format('$remote_addr')
assert client.get()['status'] == 200
assert wait_for_record(r'^127\.0\.0\.1$', 'access.log') is not None
assert 'success' in client.conf(
{"[::1]:7080": {"pass": "routes"}}, 'listeners'
)
reg = r'^::1$'
assert search_in_file(reg, 'access.log') is None
assert client.get(sock_type='ipv6')['status'] == 200
assert wait_for_record(reg, 'access.log') is not None
def test_variables_time_local(
date_to_sec_epoch, search_in_file, wait_for_record
):
set_format('$uri $time_local $uri')
assert search_in_file(r'/time_local', 'access.log') is None
assert client.get(url='/time_local')['status'] == 200
assert wait_for_record(r'/time_local', 'access.log') is not None, 'time log'
date = search_in_file(r'^\/time_local (.*) \/time_local$', 'access.log')[1]
assert (
abs(
date_to_sec_epoch(date, '%d/%b/%Y:%X %z')
- time.mktime(time.localtime())
)
< 5
), '$time_local'
def test_variables_request_line(search_in_file, wait_for_record):
set_format('$request_line')
reg = r'^GET \/r_line HTTP\/1\.1$'
assert search_in_file(reg, 'access.log') is None
assert client.get(url='/r_line')['status'] == 200
assert wait_for_record(reg, 'access.log') is not None
def test_variables_status(search_in_file, wait_for_record):
set_format('$status')
assert 'success' in client.conf("418", 'routes/0/action/return')
reg = r'^418$'
assert search_in_file(reg, 'access.log') is None
assert client.get()['status'] == 418
assert wait_for_record(reg, 'access.log') is not None
def test_variables_header_referer(search_in_file, wait_for_record):
set_format('$method $header_referer')
def check_referer(referer):
reg = fr'^GET {re.escape(referer)}$'
assert search_in_file(reg, 'access.log') is None
assert (
client.get(
headers={
'Host': 'localhost',
'Connection': 'close',
'Referer': referer,
}
)['status']
== 200
)
assert wait_for_record(reg, 'access.log') is not None
check_referer('referer-value')
check_referer('')
check_referer('no')
def test_variables_header_user_agent(search_in_file, wait_for_record):
set_format('$method $header_user_agent')
def check_user_agent(user_agent):
reg = fr'^GET {re.escape(user_agent)}$'
assert search_in_file(reg, 'access.log') is None
assert (
client.get(
headers={
'Host': 'localhost',
'Connection': 'close',
'User-Agent': user_agent,
}
)['status']
== 200
)
assert wait_for_record(reg, 'access.log') is not None
check_user_agent('MSIE')
check_user_agent('')
check_user_agent('no')
def test_variables_many(search_in_file, wait_for_record):
def check_vars(uri, expect):
reg = fr'^{re.escape(expect)}$'
assert search_in_file(reg, 'access.log') is None
assert client.get(url=uri)['status'] == 200
assert wait_for_record(reg, 'access.log') is not None
set_format('$uri$method')
check_vars('/1', '/1GET')
set_format('${uri}${method}')
check_vars('/2', '/2GET')
set_format('${uri}$method')
check_vars('/3', '/3GET')
set_format('$method$method')
check_vars('/', 'GETGET')
def test_variables_dynamic(wait_for_record):
set_format('$header_foo$cookie_foo$arg_foo')
assert (
client.get(
url='/?foo=h',
headers={'Foo': 'b', 'Cookie': 'foo=la', 'Connection': 'close'},
)['status']
== 200
)
assert wait_for_record(r'^blah$', 'access.log') is not None
def test_variables_dynamic_arguments(search_in_file, wait_for_record):
def check_arg(url, expect=None):
expect = url if expect is None else expect
reg = fr'^{re.escape(expect)}$'
assert search_in_file(reg, 'access.log') is None
assert client.get(url=url)['status'] == 200
assert wait_for_record(reg, 'access.log') is not None
def check_no_arg(url):
assert client.get(url=url)['status'] == 200
assert search_in_file(r'^0$', 'access.log') is None
set_format('$arg_foo_bar')
check_arg('/?foo_bar=1', '1')
check_arg('/?foo_b%61r=2', '2')
check_arg('/?bar&foo_bar=3&foo', '3')
check_arg('/?foo_bar=l&foo_bar=4', '4')
check_no_arg('/')
check_no_arg('/?foo_bar=')
check_no_arg('/?Foo_bar=0')
check_no_arg('/?foo-bar=0')
check_no_arg('/?foo_bar=0&foo_bar=l')
set_format('$arg_foo_b%61r')
check_no_arg('/?foo_b=0')
check_no_arg('/?foo_bar=0')
set_format('$arg_f!~')
check_no_arg('/?f=0')
check_no_arg('/?f!~=0')
def test_variables_dynamic_headers(search_in_file, wait_for_record):
def check_header(header, value):
reg = fr'^{value}$'
assert search_in_file(reg, 'access.log') is None
assert (
client.get(headers={header: value, 'Connection': 'close'})['status']
== 200
)
assert wait_for_record(reg, 'access.log') is not None
def check_no_header(header):
assert (
client.get(headers={header: '0', 'Connection': 'close'})['status']
== 200
)
assert search_in_file(r'^0$', 'access.log') is None
set_format('$header_foo_bar')
check_header('foo-bar', '1')
check_header('Foo-Bar', '2')
check_no_header('foo_bar')
check_no_header('foobar')
set_format('$header_Foo_Bar')
check_header('Foo-Bar', '4')
check_header('foo-bar', '5')
check_no_header('foo_bar')
check_no_header('foobar')
def test_variables_dynamic_cookies(search_in_file, wait_for_record):
def check_no_cookie(cookie):
assert (
client.get(
headers={
'Host': 'localhost',
'Cookie': cookie,
'Connection': 'close',
},
)['status']
== 200
)
assert search_in_file(r'^0$', 'access.log') is None
set_format('$cookie_foo_bar')
reg = r'^1$'
assert search_in_file(reg, 'access.log') is None
assert (
client.get(
headers={
'Host': 'localhost',
'Cookie': 'foo_bar=1',
'Connection': 'close',
},
)['status']
== 200
)
assert wait_for_record(reg, 'access.log') is not None
check_no_cookie('fOo_bar=0')
check_no_cookie('foo_bar=')
def test_variables_response_header(temp_dir, wait_for_record):
# If response has two headers with the same name then first value
# will be stored in variable.
# $response_header_transfer_encoding value can be 'chunked' or null only.
# return
set_format(
'return@$response_header_server@$response_header_date@'
'$response_header_content_length@$response_header_connection'
)
assert client.get()['status'] == 200
assert (
wait_for_record(r'return@Unit/.*@.*GMT@0@close', 'access.log')
is not None
)
# share
Path(f'{temp_dir}/foo').mkdir()
Path(f'{temp_dir}/foo/index.html').write_text('index')
assert 'success' in client.conf(
{
"listeners": {"*:7080": {"pass": "routes"}},
"routes": [
{
"action": {
"share": f'{temp_dir}$uri',
}
}
],
}
)
set_format(
'share@$response_header_last_modified@$response_header_etag@'
'$response_header_content_type@$response_header_server@'
'$response_header_date@$response_header_content_length@'
'$response_header_connection'
)
assert client.get(url='/foo/index.html')['status'] == 200
assert (
wait_for_record(
r'share@.*GMT@".*"@text/html@Unit/.*@.*GMT@5@close', 'access.log'
)
is not None
)
# redirect
set_format(
'redirect@$response_header_location@$response_header_server@'
'$response_header_date@$response_header_content_length@'
'$response_header_connection'
)
assert client.get(url='/foo')['status'] == 301
assert (
wait_for_record(r'redirect@/foo/@Unit/.*@.*GMT@0@close', 'access.log')
is not None
)
# error
set_format(
'error@$response_header_content_type@$response_header_server@'
'$response_header_date@$response_header_content_length@'
'$response_header_connection'
)
assert client.get(url='/blah')['status'] == 404
assert (
wait_for_record(r'error@text/html@Unit/.*@.*GMT@54@close', 'access.log')
is not None
)
def test_variables_response_header_application(require, wait_for_record):
require({'modules': {'python': 'any'}})
client_python.load('chunked')
set_format('$uri@$response_header_transfer_encoding')
assert client_python.get(url='/1')['status'] == 200
assert wait_for_record(r'/1@chunked', 'access.log') is not None
def test_variables_invalid(temp_dir):
def check_variables(format):
assert 'error' in client.conf(
{
'path': f'{temp_dir}/access.log',
'format': format,
},
'access_log',
), 'access_log format'
check_variables("$")
check_variables("${")
check_variables("${}")
check_variables("$ur")
check_variables("$uri$$host")
check_variables("$uriblah")
check_variables("${uri")
check_variables("${{uri}")
check_variables("$ar")
check_variables("$arg")
check_variables("$arg_")
check_variables("$cookie")
check_variables("$cookie_")
check_variables("$header")
check_variables("$header_")
|