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
|
from pathlib import Path
import pytest
from unit.applications.proto import ApplicationProto
from unit.option import option
from unit.utils import waitforfiles
prerequisites = {'modules': {'njs': 'any'}}
client = ApplicationProto()
@pytest.fixture(autouse=True)
def setup_method_fixture(temp_dir):
assert 'success' in client.conf(
{
"listeners": {"*:8080": {"pass": "routes"}},
"routes": [{"action": {"share": f"{temp_dir}/assets$uri"}}],
}
)
def create_files(*files):
assets_dir = f'{option.temp_dir}/assets/'
Path(assets_dir).mkdir(exist_ok=True)
_ = [Path(assets_dir + f).touch() for f in files]
waitforfiles(*[assets_dir + f for f in files])
def set_share(share):
assert 'success' in client.conf(share, 'routes/0/action/share')
def check_expression(expression, url='/'):
set_share(f'"`{option.temp_dir}/assets{expression}`"')
assert client.get(url=url)['status'] == 200
def test_njs_template_string(temp_dir):
create_files('str', '`string`', '`backtick', 'l1\nl2')
check_expression('/str')
check_expression('/\\\\`backtick')
check_expression('/l1\\nl2')
set_share(f'"{temp_dir}/assets/`string`"')
assert client.get()['status'] == 200
def test_njs_template_expression():
create_files('str', 'localhost')
check_expression('${uri}', '/str')
check_expression('${uri}${host}')
check_expression('${uri + host}')
check_expression('${uri + `${host}`}')
def test_njs_iteration():
create_files('Connection,Host', 'close,localhost')
check_expression('/${Object.keys(headers).sort().join()}')
check_expression('/${Object.values(headers).sort().join()}')
def test_njs_variables(temp_dir):
create_files('str', 'localhost', '127.0.0.1')
check_expression('/${host}')
check_expression('/${remoteAddr}')
check_expression('/${headers.Host}')
set_share(f'"`{temp_dir}/assets/${{cookies.foo}}`"')
assert (
client.get(headers={'Cookie': 'foo=str', 'Connection': 'close'})[
'status'
]
== 200
), 'cookies'
set_share(f'"`{temp_dir}/assets/${{args.foo}}`"')
assert client.get(url='/?foo=str')['status'] == 200, 'args'
check_expression('/${vars.header_host}')
set_share(f'"`{temp_dir}/assets/${{vars[\\"arg_foo\\"]}}`"')
assert client.get(url='/?foo=str')['status'] == 200, 'vars'
set_share(f'"`{temp_dir}/assets/${{vars.non_exist}}`"')
assert client.get()['status'] == 404, 'undefined'
create_files('undefined')
assert client.get()['status'] == 200, 'undefined 2'
def test_njs_variables_cacheable(temp_dir):
create_files('str')
def check_rewrite(rewrite, uri):
assert 'success' in client.conf(
[
{
"action": {
"rewrite": rewrite,
"share": f"`{temp_dir}/assets{uri}`",
},
},
],
'routes',
)
assert client.get()['status'] == 200
check_rewrite('/str', '${uri}')
check_rewrite('/str', '${vars.uri}')
def test_njs_variables_cacheable_access_log(findall, temp_dir):
assert 'success' in client.conf({"return": 200}, 'routes/0/action')
assert 'success' in client.conf(
{
'path': f'{temp_dir}/access.log',
'format': '`${vars.host}, ${vars.status}\n`',
},
'access_log'
), 'access_log configure'
reqs = 50
for _ in range(reqs):
client.get()
assert len(findall(r'localhost, 200', 'access.log')) == reqs
def test_njs_invalid(skip_alert):
skip_alert(r'js exception:')
def check_invalid(template):
assert 'error' in client.conf(template, 'routes/0/action/share')
check_invalid('"`a"')
check_invalid('"`a``"')
check_invalid('"`a`/"')
check_invalid('"`${vars.}`"')
def check_invalid_resolve(template):
assert 'success' in client.conf(template, 'routes/0/action/share')
assert client.get()['status'] == 500
check_invalid_resolve('"`${a}`"')
check_invalid_resolve('"`${uri.a.a}`"')
check_invalid_resolve('"`${vars.a.a}`"')
|