Skip to content

Commit 56af7bb

Browse files
Tests: added tests for the URI rewrite.
1 parent 14d6d97 commit 56af7bb

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

test/test_rewrite.py

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
import os
2+
3+
import pytest
4+
from unit.applications.proto import TestApplicationProto
5+
from unit.option import option
6+
7+
8+
class TestRewrite(TestApplicationProto):
9+
prerequisites = {}
10+
11+
def setup_method(self):
12+
assert 'success' in self.conf(
13+
{
14+
"listeners": {"*:7080": {"pass": "routes"}},
15+
"routes": [
16+
{
17+
"match": {"uri": "/"},
18+
"action": {"rewrite": "/new", "pass": "routes"},
19+
},
20+
{"match": {"uri": "/new"}, "action": {"return": 200}},
21+
],
22+
"applications": {},
23+
"settings": {"http": {"log_route": True}},
24+
},
25+
), 'set initial configuration'
26+
27+
def set_rewrite(self, rewrite, uri):
28+
assert 'success' in self.conf(
29+
[
30+
{
31+
"match": {"uri": "/"},
32+
"action": {"rewrite": rewrite, "pass": "routes"},
33+
},
34+
{"match": {"uri": uri}, "action": {"return": 200}},
35+
],
36+
'routes',
37+
)
38+
39+
def test_rewrite(self):
40+
assert self.get()['status'] == 200
41+
assert (
42+
self.wait_for_record(rf'\[notice\].*"routes/1" selected')
43+
is not None
44+
)
45+
assert len(self.findall(rf'\[notice\].*URI rewritten to "/new"')) == 1
46+
assert len(self.findall(rf'\[notice\].*URI rewritten')) == 1
47+
48+
self.set_rewrite("", "")
49+
assert self.get()['status'] == 200
50+
51+
def test_rewrite_variable(self):
52+
self.set_rewrite("/$host", "/localhost")
53+
assert self.get()['status'] == 200
54+
55+
self.set_rewrite("${uri}a", "/a")
56+
assert self.get()['status'] == 200
57+
58+
def test_rewrite_encoded(self):
59+
assert 'success' in self.conf(
60+
[
61+
{
62+
"match": {"uri": "/f"},
63+
"action": {"rewrite": "${request_uri}oo", "pass": "routes"},
64+
},
65+
{"match": {"uri": "/foo"}, "action": {"return": 200}},
66+
],
67+
'routes',
68+
)
69+
assert self.get(url='/%66')['status'] == 200
70+
71+
assert 'success' in self.conf(
72+
[
73+
{
74+
"match": {"uri": "/f"},
75+
"action": {
76+
"rewrite": "${request_uri}o%6F",
77+
"pass": "routes",
78+
},
79+
},
80+
{"match": {"uri": "/foo"}, "action": {"return": 200}},
81+
],
82+
'routes',
83+
)
84+
assert self.get(url='/%66')['status'] == 200
85+
86+
def test_rewrite_arguments(self):
87+
assert 'success' in self.conf(
88+
[
89+
{
90+
"match": {"uri": "/foo", "arguments": {"arg": "val"}},
91+
"action": {"rewrite": "/new?some", "pass": "routes"},
92+
},
93+
{
94+
"match": {"uri": "/new", "arguments": {"arg": "val"}},
95+
"action": {"return": 200},
96+
},
97+
],
98+
'routes',
99+
)
100+
assert self.get(url='/foo?arg=val')['status'] == 200
101+
102+
def test_rewrite_njs(self):
103+
if 'njs' not in option.available['modules'].keys():
104+
pytest.skip('NJS is not available')
105+
106+
self.set_rewrite("`/${host}`", "/localhost")
107+
assert self.get()['status'] == 200
108+
109+
def test_rewrite_location(self):
110+
def check_location(rewrite, expect):
111+
assert 'success' in self.conf(
112+
{
113+
"listeners": {"*:7080": {"pass": "routes"}},
114+
"routes": [
115+
{
116+
"action": {
117+
"return": 301,
118+
"location": "$uri",
119+
"rewrite": rewrite,
120+
}
121+
}
122+
],
123+
}
124+
)
125+
assert self.get()['headers']['Location'] == expect
126+
127+
check_location('/new', '/new')
128+
check_location('${request_uri}new', '/new')
129+
130+
def test_rewrite_share(self, temp_dir):
131+
os.makedirs(f'{temp_dir}/dir')
132+
os.makedirs(f'{temp_dir}/foo')
133+
134+
with open(f'{temp_dir}/foo/index.html', 'w') as fooindex:
135+
fooindex.write('fooindex')
136+
137+
# same action block
138+
139+
assert 'success' in self.conf(
140+
{
141+
"listeners": {"*:7080": {"pass": "routes"}},
142+
"routes": [
143+
{
144+
"action": {
145+
"rewrite": "${request_uri}dir",
146+
"share": f'{temp_dir}$uri',
147+
}
148+
}
149+
],
150+
}
151+
)
152+
153+
resp = self.get()
154+
assert resp['status'] == 301, 'redirect status'
155+
assert resp['headers']['Location'] == '/dir/', 'redirect Location'
156+
157+
# request_uri
158+
159+
index_path = f'{temp_dir}${{request_uri}}/index.html'
160+
assert 'success' in self.conf(
161+
{
162+
"listeners": {"*:7080": {"pass": "routes"}},
163+
"routes": [
164+
{
165+
"match": {"uri": "/foo"},
166+
"action": {
167+
"rewrite": "${request_uri}dir",
168+
"pass": "routes",
169+
},
170+
},
171+
{"action": {"share": index_path}},
172+
],
173+
}
174+
)
175+
176+
assert self.get(url='/foo')['body'] == 'fooindex'
177+
178+
# different action block
179+
180+
assert 'success' in self.conf(
181+
{
182+
"listeners": {"*:7080": {"pass": "routes"}},
183+
"routes": [
184+
{
185+
"match": {"uri": "/foo"},
186+
"action": {
187+
"rewrite": "${request_uri}dir",
188+
"pass": "routes",
189+
},
190+
},
191+
{
192+
"action": {
193+
"share": f'{temp_dir}/dir',
194+
}
195+
},
196+
],
197+
}
198+
)
199+
resp = self.get(url='/foo')
200+
assert resp['status'] == 301, 'redirect status 2'
201+
assert resp['headers']['Location'] == '/foodir/', 'redirect Location 2'
202+
203+
def test_rewrite_invalid(self, skip_alert):
204+
skip_alert(r'failed to apply new conf')
205+
206+
def check_rewrite(rewrite):
207+
assert 'error' in self.conf(
208+
[
209+
{
210+
"match": {"uri": "/"},
211+
"action": {"rewrite": rewrite, "pass": "routes"},
212+
},
213+
{"action": {"return": 200}},
214+
],
215+
'routes',
216+
)
217+
218+
check_rewrite("/$blah")
219+
check_rewrite(["/"])

0 commit comments

Comments
 (0)