test_routing.py (1510:945e0b006858) test_routing.py (1596:b7e2d4d92624)
1# -*- coding: utf-8 -*-
1# -*- coding: utf-8 -*-
2import unittest
2import pytest
3
4from unit.applications.proto import TestApplicationProto
3
4from unit.applications.proto import TestApplicationProto
5from conftest import option, skip_alert
5
6
7class TestRouting(TestApplicationProto):
8 prerequisites = {'modules': {'python': 'any'}}
9
6
7
8class TestRouting(TestApplicationProto):
9 prerequisites = {'modules': {'python': 'any'}}
10
10 def setUp(self):
11 super().setUp()
11 def setup_method(self):
12 super().setup_method()
12
13
13 self.assertIn(
14 'success',
15 self.conf(
16 {
17 "listeners": {"*:7080": {"pass": "routes"}},
18 "routes": [
19 {
20 "match": {"method": "GET"},
21 "action": {"return": 200},
22 }
23 ],
24 "applications": {},
25 }
26 ),
27 'routing configure',
28 )
14 assert 'success' in self.conf(
15 {
16 "listeners": {"*:7080": {"pass": "routes"}},
17 "routes": [
18 {"match": {"method": "GET"}, "action": {"return": 200},}
19 ],
20 "applications": {},
21 }
22 ), 'routing configure'
29
30 def route(self, route):
31 return self.conf([route], 'routes')
32
33 def route_match(self, match):
23
24 def route(self, route):
25 return self.conf([route], 'routes')
26
27 def route_match(self, match):
34 self.assertIn(
35 'success',
36 self.route({"match": match, "action": {"return": 200}}),
37 'route match configure',
38 )
28 assert 'success' in self.route(
29 {"match": match, "action": {"return": 200}}
30 ), 'route match configure'
39
40 def route_match_invalid(self, match):
31
32 def route_match_invalid(self, match):
41 self.assertIn(
42 'error',
43 self.route({"match": match, "action": {"return": 200}}),
44 'route match configure invalid',
45 )
33 assert 'error' in self.route(
34 {"match": match, "action": {"return": 200}}
35 ), 'route match configure invalid'
46
47 def host(self, host, status):
36
37 def host(self, host, status):
48 self.assertEqual(
49 self.get(headers={'Host': host, 'Connection': 'close'})[
50 'status'
51 ],
52 status,
53 'match host',
54 )
38 assert (
39 self.get(headers={'Host': host, 'Connection': 'close'})['status']
40 == status
41 ), 'match host'
55
56 def cookie(self, cookie, status):
42
43 def cookie(self, cookie, status):
57 self.assertEqual(
44 assert (
58 self.get(
59 headers={
60 'Host': 'localhost',
61 'Cookie': cookie,
62 'Connection': 'close',
63 },
45 self.get(
46 headers={
47 'Host': 'localhost',
48 'Cookie': cookie,
49 'Connection': 'close',
50 },
64 )['status'],
65 status,
66 'match cookie',
67 )
51 )['status']
52 == status
53 ), 'match cookie'
68
69 def test_routes_match_method_positive(self):
54
55 def test_routes_match_method_positive(self):
70 self.assertEqual(self.get()['status'], 200, 'GET')
71 self.assertEqual(self.post()['status'], 404, 'POST')
56 assert self.get()['status'] == 200, 'GET'
57 assert self.post()['status'] == 404, 'POST'
72
73 def test_routes_match_method_positive_many(self):
74 self.route_match({"method": ["GET", "POST"]})
75
58
59 def test_routes_match_method_positive_many(self):
60 self.route_match({"method": ["GET", "POST"]})
61
76 self.assertEqual(self.get()['status'], 200, 'GET')
77 self.assertEqual(self.post()['status'], 200, 'POST')
78 self.assertEqual(self.delete()['status'], 404, 'DELETE')
62 assert self.get()['status'] == 200, 'GET'
63 assert self.post()['status'] == 200, 'POST'
64 assert self.delete()['status'] == 404, 'DELETE'
79
80 def test_routes_match_method_negative(self):
81 self.route_match({"method": "!GET"})
82
65
66 def test_routes_match_method_negative(self):
67 self.route_match({"method": "!GET"})
68
83 self.assertEqual(self.get()['status'], 404, 'GET')
84 self.assertEqual(self.post()['status'], 200, 'POST')
69 assert self.get()['status'] == 404, 'GET'
70 assert self.post()['status'] == 200, 'POST'
85
86 def test_routes_match_method_negative_many(self):
87 self.route_match({"method": ["!GET", "!POST"]})
88
71
72 def test_routes_match_method_negative_many(self):
73 self.route_match({"method": ["!GET", "!POST"]})
74
89 self.assertEqual(self.get()['status'], 404, 'GET')
90 self.assertEqual(self.post()['status'], 404, 'POST')
91 self.assertEqual(self.delete()['status'], 200, 'DELETE')
75 assert self.get()['status'] == 404, 'GET'
76 assert self.post()['status'] == 404, 'POST'
77 assert self.delete()['status'] == 200, 'DELETE'
92
93 def test_routes_match_method_wildcard_left(self):
94 self.route_match({"method": "*ET"})
95
78
79 def test_routes_match_method_wildcard_left(self):
80 self.route_match({"method": "*ET"})
81
96 self.assertEqual(self.get()['status'], 200, 'GET')
97 self.assertEqual(self.post()['status'], 404, 'POST')
82 assert self.get()['status'] == 200, 'GET'
83 assert self.post()['status'] == 404, 'POST'
98
99 def test_routes_match_method_wildcard_right(self):
100 self.route_match({"method": "GE*"})
101
84
85 def test_routes_match_method_wildcard_right(self):
86 self.route_match({"method": "GE*"})
87
102 self.assertEqual(self.get()['status'], 200, 'GET')
103 self.assertEqual(self.post()['status'], 404, 'POST')
88 assert self.get()['status'] == 200, 'GET'
89 assert self.post()['status'] == 404, 'POST'
104
105 def test_routes_match_method_wildcard_left_right(self):
106 self.route_match({"method": "*GET*"})
107
90
91 def test_routes_match_method_wildcard_left_right(self):
92 self.route_match({"method": "*GET*"})
93
108 self.assertEqual(self.get()['status'], 200, 'GET')
109 self.assertEqual(self.post()['status'], 404, 'POST')
94 assert self.get()['status'] == 200, 'GET'
95 assert self.post()['status'] == 404, 'POST'
110
111 def test_routes_match_method_wildcard(self):
112 self.route_match({"method": "*"})
113
96
97 def test_routes_match_method_wildcard(self):
98 self.route_match({"method": "*"})
99
114 self.assertEqual(self.get()['status'], 200, 'GET')
100 assert self.get()['status'] == 200, 'GET'
115
116 def test_routes_match_invalid(self):
117 self.route_match_invalid({"method": "**"})
118
119 def test_routes_match_valid(self):
120 self.route_match({"method": "blah*"})
121 self.route_match({"host": "*blah*blah"})
122 self.route_match({"host": "blah*blah*blah"})
123 self.route_match({"host": "blah*blah*"})
124
125 def test_routes_match_empty_exact(self):
126 self.route_match({"uri": ""})
101
102 def test_routes_match_invalid(self):
103 self.route_match_invalid({"method": "**"})
104
105 def test_routes_match_valid(self):
106 self.route_match({"method": "blah*"})
107 self.route_match({"host": "*blah*blah"})
108 self.route_match({"host": "blah*blah*blah"})
109 self.route_match({"host": "blah*blah*"})
110
111 def test_routes_match_empty_exact(self):
112 self.route_match({"uri": ""})
127 self.assertEqual(self.get()['status'], 404)
113 assert self.get()['status'] == 404
128
129 self.route_match({"uri": "/"})
114
115 self.route_match({"uri": "/"})
130 self.assertEqual(self.get()['status'], 200)
131 self.assertEqual(self.get(url='/blah')['status'], 404)
116 assert self.get()['status'] == 200
117 assert self.get(url='/blah')['status'] == 404
132
133 def test_routes_match_negative(self):
134 self.route_match({"uri": "!"})
118
119 def test_routes_match_negative(self):
120 self.route_match({"uri": "!"})
135 self.assertEqual(self.get()['status'], 404)
121 assert self.get()['status'] == 404
136
137 self.route_match({"uri": "!/"})
122
123 self.route_match({"uri": "!/"})
138 self.assertEqual(self.get()['status'], 404)
139 self.assertEqual(self.get(url='/blah')['status'], 200)
124 assert self.get()['status'] == 404
125 assert self.get(url='/blah')['status'] == 200
140
141 self.route_match({"uri": "!*blah"})
126
127 self.route_match({"uri": "!*blah"})
142 self.assertEqual(self.get()['status'], 200)
143 self.assertEqual(self.get(url='/bla')['status'], 200)
144 self.assertEqual(self.get(url='/blah')['status'], 404)
145 self.assertEqual(self.get(url='/blah1')['status'], 200)
128 assert self.get()['status'] == 200
129 assert self.get(url='/bla')['status'] == 200
130 assert self.get(url='/blah')['status'] == 404
131 assert self.get(url='/blah1')['status'] == 200
146
147 self.route_match({"uri": "!/blah*1*"})
132
133 self.route_match({"uri": "!/blah*1*"})
148 self.assertEqual(self.get()['status'], 200)
149 self.assertEqual(self.get(url='/blah')['status'], 200)
150 self.assertEqual(self.get(url='/blah1')['status'], 404)
151 self.assertEqual(self.get(url='/blah12')['status'], 404)
152 self.assertEqual(self.get(url='/blah2')['status'], 200)
134 assert self.get()['status'] == 200
135 assert self.get(url='/blah')['status'] == 200
136 assert self.get(url='/blah1')['status'] == 404
137 assert self.get(url='/blah12')['status'] == 404
138 assert self.get(url='/blah2')['status'] == 200
153
154 def test_routes_match_wildcard_middle(self):
155 self.route_match({"host": "ex*le"})
156
157 self.host('example', 200)
158 self.host('www.example', 404)
159 self.host('example.com', 404)
160 self.host('exampl', 404)
161
162 def test_routes_match_method_case_insensitive(self):
163 self.route_match({"method": "get"})
164
139
140 def test_routes_match_wildcard_middle(self):
141 self.route_match({"host": "ex*le"})
142
143 self.host('example', 200)
144 self.host('www.example', 404)
145 self.host('example.com', 404)
146 self.host('exampl', 404)
147
148 def test_routes_match_method_case_insensitive(self):
149 self.route_match({"method": "get"})
150
165 self.assertEqual(self.get()['status'], 200, 'GET')
151 assert self.get()['status'] == 200, 'GET'
166
167 def test_routes_match_wildcard_left_case_insensitive(self):
168 self.route_match({"method": "*get"})
152
153 def test_routes_match_wildcard_left_case_insensitive(self):
154 self.route_match({"method": "*get"})
169 self.assertEqual(self.get()['status'], 200, 'GET')
155 assert self.get()['status'] == 200, 'GET'
170
171 self.route_match({"method": "*et"})
156
157 self.route_match({"method": "*et"})
172 self.assertEqual(self.get()['status'], 200, 'GET')
158 assert self.get()['status'] == 200, 'GET'
173
174 def test_routes_match_wildcard_middle_case_insensitive(self):
175 self.route_match({"method": "g*t"})
176
159
160 def test_routes_match_wildcard_middle_case_insensitive(self):
161 self.route_match({"method": "g*t"})
162
177 self.assertEqual(self.get()['status'], 200, 'GET')
163 assert self.get()['status'] == 200, 'GET'
178
179 def test_routes_match_wildcard_right_case_insensitive(self):
180 self.route_match({"method": "get*"})
164
165 def test_routes_match_wildcard_right_case_insensitive(self):
166 self.route_match({"method": "get*"})
181 self.assertEqual(self.get()['status'], 200, 'GET')
167 assert self.get()['status'] == 200, 'GET'
182
183 self.route_match({"method": "ge*"})
168
169 self.route_match({"method": "ge*"})
184 self.assertEqual(self.get()['status'], 200, 'GET')
170 assert self.get()['status'] == 200, 'GET'
185
186 def test_routes_match_wildcard_substring_case_insensitive(self):
187 self.route_match({"method": "*et*"})
188
171
172 def test_routes_match_wildcard_substring_case_insensitive(self):
173 self.route_match({"method": "*et*"})
174
189 self.assertEqual(self.get()['status'], 200, 'GET')
175 assert self.get()['status'] == 200, 'GET'
190
191 def test_routes_match_wildcard_left_case_sensitive(self):
192 self.route_match({"uri": "*blah"})
193
176
177 def test_routes_match_wildcard_left_case_sensitive(self):
178 self.route_match({"uri": "*blah"})
179
194 self.assertEqual(self.get(url='/blah')['status'], 200, '/blah')
195 self.assertEqual(self.get(url='/BLAH')['status'], 404, '/BLAH')
180 assert self.get(url='/blah')['status'] == 200, '/blah'
181 assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
196
197 def test_routes_match_wildcard_middle_case_sensitive(self):
198 self.route_match({"uri": "/b*h"})
199
182
183 def test_routes_match_wildcard_middle_case_sensitive(self):
184 self.route_match({"uri": "/b*h"})
185
200 self.assertEqual(self.get(url='/blah')['status'], 200, '/blah')
201 self.assertEqual(self.get(url='/BLAH')['status'], 404, '/BLAH')
186 assert self.get(url='/blah')['status'] == 200, '/blah'
187 assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
202
203 def test_route_match_wildcards_ordered(self):
204 self.route_match({"uri": "/a*x*y*"})
205
188
189 def test_route_match_wildcards_ordered(self):
190 self.route_match({"uri": "/a*x*y*"})
191
206 self.assertEqual(self.get(url='/axy')['status'], 200, '/axy')
207 self.assertEqual(self.get(url='/ayx')['status'], 404, '/ayx')
192 assert self.get(url='/axy')['status'] == 200, '/axy'
193 assert self.get(url='/ayx')['status'] == 404, '/ayx'
208
209 def test_route_match_wildcards_adjust_start(self):
210 self.route_match({"uri": "/bla*bla*"})
211
194
195 def test_route_match_wildcards_adjust_start(self):
196 self.route_match({"uri": "/bla*bla*"})
197
212 self.assertEqual(self.get(url='/bla_foo')['status'], 404, '/bla_foo')
198 assert self.get(url='/bla_foo')['status'] == 404, '/bla_foo'
213
214 def test_route_match_wildcards_adjust_start_substr(self):
215 self.route_match({"uri": "*bla*bla*"})
216
199
200 def test_route_match_wildcards_adjust_start_substr(self):
201 self.route_match({"uri": "*bla*bla*"})
202
217 self.assertEqual(self.get(url='/bla_foo')['status'], 404, '/bla_foo')
203 assert self.get(url='/bla_foo')['status'] == 404, '/bla_foo'
218
219 def test_route_match_wildcards_adjust_end(self):
220 self.route_match({"uri": "/bla*bla"})
221
204
205 def test_route_match_wildcards_adjust_end(self):
206 self.route_match({"uri": "/bla*bla"})
207
222 self.assertEqual(self.get(url='/foo_bla')['status'], 404, '/foo_bla')
208 assert self.get(url='/foo_bla')['status'] == 404, '/foo_bla'
223
224 def test_routes_match_wildcard_right_case_sensitive(self):
225 self.route_match({"uri": "/bla*"})
226
209
210 def test_routes_match_wildcard_right_case_sensitive(self):
211 self.route_match({"uri": "/bla*"})
212
227 self.assertEqual(self.get(url='/blah')['status'], 200, '/blah')
228 self.assertEqual(self.get(url='/BLAH')['status'], 404, '/BLAH')
213 assert self.get(url='/blah')['status'] == 200, '/blah'
214 assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
229
230 def test_routes_match_wildcard_substring_case_sensitive(self):
231 self.route_match({"uri": "*bla*"})
232
215
216 def test_routes_match_wildcard_substring_case_sensitive(self):
217 self.route_match({"uri": "*bla*"})
218
233 self.assertEqual(self.get(url='/blah')['status'], 200, '/blah')
234 self.assertEqual(self.get(url='/BLAH')['status'], 404, '/BLAH')
219 assert self.get(url='/blah')['status'] == 200, '/blah'
220 assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
235
236 def test_routes_match_many_wildcard_substrings_case_sensitive(self):
237 self.route_match({"uri": "*a*B*c*"})
238
221
222 def test_routes_match_many_wildcard_substrings_case_sensitive(self):
223 self.route_match({"uri": "*a*B*c*"})
224
239 self.assertEqual(self.get(url='/blah-a-B-c-blah')['status'], 200)
240 self.assertEqual(self.get(url='/a-B-c')['status'], 200)
241 self.assertEqual(self.get(url='/aBc')['status'], 200)
242 self.assertEqual(self.get(url='/aBCaBbc')['status'], 200)
243 self.assertEqual(self.get(url='/ABc')['status'], 404)
225 assert self.get(url='/blah-a-B-c-blah')['status'] == 200
226 assert self.get(url='/a-B-c')['status'] == 200
227 assert self.get(url='/aBc')['status'] == 200
228 assert self.get(url='/aBCaBbc')['status'] == 200
229 assert self.get(url='/ABc')['status'] == 404
244
245 def test_routes_pass_encode(self):
246 def check_pass(path, name):
230
231 def test_routes_pass_encode(self):
232 def check_pass(path, name):
247 self.assertIn(
248 'success',
249 self.conf(
250 {
251 "listeners": {
252 "*:7080": {"pass": "applications/" + path}
253 },
254 "applications": {
255 name: {
256 "type": "python",
257 "processes": {"spare": 0},
258 "path": self.current_dir + '/python/empty',
259 "working_directory": self.current_dir
260 + '/python/empty',
261 "module": "wsgi",
262 }
263 },
264 }
265 ),
233 assert 'success' in self.conf(
234 {
235 "listeners": {"*:7080": {"pass": "applications/" + path}},
236 "applications": {
237 name: {
238 "type": "python",
239 "processes": {"spare": 0},
240 "path": option.test_dir + '/python/empty',
241 "working_directory": option.test_dir
242 + '/python/empty',
243 "module": "wsgi",
244 }
245 },
246 }
266 )
267
247 )
248
268 self.assertEqual(self.get()['status'], 200)
249 assert self.get()['status'] == 200
269
270 check_pass("%25", "%")
271 check_pass("blah%2Fblah", "blah/blah")
272 check_pass("%2Fblah%2F%2Fblah%2F", "/blah//blah/")
273 check_pass("%20blah%252Fblah%7E", " blah%2Fblah~")
274
275 def check_pass_error(path, name):
250
251 check_pass("%25", "%")
252 check_pass("blah%2Fblah", "blah/blah")
253 check_pass("%2Fblah%2F%2Fblah%2F", "/blah//blah/")
254 check_pass("%20blah%252Fblah%7E", " blah%2Fblah~")
255
256 def check_pass_error(path, name):
276 self.assertIn(
277 'error',
278 self.conf(
279 {
280 "listeners": {
281 "*:7080": {"pass": "applications/" + path}
282 },
283 "applications": {
284 name: {
285 "type": "python",
286 "processes": {"spare": 0},
287 "path": self.current_dir + '/python/empty',
288 "working_directory": self.current_dir
289 + '/python/empty',
290 "module": "wsgi",
291 }
292 },
293 }
294 ),
257 assert 'error' in self.conf(
258 {
259 "listeners": {"*:7080": {"pass": "applications/" + path}},
260 "applications": {
261 name: {
262 "type": "python",
263 "processes": {"spare": 0},
264 "path": option.test_dir + '/python/empty',
265 "working_directory": option.test_dir
266 + '/python/empty',
267 "module": "wsgi",
268 }
269 },
270 }
295 )
296
297 check_pass_error("%", "%")
298 check_pass_error("%1", "%1")
299
300 def test_routes_absent(self):
301 self.conf(
302 {
303 "listeners": {"*:7081": {"pass": "applications/empty"}},
304 "applications": {
305 "empty": {
306 "type": "python",
307 "processes": {"spare": 0},
271 )
272
273 check_pass_error("%", "%")
274 check_pass_error("%1", "%1")
275
276 def test_routes_absent(self):
277 self.conf(
278 {
279 "listeners": {"*:7081": {"pass": "applications/empty"}},
280 "applications": {
281 "empty": {
282 "type": "python",
283 "processes": {"spare": 0},
308 "path": self.current_dir + '/python/empty',
309 "working_directory": self.current_dir
284 "path": option.test_dir + '/python/empty',
285 "working_directory": option.test_dir
310 + '/python/empty',
311 "module": "wsgi",
312 }
313 },
314 }
315 )
316
286 + '/python/empty',
287 "module": "wsgi",
288 }
289 },
290 }
291 )
292
317 self.assertEqual(self.get(port=7081)['status'], 200, 'routes absent')
293 assert self.get(port=7081)['status'] == 200, 'routes absent'
318
319 def test_routes_pass_invalid(self):
294
295 def test_routes_pass_invalid(self):
320 self.assertIn(
321 'error',
322 self.conf({"pass": "routes/blah"}, 'listeners/*:7080'),
323 'routes invalid',
324 )
296 assert 'error' in self.conf(
297 {"pass": "routes/blah"}, 'listeners/*:7080'
298 ), 'routes invalid'
325
326 def test_route_empty(self):
299
300 def test_route_empty(self):
327 self.assertIn(
328 'success',
329 self.conf(
330 {
331 "listeners": {"*:7080": {"pass": "routes/main"}},
332 "routes": {"main": []},
333 "applications": {},
334 }
335 ),
336 'route empty configure',
337 )
301 assert 'success' in self.conf(
302 {
303 "listeners": {"*:7080": {"pass": "routes/main"}},
304 "routes": {"main": []},
305 "applications": {},
306 }
307 ), 'route empty configure'
338
308
339 self.assertEqual(self.get()['status'], 404, 'route empty')
309 assert self.get()['status'] == 404, 'route empty'
340
341 def test_routes_route_empty(self):
310
311 def test_routes_route_empty(self):
342 self.assertIn(
343 'success',
344 self.conf({}, 'listeners'),
345 'routes empty listeners configure',
346 )
312 assert 'success' in self.conf(
313 {}, 'listeners'
314 ), 'routes empty listeners configure'
347
315
348 self.assertIn(
349 'success', self.conf({}, 'routes'), 'routes empty configure'
350 )
316 assert 'success' in self.conf({}, 'routes'), 'routes empty configure'
351
352 def test_routes_route_match_absent(self):
317
318 def test_routes_route_match_absent(self):
353 self.assertIn(
354 'success',
355 self.conf([{"action": {"return": 200}}], 'routes'),
356 'route match absent configure',
357 )
319 assert 'success' in self.conf(
320 [{"action": {"return": 200}}], 'routes'
321 ), 'route match absent configure'
358
322
359 self.assertEqual(self.get()['status'], 200, 'route match absent')
323 assert self.get()['status'] == 200, 'route match absent'
360
361 def test_routes_route_action_absent(self):
324
325 def test_routes_route_action_absent(self):
362 self.skip_alerts.append(r'failed to apply new conf')
326 skip_alert(r'failed to apply new conf')
363
327
364 self.assertIn(
365 'error',
366 self.conf([{"match": {"method": "GET"}}], 'routes'),
367 'route pass absent configure',
368 )
328 assert 'error' in self.conf(
329 [{"match": {"method": "GET"}}], 'routes'
330 ), 'route pass absent configure'
369
370 def test_routes_route_pass_absent(self):
331
332 def test_routes_route_pass_absent(self):
371 self.assertIn(
372 'error',
373 self.conf([{"match": {"method": "GET"}, "action": {}}], 'routes'),
374 'route pass absent configure',
375 )
333 assert 'error' in self.conf(
334 [{"match": {"method": "GET"}, "action": {}}], 'routes'
335 ), 'route pass absent configure'
376
377 def test_routes_action_unique(self):
336
337 def test_routes_action_unique(self):
378 self.assertIn(
379 'success',
380 self.conf(
381 {
382 "listeners": {
383 "*:7080": {"pass": "routes"},
384 "*:7081": {"pass": "applications/app"},
385 },
386 "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
387 "applications": {
388 "app": {
389 "type": "python",
390 "processes": {"spare": 0},
391 "path": "/app",
392 "module": "wsgi",
393 }
394 },
395 }
396 ),
397 )
398
399 self.assertIn(
400 'error',
401 self.conf(
402 {"proxy": "http://127.0.0.1:7081", "share": self.testdir},
403 'routes/0/action',
404 ),
405 'proxy share',
406 )
407 self.assertIn(
408 'error',
409 self.conf(
410 {
411 "proxy": "http://127.0.0.1:7081",
412 "pass": "applications/app",
338 assert 'success' in self.conf(
339 {
340 "listeners": {
341 "*:7080": {"pass": "routes"},
342 "*:7081": {"pass": "applications/app"},
413 },
343 },
414 'routes/0/action',
415 ),
416 'proxy pass',
344 "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
345 "applications": {
346 "app": {
347 "type": "python",
348 "processes": {"spare": 0},
349 "path": "/app",
350 "module": "wsgi",
351 }
352 },
353 }
417 )
354 )
418 self.assertIn(
419 'error',
420 self.conf(
421 {"share": self.testdir, "pass": "applications/app"},
422 'routes/0/action',
423 ),
424 'share pass',
425 )
426
355
356 assert 'error' in self.conf(
357 {"proxy": "http://127.0.0.1:7081", "share": self.temp_dir},
358 'routes/0/action',
359 ), 'proxy share'
360 assert 'error' in self.conf(
361 {"proxy": "http://127.0.0.1:7081", "pass": "applications/app",},
362 'routes/0/action',
363 ), 'proxy pass'
364 assert 'error' in self.conf(
365 {"share": self.temp_dir, "pass": "applications/app"},
366 'routes/0/action',
367 ), 'share pass'
368
427 def test_routes_rules_two(self):
369 def test_routes_rules_two(self):
428 self.assertIn(
429 'success',
430 self.conf(
431 [
432 {"match": {"method": "GET"}, "action": {"return": 200}},
433 {"match": {"method": "POST"}, "action": {"return": 201}},
434 ],
435 'routes',
436 ),
437 'rules two configure',
438 )
370 assert 'success' in self.conf(
371 [
372 {"match": {"method": "GET"}, "action": {"return": 200}},
373 {"match": {"method": "POST"}, "action": {"return": 201}},
374 ],
375 'routes',
376 ), 'rules two configure'
439
377
440 self.assertEqual(self.get()['status'], 200, 'rules two match first')
441 self.assertEqual(self.post()['status'], 201, 'rules two match second')
378 assert self.get()['status'] == 200, 'rules two match first'
379 assert self.post()['status'] == 201, 'rules two match second'
442
443 def test_routes_two(self):
380
381 def test_routes_two(self):
444 self.assertIn(
445 'success',
446 self.conf(
447 {
448 "listeners": {"*:7080": {"pass": "routes/first"}},
449 "routes": {
450 "first": [
451 {
452 "match": {"method": "GET"},
453 "action": {"pass": "routes/second"},
454 }
455 ],
456 "second": [
457 {
458 "match": {"host": "localhost"},
459 "action": {"return": 200},
460 }
461 ],
462 },
463 "applications": {},
464 }
465 ),
466 'routes two configure',
467 )
382 assert 'success' in self.conf(
383 {
384 "listeners": {"*:7080": {"pass": "routes/first"}},
385 "routes": {
386 "first": [
387 {
388 "match": {"method": "GET"},
389 "action": {"pass": "routes/second"},
390 }
391 ],
392 "second": [
393 {
394 "match": {"host": "localhost"},
395 "action": {"return": 200},
396 }
397 ],
398 },
399 "applications": {},
400 }
401 ), 'routes two configure'
468
402
469 self.assertEqual(self.get()['status'], 200, 'routes two')
403 assert self.get()['status'] == 200, 'routes two'
470
471 def test_routes_match_host_positive(self):
472 self.route_match({"host": "localhost"})
473
404
405 def test_routes_match_host_positive(self):
406 self.route_match({"host": "localhost"})
407
474 self.assertEqual(self.get()['status'], 200, 'localhost')
408 assert self.get()['status'] == 200, 'localhost'
475 self.host('localhost.', 200)
476 self.host('localhost.', 200)
477 self.host('.localhost', 404)
478 self.host('www.localhost', 404)
479 self.host('localhost1', 404)
480
409 self.host('localhost.', 200)
410 self.host('localhost.', 200)
411 self.host('.localhost', 404)
412 self.host('www.localhost', 404)
413 self.host('localhost1', 404)
414
481 @unittest.skip('not yet')
415 @pytest.mark.skip('not yet')
482 def test_routes_match_host_absent(self):
483 self.route_match({"host": "localhost"})
484
416 def test_routes_match_host_absent(self):
417 self.route_match({"host": "localhost"})
418
485 self.assertEqual(
486 self.get(headers={'Connection': 'close'})['status'],
487 400,
488 'match host absent',
489 )
419 assert (
420 self.get(headers={'Connection': 'close'})['status'] == 400
421 ), 'match host absent'
490
491 def test_routes_match_host_ipv4(self):
492 self.route_match({"host": "127.0.0.1"})
493
494 self.host('127.0.0.1', 200)
495 self.host('127.0.0.1:7080', 200)
496
497 def test_routes_match_host_ipv6(self):
498 self.route_match({"host": "[::1]"})
499
500 self.host('[::1]', 200)
501 self.host('[::1]:7080', 200)
502
503 def test_routes_match_host_positive_many(self):
504 self.route_match({"host": ["localhost", "example.com"]})
505
422
423 def test_routes_match_host_ipv4(self):
424 self.route_match({"host": "127.0.0.1"})
425
426 self.host('127.0.0.1', 200)
427 self.host('127.0.0.1:7080', 200)
428
429 def test_routes_match_host_ipv6(self):
430 self.route_match({"host": "[::1]"})
431
432 self.host('[::1]', 200)
433 self.host('[::1]:7080', 200)
434
435 def test_routes_match_host_positive_many(self):
436 self.route_match({"host": ["localhost", "example.com"]})
437
506 self.assertEqual(self.get()['status'], 200, 'localhost')
438 assert self.get()['status'] == 200, 'localhost'
507 self.host('example.com', 200)
508
509 def test_routes_match_host_positive_and_negative(self):
510 self.route_match({"host": ["*example.com", "!www.example.com"]})
511
439 self.host('example.com', 200)
440
441 def test_routes_match_host_positive_and_negative(self):
442 self.route_match({"host": ["*example.com", "!www.example.com"]})
443
512 self.assertEqual(self.get()['status'], 404, 'localhost')
444 assert self.get()['status'] == 404, 'localhost'
513 self.host('example.com', 200)
514 self.host('www.example.com', 404)
515 self.host('!www.example.com', 200)
516
517 def test_routes_match_host_positive_and_negative_wildcard(self):
518 self.route_match({"host": ["*example*", "!www.example*"]})
519
520 self.host('example.com', 200)

--- 9 unchanged lines hidden (view full) ---

530 self.route_match({"host": "example.com"})
531
532 self.host('example.com:7080', 200)
533
534 def test_routes_match_host_empty(self):
535 self.route_match({"host": ""})
536
537 self.host('', 200)
445 self.host('example.com', 200)
446 self.host('www.example.com', 404)
447 self.host('!www.example.com', 200)
448
449 def test_routes_match_host_positive_and_negative_wildcard(self):
450 self.route_match({"host": ["*example*", "!www.example*"]})
451
452 self.host('example.com', 200)

--- 9 unchanged lines hidden (view full) ---

462 self.route_match({"host": "example.com"})
463
464 self.host('example.com:7080', 200)
465
466 def test_routes_match_host_empty(self):
467 self.route_match({"host": ""})
468
469 self.host('', 200)
538 self.assertEqual(
539 self.get(http_10=True, headers={})['status'],
540 200,
541 'match host empty 2',
542 )
543 self.assertEqual(self.get()['status'], 404, 'match host empty 3')
470 assert (
471 self.get(http_10=True, headers={})['status'] == 200
472 ), 'match host empty 2'
473 assert self.get()['status'] == 404, 'match host empty 3'
544
545 def test_routes_match_uri_positive(self):
546 self.route_match({"uri": ["/blah", "/slash/"]})
547
474
475 def test_routes_match_uri_positive(self):
476 self.route_match({"uri": ["/blah", "/slash/"]})
477
548 self.assertEqual(self.get()['status'], 404, '/')
549 self.assertEqual(self.get(url='/blah')['status'], 200, '/blah')
550 self.assertEqual(self.get(url='/blah#foo')['status'], 200, '/blah#foo')
551 self.assertEqual(self.get(url='/blah?var')['status'], 200, '/blah?var')
552 self.assertEqual(self.get(url='//blah')['status'], 200, '//blah')
553 self.assertEqual(
554 self.get(url='/slash/foo/../')['status'], 200, 'relative'
555 )
556 self.assertEqual(self.get(url='/slash/./')['status'], 200, '/slash/./')
557 self.assertEqual(
558 self.get(url='/slash//.//')['status'], 200, 'adjacent slashes'
559 )
560 self.assertEqual(self.get(url='/%')['status'], 400, 'percent')
561 self.assertEqual(self.get(url='/%1')['status'], 400, 'percent digit')
562 self.assertEqual(self.get(url='/%A')['status'], 400, 'percent letter')
563 self.assertEqual(
564 self.get(url='/slash/.?args')['status'], 200, 'dot args'
565 )
566 self.assertEqual(
567 self.get(url='/slash/.#frag')['status'], 200, 'dot frag'
568 )
569 self.assertEqual(
570 self.get(url='/slash/foo/..?args')['status'],
571 200,
572 'dot dot args',
573 )
574 self.assertEqual(
575 self.get(url='/slash/foo/..#frag')['status'],
576 200,
577 'dot dot frag',
578 )
579 self.assertEqual(
580 self.get(url='/slash/.')['status'], 200, 'trailing dot'
581 )
582 self.assertEqual(
583 self.get(url='/slash/foo/..')['status'],
584 200,
585 'trailing dot dot',
586 )
478 assert self.get()['status'] == 404, '/'
479 assert self.get(url='/blah')['status'] == 200, '/blah'
480 assert self.get(url='/blah#foo')['status'] == 200, '/blah#foo'
481 assert self.get(url='/blah?var')['status'] == 200, '/blah?var'
482 assert self.get(url='//blah')['status'] == 200, '//blah'
483 assert self.get(url='/slash/foo/../')['status'] == 200, 'relative'
484 assert self.get(url='/slash/./')['status'] == 200, '/slash/./'
485 assert self.get(url='/slash//.//')['status'] == 200, 'adjacent slashes'
486 assert self.get(url='/%')['status'] == 400, 'percent'
487 assert self.get(url='/%1')['status'] == 400, 'percent digit'
488 assert self.get(url='/%A')['status'] == 400, 'percent letter'
489 assert self.get(url='/slash/.?args')['status'] == 200, 'dot args'
490 assert self.get(url='/slash/.#frag')['status'] == 200, 'dot frag'
491 assert (
492 self.get(url='/slash/foo/..?args')['status'] == 200
493 ), 'dot dot args'
494 assert (
495 self.get(url='/slash/foo/..#frag')['status'] == 200
496 ), 'dot dot frag'
497 assert self.get(url='/slash/.')['status'] == 200, 'trailing dot'
498 assert (
499 self.get(url='/slash/foo/..')['status'] == 200
500 ), 'trailing dot dot'
587
588 def test_routes_match_uri_case_sensitive(self):
589 self.route_match({"uri": "/BLAH"})
590
501
502 def test_routes_match_uri_case_sensitive(self):
503 self.route_match({"uri": "/BLAH"})
504
591 self.assertEqual(self.get(url='/blah')['status'], 404, '/blah')
592 self.assertEqual(self.get(url='/BlaH')['status'], 404, '/BlaH')
593 self.assertEqual(self.get(url='/BLAH')['status'], 200, '/BLAH')
505 assert self.get(url='/blah')['status'] == 404, '/blah'
506 assert self.get(url='/BlaH')['status'] == 404, '/BlaH'
507 assert self.get(url='/BLAH')['status'] == 200, '/BLAH'
594
595 def test_routes_match_uri_normalize(self):
596 self.route_match({"uri": "/blah"})
597
508
509 def test_routes_match_uri_normalize(self):
510 self.route_match({"uri": "/blah"})
511
598 self.assertEqual(
599 self.get(url='/%62%6c%61%68')['status'], 200, 'normalize'
600 )
512 assert self.get(url='/%62%6c%61%68')['status'] == 200, 'normalize'
601
602 def test_routes_match_empty_array(self):
603 self.route_match({"uri": []})
604
513
514 def test_routes_match_empty_array(self):
515 self.route_match({"uri": []})
516
605 self.assertEqual(self.get(url='/blah')['status'], 200, 'empty array')
517 assert self.get(url='/blah')['status'] == 200, 'empty array'
606
607 def test_routes_reconfigure(self):
518
519 def test_routes_reconfigure(self):
608 self.assertIn('success', self.conf([], 'routes'), 'redefine')
609 self.assertEqual(self.get()['status'], 404, 'redefine request')
520 assert 'success' in self.conf([], 'routes'), 'redefine'
521 assert self.get()['status'] == 404, 'redefine request'
610
522
611 self.assertIn(
612 'success',
613 self.conf([{"action": {"return": 200}}], 'routes'),
614 'redefine 2',
615 )
616 self.assertEqual(self.get()['status'], 200, 'redefine request 2')
523 assert 'success' in self.conf(
524 [{"action": {"return": 200}}], 'routes'
525 ), 'redefine 2'
526 assert self.get()['status'] == 200, 'redefine request 2'
617
527
618 self.assertIn('success', self.conf([], 'routes'), 'redefine 3')
619 self.assertEqual(self.get()['status'], 404, 'redefine request 3')
528 assert 'success' in self.conf([], 'routes'), 'redefine 3'
529 assert self.get()['status'] == 404, 'redefine request 3'
620
530
621 self.assertIn(
622 'success',
623 self.conf(
624 {
625 "listeners": {"*:7080": {"pass": "routes/main"}},
626 "routes": {"main": [{"action": {"return": 200}}]},
627 "applications": {},
628 }
629 ),
630 'redefine 4',
631 )
632 self.assertEqual(self.get()['status'], 200, 'redefine request 4')
531 assert 'success' in self.conf(
532 {
533 "listeners": {"*:7080": {"pass": "routes/main"}},
534 "routes": {"main": [{"action": {"return": 200}}]},
535 "applications": {},
536 }
537 ), 'redefine 4'
538 assert self.get()['status'] == 200, 'redefine request 4'
633
539
634 self.assertIn(
635 'success', self.conf_delete('routes/main/0'), 'redefine 5'
636 )
637 self.assertEqual(self.get()['status'], 404, 'redefine request 5')
540 assert 'success' in self.conf_delete('routes/main/0'), 'redefine 5'
541 assert self.get()['status'] == 404, 'redefine request 5'
638
542
639 self.assertIn(
640 'success',
641 self.conf_post({"action": {"return": 200}}, 'routes/main'),
642 'redefine 6',
643 )
644 self.assertEqual(self.get()['status'], 200, 'redefine request 6')
543 assert 'success' in self.conf_post(
544 {"action": {"return": 200}}, 'routes/main'
545 ), 'redefine 6'
546 assert self.get()['status'] == 200, 'redefine request 6'
645
547
646 self.assertIn(
647 'error',
648 self.conf({"action": {"return": 200}}, 'routes/main/2'),
649 'redefine 7',
650 )
651 self.assertIn(
652 'success',
653 self.conf({"action": {"return": 201}}, 'routes/main/1'),
654 'redefine 8',
655 )
548 assert 'error' in self.conf(
549 {"action": {"return": 200}}, 'routes/main/2'
550 ), 'redefine 7'
551 assert 'success' in self.conf(
552 {"action": {"return": 201}}, 'routes/main/1'
553 ), 'redefine 8'
656
554
657 self.assertEqual(
658 len(self.conf_get('routes/main')), 2, 'redefine conf 8'
659 )
660 self.assertEqual(self.get()['status'], 200, 'redefine request 8')
555 assert len(self.conf_get('routes/main')) == 2, 'redefine conf 8'
556 assert self.get()['status'] == 200, 'redefine request 8'
661
662 def test_routes_edit(self):
663 self.route_match({"method": "GET"})
664
557
558 def test_routes_edit(self):
559 self.route_match({"method": "GET"})
560
665 self.assertEqual(self.get()['status'], 200, 'routes edit GET')
666 self.assertEqual(self.post()['status'], 404, 'routes edit POST')
561 assert self.get()['status'] == 200, 'routes edit GET'
562 assert self.post()['status'] == 404, 'routes edit POST'
667
563
668 self.assertIn(
669 'success',
670 self.conf_post(
671 {"match": {"method": "POST"}, "action": {"return": 200}},
672 'routes',
673 ),
674 'routes edit configure 2',
675 )
676 self.assertEqual(
677 'GET',
678 self.conf_get('routes/0/match/method'),
679 'routes edit configure 2 check',
680 )
681 self.assertEqual(
682 'POST',
683 self.conf_get('routes/1/match/method'),
684 'routes edit configure 2 check 2',
685 )
564 assert 'success' in self.conf_post(
565 {"match": {"method": "POST"}, "action": {"return": 200}}, 'routes',
566 ), 'routes edit configure 2'
567 assert 'GET' == self.conf_get(
568 'routes/0/match/method'
569 ), 'routes edit configure 2 check'
570 assert 'POST' == self.conf_get(
571 'routes/1/match/method'
572 ), 'routes edit configure 2 check 2'
686
573
687 self.assertEqual(self.get()['status'], 200, 'routes edit GET 2')
688 self.assertEqual(self.post()['status'], 200, 'routes edit POST 2')
574 assert self.get()['status'] == 200, 'routes edit GET 2'
575 assert self.post()['status'] == 200, 'routes edit POST 2'
689
576
690 self.assertIn(
691 'success', self.conf_delete('routes/0'), 'routes edit configure 3',
692 )
577 assert 'success' in self.conf_delete(
578 'routes/0'
579 ), 'routes edit configure 3'
693
580
694 self.assertEqual(self.get()['status'], 404, 'routes edit GET 3')
695 self.assertEqual(self.post()['status'], 200, 'routes edit POST 3')
581 assert self.get()['status'] == 404, 'routes edit GET 3'
582 assert self.post()['status'] == 200, 'routes edit POST 3'
696
583
697 self.assertIn(
698 'error',
699 self.conf_delete('routes/1'),
700 'routes edit configure invalid',
701 )
702 self.assertIn(
703 'error',
704 self.conf_delete('routes/-1'),
705 'routes edit configure invalid 2',
706 )
707 self.assertIn(
708 'error',
709 self.conf_delete('routes/blah'),
710 'routes edit configure invalid 3',
711 )
584 assert 'error' in self.conf_delete(
585 'routes/1'
586 ), 'routes edit configure invalid'
587 assert 'error' in self.conf_delete(
588 'routes/-1'
589 ), 'routes edit configure invalid 2'
590 assert 'error' in self.conf_delete(
591 'routes/blah'
592 ), 'routes edit configure invalid 3'
712
593
713 self.assertEqual(self.get()['status'], 404, 'routes edit GET 4')
714 self.assertEqual(self.post()['status'], 200, 'routes edit POST 4')
594 assert self.get()['status'] == 404, 'routes edit GET 4'
595 assert self.post()['status'] == 200, 'routes edit POST 4'
715
596
716 self.assertIn(
717 'success', self.conf_delete('routes/0'), 'routes edit configure 5',
718 )
597 assert 'success' in self.conf_delete(
598 'routes/0'
599 ), 'routes edit configure 5'
719
600
720 self.assertEqual(self.get()['status'], 404, 'routes edit GET 5')
721 self.assertEqual(self.post()['status'], 404, 'routes edit POST 5')
601 assert self.get()['status'] == 404, 'routes edit GET 5'
602 assert self.post()['status'] == 404, 'routes edit POST 5'
722
603
723 self.assertIn(
724 'success',
725 self.conf_post(
726 {"match": {"method": "POST"}, "action": {"return": 200}},
727 'routes',
728 ),
729 'routes edit configure 6',
730 )
604 assert 'success' in self.conf_post(
605 {"match": {"method": "POST"}, "action": {"return": 200}}, 'routes',
606 ), 'routes edit configure 6'
731
607
732 self.assertEqual(self.get()['status'], 404, 'routes edit GET 6')
733 self.assertEqual(self.post()['status'], 200, 'routes edit POST 6')
608 assert self.get()['status'] == 404, 'routes edit GET 6'
609 assert self.post()['status'] == 200, 'routes edit POST 6'
734
610
735 self.assertIn(
736 'success',
737 self.conf(
738 {
739 "listeners": {"*:7080": {"pass": "routes/main"}},
740 "routes": {"main": [{"action": {"return": 200}}]},
741 "applications": {},
742 }
743 ),
744 'route edit configure 7',
745 )
611 assert 'success' in self.conf(
612 {
613 "listeners": {"*:7080": {"pass": "routes/main"}},
614 "routes": {"main": [{"action": {"return": 200}}]},
615 "applications": {},
616 }
617 ), 'route edit configure 7'
746
618
747 self.assertIn(
748 'error',
749 self.conf_delete('routes/0'),
750 'routes edit configure invalid 4',
751 )
752 self.assertIn(
753 'error',
754 self.conf_delete('routes/main'),
755 'routes edit configure invalid 5',
756 )
619 assert 'error' in self.conf_delete(
620 'routes/0'
621 ), 'routes edit configure invalid 4'
622 assert 'error' in self.conf_delete(
623 'routes/main'
624 ), 'routes edit configure invalid 5'
757
625
758 self.assertEqual(self.get()['status'], 200, 'routes edit GET 7')
626 assert self.get()['status'] == 200, 'routes edit GET 7'
759
627
760 self.assertIn(
761 'success',
762 self.conf_delete('listeners/*:7080'),
763 'route edit configure 8',
764 )
765 self.assertIn(
766 'success',
767 self.conf_delete('routes/main'),
768 'route edit configure 9',
769 )
628 assert 'success' in self.conf_delete(
629 'listeners/*:7080'
630 ), 'route edit configure 8'
631 assert 'success' in self.conf_delete(
632 'routes/main'
633 ), 'route edit configure 9'
770
771 def test_match_edit(self):
634
635 def test_match_edit(self):
772 self.skip_alerts.append(r'failed to apply new conf')
636 skip_alert(r'failed to apply new conf')
773
774 self.route_match({"method": ["GET", "POST"]})
775
637
638 self.route_match({"method": ["GET", "POST"]})
639
776 self.assertEqual(self.get()['status'], 200, 'match edit GET')
777 self.assertEqual(self.post()['status'], 200, 'match edit POST')
778 self.assertEqual(self.put()['status'], 404, 'match edit PUT')
640 assert self.get()['status'] == 200, 'match edit GET'
641 assert self.post()['status'] == 200, 'match edit POST'
642 assert self.put()['status'] == 404, 'match edit PUT'
779
643
780 self.assertIn(
781 'success',
782 self.conf_post('\"PUT\"', 'routes/0/match/method'),
783 'match edit configure 2',
784 )
785 self.assertListEqual(
786 ['GET', 'POST', 'PUT'],
787 self.conf_get('routes/0/match/method'),
788 'match edit configure 2 check',
789 )
644 assert 'success' in self.conf_post(
645 '\"PUT\"', 'routes/0/match/method'
646 ), 'match edit configure 2'
647 assert ['GET', 'POST', 'PUT'] == self.conf_get(
648 'routes/0/match/method'
649 ), 'match edit configure 2 check'
790
650
791 self.assertEqual(self.get()['status'], 200, 'match edit GET 2')
792 self.assertEqual(self.post()['status'], 200, 'match edit POST 2')
793 self.assertEqual(self.put()['status'], 200, 'match edit PUT 2')
651 assert self.get()['status'] == 200, 'match edit GET 2'
652 assert self.post()['status'] == 200, 'match edit POST 2'
653 assert self.put()['status'] == 200, 'match edit PUT 2'
794
654
795 self.assertIn(
796 'success',
797 self.conf_delete('routes/0/match/method/1'),
798 'match edit configure 3',
799 )
800 self.assertListEqual(
801 ['GET', 'PUT'],
802 self.conf_get('routes/0/match/method'),
803 'match edit configure 3 check',
804 )
655 assert 'success' in self.conf_delete(
656 'routes/0/match/method/1'
657 ), 'match edit configure 3'
658 assert ['GET', 'PUT'] == self.conf_get(
659 'routes/0/match/method'
660 ), 'match edit configure 3 check'
805
661
806 self.assertEqual(self.get()['status'], 200, 'match edit GET 3')
807 self.assertEqual(self.post()['status'], 404, 'match edit POST 3')
808 self.assertEqual(self.put()['status'], 200, 'match edit PUT 3')
662 assert self.get()['status'] == 200, 'match edit GET 3'
663 assert self.post()['status'] == 404, 'match edit POST 3'
664 assert self.put()['status'] == 200, 'match edit PUT 3'
809
665
810 self.assertIn(
811 'success',
812 self.conf_delete('routes/0/match/method/1'),
813 'match edit configure 4',
814 )
815 self.assertListEqual(
816 ['GET'],
817 self.conf_get('routes/0/match/method'),
818 'match edit configure 4 check',
819 )
666 assert 'success' in self.conf_delete(
667 'routes/0/match/method/1'
668 ), 'match edit configure 4'
669 assert ['GET'] == self.conf_get(
670 'routes/0/match/method'
671 ), 'match edit configure 4 check'
820
672
821 self.assertEqual(self.get()['status'], 200, 'match edit GET 4')
822 self.assertEqual(self.post()['status'], 404, 'match edit POST 4')
823 self.assertEqual(self.put()['status'], 404, 'match edit PUT 4')
673 assert self.get()['status'] == 200, 'match edit GET 4'
674 assert self.post()['status'] == 404, 'match edit POST 4'
675 assert self.put()['status'] == 404, 'match edit PUT 4'
824
676
825 self.assertIn(
826 'error',
827 self.conf_delete('routes/0/match/method/1'),
828 'match edit configure invalid',
829 )
830 self.assertIn(
831 'error',
832 self.conf_delete('routes/0/match/method/-1'),
833 'match edit configure invalid 2',
834 )
835 self.assertIn(
836 'error',
837 self.conf_delete('routes/0/match/method/blah'),
838 'match edit configure invalid 3',
839 )
840 self.assertListEqual(
841 ['GET'],
842 self.conf_get('routes/0/match/method'),
843 'match edit configure 5 check',
844 )
677 assert 'error' in self.conf_delete(
678 'routes/0/match/method/1'
679 ), 'match edit configure invalid'
680 assert 'error' in self.conf_delete(
681 'routes/0/match/method/-1'
682 ), 'match edit configure invalid 2'
683 assert 'error' in self.conf_delete(
684 'routes/0/match/method/blah'
685 ), 'match edit configure invalid 3'
686 assert ['GET'] == self.conf_get(
687 'routes/0/match/method'
688 ), 'match edit configure 5 check'
845
689
846 self.assertEqual(self.get()['status'], 200, 'match edit GET 5')
847 self.assertEqual(self.post()['status'], 404, 'match edit POST 5')
848 self.assertEqual(self.put()['status'], 404, 'match edit PUT 5')
690 assert self.get()['status'] == 200, 'match edit GET 5'
691 assert self.post()['status'] == 404, 'match edit POST 5'
692 assert self.put()['status'] == 404, 'match edit PUT 5'
849
693
850 self.assertIn(
851 'success',
852 self.conf_delete('routes/0/match/method/0'),
853 'match edit configure 6',
854 )
855 self.assertListEqual(
856 [],
857 self.conf_get('routes/0/match/method'),
858 'match edit configure 6 check',
859 )
694 assert 'success' in self.conf_delete(
695 'routes/0/match/method/0'
696 ), 'match edit configure 6'
697 assert [] == self.conf_get(
698 'routes/0/match/method'
699 ), 'match edit configure 6 check'
860
700
861 self.assertEqual(self.get()['status'], 200, 'match edit GET 6')
862 self.assertEqual(self.post()['status'], 200, 'match edit POST 6')
863 self.assertEqual(self.put()['status'], 200, 'match edit PUT 6')
701 assert self.get()['status'] == 200, 'match edit GET 6'
702 assert self.post()['status'] == 200, 'match edit POST 6'
703 assert self.put()['status'] == 200, 'match edit PUT 6'
864
704
865 self.assertIn(
866 'success',
867 self.conf('"GET"', 'routes/0/match/method'),
868 'match edit configure 7',
869 )
705 assert 'success' in self.conf(
706 '"GET"', 'routes/0/match/method'
707 ), 'match edit configure 7'
870
708
871 self.assertEqual(self.get()['status'], 200, 'match edit GET 7')
872 self.assertEqual(self.post()['status'], 404, 'match edit POST 7')
873 self.assertEqual(self.put()['status'], 404, 'match edit PUT 7')
709 assert self.get()['status'] == 200, 'match edit GET 7'
710 assert self.post()['status'] == 404, 'match edit POST 7'
711 assert self.put()['status'] == 404, 'match edit PUT 7'
874
712
875 self.assertIn(
876 'error',
877 self.conf_delete('routes/0/match/method/0'),
878 'match edit configure invalid 5',
879 )
880 self.assertIn(
881 'error',
882 self.conf({}, 'routes/0/action'),
883 'match edit configure invalid 6',
884 )
713 assert 'error' in self.conf_delete(
714 'routes/0/match/method/0'
715 ), 'match edit configure invalid 5'
716 assert 'error' in self.conf(
717 {}, 'routes/0/action'
718 ), 'match edit configure invalid 6'
885
719
886 self.assertIn(
887 'success',
888 self.conf({}, 'routes/0/match'),
889 'match edit configure 8',
890 )
720 assert 'success' in self.conf(
721 {}, 'routes/0/match'
722 ), 'match edit configure 8'
891
723
892 self.assertEqual(self.get()['status'], 200, 'match edit GET 8')
724 assert self.get()['status'] == 200, 'match edit GET 8'
893
894 def test_routes_match_rules(self):
895 self.route_match({"method": "GET", "host": "localhost", "uri": "/"})
896
725
726 def test_routes_match_rules(self):
727 self.route_match({"method": "GET", "host": "localhost", "uri": "/"})
728
897 self.assertEqual(self.get()['status'], 200, 'routes match rules')
729 assert self.get()['status'] == 200, 'routes match rules'
898
899 def test_routes_loop(self):
730
731 def test_routes_loop(self):
900 self.assertIn(
901 'success',
902 self.route({"match": {"uri": "/"}, "action": {"pass": "routes"}}),
903 'routes loop configure',
904 )
732 assert 'success' in self.route(
733 {"match": {"uri": "/"}, "action": {"pass": "routes"}}
734 ), 'routes loop configure'
905
735
906 self.assertEqual(self.get()['status'], 500, 'routes loop')
736 assert self.get()['status'] == 500, 'routes loop'
907
908 def test_routes_match_headers(self):
909 self.route_match({"headers": {"host": "localhost"}})
910
737
738 def test_routes_match_headers(self):
739 self.route_match({"headers": {"host": "localhost"}})
740
911 self.assertEqual(self.get()['status'], 200, 'match headers')
741 assert self.get()['status'] == 200, 'match headers'
912 self.host('Localhost', 200)
913 self.host('localhost.com', 404)
914 self.host('llocalhost', 404)
915 self.host('host', 404)
916
917 def test_routes_match_headers_multiple(self):
918 self.route_match({"headers": {"host": "localhost", "x-blah": "test"}})
919
742 self.host('Localhost', 200)
743 self.host('localhost.com', 404)
744 self.host('llocalhost', 404)
745 self.host('host', 404)
746
747 def test_routes_match_headers_multiple(self):
748 self.route_match({"headers": {"host": "localhost", "x-blah": "test"}})
749
920 self.assertEqual(self.get()['status'], 404, 'match headers multiple')
921 self.assertEqual(
750 assert self.get()['status'] == 404, 'match headers multiple'
751 assert (
922 self.get(
923 headers={
924 "Host": "localhost",
925 "X-blah": "test",
926 "Connection": "close",
927 }
752 self.get(
753 headers={
754 "Host": "localhost",
755 "X-blah": "test",
756 "Connection": "close",
757 }
928 )['status'],
929 200,
930 'match headers multiple 2',
931 )
758 )['status']
759 == 200
760 ), 'match headers multiple 2'
932
761
933 self.assertEqual(
762 assert (
934 self.get(
935 headers={
936 "Host": "localhost",
937 "X-blah": "",
938 "Connection": "close",
939 }
763 self.get(
764 headers={
765 "Host": "localhost",
766 "X-blah": "",
767 "Connection": "close",
768 }
940 )['status'],
941 404,
942 'match headers multiple 3',
943 )
769 )['status']
770 == 404
771 ), 'match headers multiple 3'
944
945 def test_routes_match_headers_multiple_values(self):
946 self.route_match({"headers": {"x-blah": "test"}})
947
772
773 def test_routes_match_headers_multiple_values(self):
774 self.route_match({"headers": {"x-blah": "test"}})
775
948 self.assertEqual(
776 assert (
949 self.get(
950 headers={
951 "Host": "localhost",
952 "X-blah": ["test", "test", "test"],
953 "Connection": "close",
954 }
777 self.get(
778 headers={
779 "Host": "localhost",
780 "X-blah": ["test", "test", "test"],
781 "Connection": "close",
782 }
955 )['status'],
956 200,
957 'match headers multiple values',
958 )
959 self.assertEqual(
783 )['status']
784 == 200
785 ), 'match headers multiple values'
786 assert (
960 self.get(
961 headers={
962 "Host": "localhost",
963 "X-blah": ["test", "blah", "test"],
964 "Connection": "close",
965 }
787 self.get(
788 headers={
789 "Host": "localhost",
790 "X-blah": ["test", "blah", "test"],
791 "Connection": "close",
792 }
966 )['status'],
967 404,
968 'match headers multiple values 2',
969 )
970 self.assertEqual(
793 )['status']
794 == 404
795 ), 'match headers multiple values 2'
796 assert (
971 self.get(
972 headers={
973 "Host": "localhost",
974 "X-blah": ["test", "", "test"],
975 "Connection": "close",
976 }
797 self.get(
798 headers={
799 "Host": "localhost",
800 "X-blah": ["test", "", "test"],
801 "Connection": "close",
802 }
977 )['status'],
978 404,
979 'match headers multiple values 3',
980 )
803 )['status']
804 == 404
805 ), 'match headers multiple values 3'
981
982 def test_routes_match_headers_multiple_rules(self):
983 self.route_match({"headers": {"x-blah": ["test", "blah"]}})
984
806
807 def test_routes_match_headers_multiple_rules(self):
808 self.route_match({"headers": {"x-blah": ["test", "blah"]}})
809
985 self.assertEqual(
986 self.get()['status'], 404, 'match headers multiple rules'
987 )
988 self.assertEqual(
810 assert self.get()['status'] == 404, 'match headers multiple rules'
811 assert (
989 self.get(
990 headers={
991 "Host": "localhost",
992 "X-blah": "test",
993 "Connection": "close",
994 }
812 self.get(
813 headers={
814 "Host": "localhost",
815 "X-blah": "test",
816 "Connection": "close",
817 }
995 )['status'],
996 200,
997 'match headers multiple rules 2',
998 )
999 self.assertEqual(
818 )['status']
819 == 200
820 ), 'match headers multiple rules 2'
821 assert (
1000 self.get(
1001 headers={
1002 "Host": "localhost",
1003 "X-blah": "blah",
1004 "Connection": "close",
1005 }
822 self.get(
823 headers={
824 "Host": "localhost",
825 "X-blah": "blah",
826 "Connection": "close",
827 }
1006 )['status'],
1007 200,
1008 'match headers multiple rules 3',
1009 )
1010 self.assertEqual(
828 )['status']
829 == 200
830 ), 'match headers multiple rules 3'
831 assert (
1011 self.get(
1012 headers={
1013 "Host": "localhost",
1014 "X-blah": ["test", "blah", "test"],
1015 "Connection": "close",
1016 }
832 self.get(
833 headers={
834 "Host": "localhost",
835 "X-blah": ["test", "blah", "test"],
836 "Connection": "close",
837 }
1017 )['status'],
1018 200,
1019 'match headers multiple rules 4',
1020 )
838 )['status']
839 == 200
840 ), 'match headers multiple rules 4'
1021
841
1022 self.assertEqual(
842 assert (
1023 self.get(
1024 headers={
1025 "Host": "localhost",
1026 "X-blah": ["blah", ""],
1027 "Connection": "close",
1028 }
843 self.get(
844 headers={
845 "Host": "localhost",
846 "X-blah": ["blah", ""],
847 "Connection": "close",
848 }
1029 )['status'],
1030 404,
1031 'match headers multiple rules 5',
1032 )
849 )['status']
850 == 404
851 ), 'match headers multiple rules 5'
1033
1034 def test_routes_match_headers_case_insensitive(self):
1035 self.route_match({"headers": {"X-BLAH": "TEST"}})
1036
852
853 def test_routes_match_headers_case_insensitive(self):
854 self.route_match({"headers": {"X-BLAH": "TEST"}})
855
1037 self.assertEqual(
856 assert (
1038 self.get(
1039 headers={
1040 "Host": "localhost",
1041 "x-blah": "test",
1042 "Connection": "close",
1043 }
857 self.get(
858 headers={
859 "Host": "localhost",
860 "x-blah": "test",
861 "Connection": "close",
862 }
1044 )['status'],
1045 200,
1046 'match headers case insensitive',
1047 )
863 )['status']
864 == 200
865 ), 'match headers case insensitive'
1048
1049 def test_routes_match_headers_invalid(self):
1050 self.route_match_invalid({"headers": ["blah"]})
1051 self.route_match_invalid({"headers": {"foo": ["bar", {}]}})
1052 self.route_match_invalid({"headers": {"": "blah"}})
1053
1054 def test_routes_match_headers_empty_rule(self):
1055 self.route_match({"headers": {"host": ""}})
1056
866
867 def test_routes_match_headers_invalid(self):
868 self.route_match_invalid({"headers": ["blah"]})
869 self.route_match_invalid({"headers": {"foo": ["bar", {}]}})
870 self.route_match_invalid({"headers": {"": "blah"}})
871
872 def test_routes_match_headers_empty_rule(self):
873 self.route_match({"headers": {"host": ""}})
874
1057 self.assertEqual(self.get()['status'], 404, 'localhost')
875 assert self.get()['status'] == 404, 'localhost'
1058 self.host('', 200)
1059
1060 def test_routes_match_headers_empty(self):
1061 self.route_match({"headers": {}})
876 self.host('', 200)
877
878 def test_routes_match_headers_empty(self):
879 self.route_match({"headers": {}})
1062 self.assertEqual(self.get()['status'], 200, 'empty')
880 assert self.get()['status'] == 200, 'empty'
1063
1064 self.route_match({"headers": []})
881
882 self.route_match({"headers": []})
1065 self.assertEqual(self.get()['status'], 200, 'empty 2')
883 assert self.get()['status'] == 200, 'empty 2'
1066
1067 def test_routes_match_headers_rule_array_empty(self):
1068 self.route_match({"headers": {"blah": []}})
1069
884
885 def test_routes_match_headers_rule_array_empty(self):
886 self.route_match({"headers": {"blah": []}})
887
1070 self.assertEqual(self.get()['status'], 404, 'array empty')
1071 self.assertEqual(
888 assert self.get()['status'] == 404, 'array empty'
889 assert (
1072 self.get(
1073 headers={
1074 "Host": "localhost",
1075 "blah": "foo",
1076 "Connection": "close",
1077 }
890 self.get(
891 headers={
892 "Host": "localhost",
893 "blah": "foo",
894 "Connection": "close",
895 }
1078 )['status'], 200, 'match headers rule array empty 2'
1079 )
896 )['status']
897 == 200
898 ), 'match headers rule array empty 2'
1080
1081 def test_routes_match_headers_array(self):
1082 self.route_match(
1083 {
1084 "headers": [
1085 {"x-header1": "foo*"},
1086 {"x-header2": "bar"},
1087 {"x-header3": ["foo", "bar"]},
1088 {"x-header1": "bar", "x-header4": "foo"},
1089 ]
1090 }
1091 )
1092
899
900 def test_routes_match_headers_array(self):
901 self.route_match(
902 {
903 "headers": [
904 {"x-header1": "foo*"},
905 {"x-header2": "bar"},
906 {"x-header3": ["foo", "bar"]},
907 {"x-header1": "bar", "x-header4": "foo"},
908 ]
909 }
910 )
911
1093 self.assertEqual(self.get()['status'], 404, 'match headers array')
1094 self.assertEqual(
912 assert self.get()['status'] == 404, 'match headers array'
913 assert (
1095 self.get(
1096 headers={
1097 "Host": "localhost",
1098 "x-header1": "foo123",
1099 "Connection": "close",
1100 }
914 self.get(
915 headers={
916 "Host": "localhost",
917 "x-header1": "foo123",
918 "Connection": "close",
919 }
1101 )['status'],
1102 200,
1103 'match headers array 2',
1104 )
1105 self.assertEqual(
920 )['status']
921 == 200
922 ), 'match headers array 2'
923 assert (
1106 self.get(
1107 headers={
1108 "Host": "localhost",
1109 "x-header2": "bar",
1110 "Connection": "close",
1111 }
924 self.get(
925 headers={
926 "Host": "localhost",
927 "x-header2": "bar",
928 "Connection": "close",
929 }
1112 )['status'],
1113 200,
1114 'match headers array 3',
1115 )
1116 self.assertEqual(
930 )['status']
931 == 200
932 ), 'match headers array 3'
933 assert (
1117 self.get(
1118 headers={
1119 "Host": "localhost",
1120 "x-header3": "bar",
1121 "Connection": "close",
1122 }
934 self.get(
935 headers={
936 "Host": "localhost",
937 "x-header3": "bar",
938 "Connection": "close",
939 }
1123 )['status'],
1124 200,
1125 'match headers array 4',
1126 )
1127 self.assertEqual(
940 )['status']
941 == 200
942 ), 'match headers array 4'
943 assert (
1128 self.get(
1129 headers={
1130 "Host": "localhost",
1131 "x-header1": "bar",
1132 "Connection": "close",
1133 }
944 self.get(
945 headers={
946 "Host": "localhost",
947 "x-header1": "bar",
948 "Connection": "close",
949 }
1134 )['status'],
1135 404,
1136 'match headers array 5',
1137 )
1138 self.assertEqual(
950 )['status']
951 == 404
952 ), 'match headers array 5'
953 assert (
1139 self.get(
1140 headers={
1141 "Host": "localhost",
1142 "x-header1": "bar",
1143 "x-header4": "foo",
1144 "Connection": "close",
1145 }
954 self.get(
955 headers={
956 "Host": "localhost",
957 "x-header1": "bar",
958 "x-header4": "foo",
959 "Connection": "close",
960 }
1146 )['status'],
1147 200,
1148 'match headers array 6',
1149 )
961 )['status']
962 == 200
963 ), 'match headers array 6'
1150
964
1151 self.assertIn(
1152 'success',
1153 self.conf_delete('routes/0/match/headers/1'),
1154 'match headers array configure 2',
1155 )
965 assert 'success' in self.conf_delete(
966 'routes/0/match/headers/1'
967 ), 'match headers array configure 2'
1156
968
1157 self.assertEqual(
969 assert (
1158 self.get(
1159 headers={
1160 "Host": "localhost",
1161 "x-header2": "bar",
1162 "Connection": "close",
1163 }
970 self.get(
971 headers={
972 "Host": "localhost",
973 "x-header2": "bar",
974 "Connection": "close",
975 }
1164 )['status'],
1165 404,
1166 'match headers array 7',
1167 )
1168 self.assertEqual(
976 )['status']
977 == 404
978 ), 'match headers array 7'
979 assert (
1169 self.get(
1170 headers={
1171 "Host": "localhost",
1172 "x-header3": "foo",
1173 "Connection": "close",
1174 }
980 self.get(
981 headers={
982 "Host": "localhost",
983 "x-header3": "foo",
984 "Connection": "close",
985 }
1175 )['status'],
1176 200,
1177 'match headers array 8',
1178 )
986 )['status']
987 == 200
988 ), 'match headers array 8'
1179
1180 def test_routes_match_arguments(self):
1181 self.route_match({"arguments": {"foo": "bar"}})
1182
989
990 def test_routes_match_arguments(self):
991 self.route_match({"arguments": {"foo": "bar"}})
992
1183 self.assertEqual(self.get()['status'], 404, 'args')
1184 self.assertEqual(self.get(url='/?foo=bar')['status'], 200, 'args 2')
1185 self.assertEqual(self.get(url='/?foo=bar1')['status'], 404, 'args 3')
1186 self.assertEqual(self.get(url='/?1foo=bar')['status'], 404, 'args 4')
1187 self.assertEqual(self.get(url='/?Foo=bar')['status'], 404, 'case')
1188 self.assertEqual(self.get(url='/?foo=Bar')['status'], 404, 'case 2')
993 assert self.get()['status'] == 404, 'args'
994 assert self.get(url='/?foo=bar')['status'] == 200, 'args 2'
995 assert self.get(url='/?foo=bar1')['status'] == 404, 'args 3'
996 assert self.get(url='/?1foo=bar')['status'] == 404, 'args 4'
997 assert self.get(url='/?Foo=bar')['status'] == 404, 'case'
998 assert self.get(url='/?foo=Bar')['status'] == 404, 'case 2'
1189
1190 def test_routes_match_arguments_chars(self):
1191 chars = (
1192 " !\"%23$%25%26'()*%2B,-./0123456789:;<%3D>?@"
1193 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
1194 )
1195
1196 chars_enc = ""
1197 for h1 in ["2", "3", "4", "5", "6", "7"]:
999
1000 def test_routes_match_arguments_chars(self):
1001 chars = (
1002 " !\"%23$%25%26'()*%2B,-./0123456789:;<%3D>?@"
1003 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
1004 )
1005
1006 chars_enc = ""
1007 for h1 in ["2", "3", "4", "5", "6", "7"]:
1198 for h2 in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
1199 "B", "C", "D", "E", "F",
1008 for h2 in [
1009 "0",
1010 "1",
1011 "2",
1012 "3",
1013 "4",
1014 "5",
1015 "6",
1016 "7",
1017 "8",
1018 "9",
1019 "A",
1020 "B",
1021 "C",
1022 "D",
1023 "E",
1024 "F",
1200 ]:
1201 chars_enc += "%" + h1 + h2
1202 chars_enc = chars_enc[:-3]
1203
1204 def check_args(args, query):
1205 self.route_match({"arguments": args})
1025 ]:
1026 chars_enc += "%" + h1 + h2
1027 chars_enc = chars_enc[:-3]
1028
1029 def check_args(args, query):
1030 self.route_match({"arguments": args})
1206 self.assertEqual(self.get(url='/?' + query)['status'], 200)
1031 assert self.get(url='/?' + query)['status'] == 200
1207
1208 check_args({chars: chars}, chars + '=' + chars)
1209 check_args({chars: chars}, chars + '=' + chars_enc)
1210 check_args({chars: chars}, chars_enc + '=' + chars)
1211 check_args({chars: chars}, chars_enc + '=' + chars_enc)
1212 check_args({chars_enc: chars_enc}, chars + '=' + chars)
1213 check_args({chars_enc: chars_enc}, chars + '=' + chars_enc)
1214 check_args({chars_enc: chars_enc}, chars_enc + '=' + chars)
1215 check_args({chars_enc: chars_enc}, chars_enc + '=' + chars_enc)
1216
1217 def test_routes_match_arguments_empty(self):
1218 self.route_match({"arguments": {}})
1032
1033 check_args({chars: chars}, chars + '=' + chars)
1034 check_args({chars: chars}, chars + '=' + chars_enc)
1035 check_args({chars: chars}, chars_enc + '=' + chars)
1036 check_args({chars: chars}, chars_enc + '=' + chars_enc)
1037 check_args({chars_enc: chars_enc}, chars + '=' + chars)
1038 check_args({chars_enc: chars_enc}, chars + '=' + chars_enc)
1039 check_args({chars_enc: chars_enc}, chars_enc + '=' + chars)
1040 check_args({chars_enc: chars_enc}, chars_enc + '=' + chars_enc)
1041
1042 def test_routes_match_arguments_empty(self):
1043 self.route_match({"arguments": {}})
1219 self.assertEqual(self.get()['status'], 200, 'arguments empty')
1044 assert self.get()['status'] == 200, 'arguments empty'
1220
1221 self.route_match({"arguments": []})
1045
1046 self.route_match({"arguments": []})
1222 self.assertEqual(self.get()['status'], 200, 'arguments empty 2')
1047 assert self.get()['status'] == 200, 'arguments empty 2'
1223
1224 def test_routes_match_arguments_space(self):
1225 self.route_match({"arguments": {"+fo o%20": "%20b+a r"}})
1048
1049 def test_routes_match_arguments_space(self):
1050 self.route_match({"arguments": {"+fo o%20": "%20b+a r"}})
1226 self.assertEqual(self.get(url='/? fo o = b a r&')['status'], 200)
1227 self.assertEqual(self.get(url='/?+fo+o+=+b+a+r&')['status'], 200)
1228 self.assertEqual(
1229 self.get(url='/?%20fo%20o%20=%20b%20a%20r&')['status'], 200
1230 )
1051 assert self.get(url='/? fo o = b a r&')['status'] == 200
1052 assert self.get(url='/?+fo+o+=+b+a+r&')['status'] == 200
1053 assert self.get(url='/?%20fo%20o%20=%20b%20a%20r&')['status'] == 200
1231
1232 self.route_match({"arguments": {"%20foo": " bar"}})
1054
1055 self.route_match({"arguments": {"%20foo": " bar"}})
1233 self.assertEqual(self.get(url='/? foo= bar')['status'], 200)
1234 self.assertEqual(self.get(url='/?+foo=+bar')['status'], 200)
1235 self.assertEqual(self.get(url='/?%20foo=%20bar')['status'], 200)
1236 self.assertEqual(self.get(url='/?+foo= bar')['status'], 200)
1237 self.assertEqual(self.get(url='/?%20foo=+bar')['status'], 200)
1056 assert self.get(url='/? foo= bar')['status'] == 200
1057 assert self.get(url='/?+foo=+bar')['status'] == 200
1058 assert self.get(url='/?%20foo=%20bar')['status'] == 200
1059 assert self.get(url='/?+foo= bar')['status'] == 200
1060 assert self.get(url='/?%20foo=+bar')['status'] == 200
1238
1239 def test_routes_match_arguments_equal(self):
1240 self.route_match({"arguments": {"=": "="}})
1061
1062 def test_routes_match_arguments_equal(self):
1063 self.route_match({"arguments": {"=": "="}})
1241 self.assertEqual(self.get(url='/?%3D=%3D')['status'], 200)
1242 self.assertEqual(self.get(url='/?%3D==')['status'], 200)
1243 self.assertEqual(self.get(url='/?===')['status'], 404)
1244 self.assertEqual(self.get(url='/?%3D%3D%3D')['status'], 404)
1245 self.assertEqual(self.get(url='/?==%3D')['status'], 404)
1064 assert self.get(url='/?%3D=%3D')['status'] == 200
1065 assert self.get(url='/?%3D==')['status'] == 200
1066 assert self.get(url='/?===')['status'] == 404
1067 assert self.get(url='/?%3D%3D%3D')['status'] == 404
1068 assert self.get(url='/?==%3D')['status'] == 404
1246
1247 def test_routes_match_arguments_enc(self):
1248 self.route_match({"arguments": {"Ю": "н"}})
1069
1070 def test_routes_match_arguments_enc(self):
1071 self.route_match({"arguments": {"Ю": "н"}})
1249 self.assertEqual(self.get(url='/?%D0%AE=%D0%BD')['status'], 200)
1250 self.assertEqual(self.get(url='/?%d0%ae=%d0%Bd')['status'], 200)
1072 assert self.get(url='/?%D0%AE=%D0%BD')['status'] == 200
1073 assert self.get(url='/?%d0%ae=%d0%Bd')['status'] == 200
1251
1252 def test_routes_match_arguments_hash(self):
1253 self.route_match({"arguments": {"#": "#"}})
1074
1075 def test_routes_match_arguments_hash(self):
1076 self.route_match({"arguments": {"#": "#"}})
1254 self.assertEqual(self.get(url='/?%23=%23')['status'], 200)
1255 self.assertEqual(self.get(url='/?%23=%23#')['status'], 200)
1256 self.assertEqual(self.get(url='/?#=#')['status'], 404)
1257 self.assertEqual(self.get(url='/?%23=#')['status'], 404)
1077 assert self.get(url='/?%23=%23')['status'] == 200
1078 assert self.get(url='/?%23=%23#')['status'] == 200
1079 assert self.get(url='/?#=#')['status'] == 404
1080 assert self.get(url='/?%23=#')['status'] == 404
1258
1259 def test_routes_match_arguments_wildcard(self):
1260 self.route_match({"arguments": {"foo": "*"}})
1081
1082 def test_routes_match_arguments_wildcard(self):
1083 self.route_match({"arguments": {"foo": "*"}})
1261 self.assertEqual(self.get(url='/?foo')['status'], 200)
1262 self.assertEqual(self.get(url='/?foo=')['status'], 200)
1263 self.assertEqual(self.get(url='/?foo=blah')['status'], 200)
1264 self.assertEqual(self.get(url='/?blah=foo')['status'], 404)
1084 assert self.get(url='/?foo')['status'] == 200
1085 assert self.get(url='/?foo=')['status'] == 200
1086 assert self.get(url='/?foo=blah')['status'] == 200
1087 assert self.get(url='/?blah=foo')['status'] == 404
1265
1266 self.route_match({"arguments": {"foo": "%25*"}})
1088
1089 self.route_match({"arguments": {"foo": "%25*"}})
1267 self.assertEqual(self.get(url='/?foo=%xx')['status'], 200)
1090 assert self.get(url='/?foo=%xx')['status'] == 200
1268
1269 self.route_match({"arguments": {"foo": "%2A*"}})
1091
1092 self.route_match({"arguments": {"foo": "%2A*"}})
1270 self.assertEqual(self.get(url='/?foo=*xx')['status'], 200)
1271 self.assertEqual(self.get(url='/?foo=xx')['status'], 404)
1093 assert self.get(url='/?foo=*xx')['status'] == 200
1094 assert self.get(url='/?foo=xx')['status'] == 404
1272
1273 self.route_match({"arguments": {"foo": "*%2A"}})
1095
1096 self.route_match({"arguments": {"foo": "*%2A"}})
1274 self.assertEqual(self.get(url='/?foo=xx*')['status'], 200)
1275 self.assertEqual(self.get(url='/?foo=xx*x')['status'], 404)
1097 assert self.get(url='/?foo=xx*')['status'] == 200
1098 assert self.get(url='/?foo=xx*x')['status'] == 404
1276
1277 self.route_match({"arguments": {"foo": "1*2"}})
1099
1100 self.route_match({"arguments": {"foo": "1*2"}})
1278 self.assertEqual(self.get(url='/?foo=12')['status'], 200)
1279 self.assertEqual(self.get(url='/?foo=1blah2')['status'], 200)
1280 self.assertEqual(self.get(url='/?foo=1%2A2')['status'], 200)
1281 self.assertEqual(self.get(url='/?foo=x12')['status'], 404)
1101 assert self.get(url='/?foo=12')['status'] == 200
1102 assert self.get(url='/?foo=1blah2')['status'] == 200
1103 assert self.get(url='/?foo=1%2A2')['status'] == 200
1104 assert self.get(url='/?foo=x12')['status'] == 404
1282
1283 self.route_match({"arguments": {"foo": "bar*", "%25": "%25"}})
1105
1106 self.route_match({"arguments": {"foo": "bar*", "%25": "%25"}})
1284 self.assertEqual(self.get(url='/?foo=barxx&%=%')['status'], 200)
1285 self.assertEqual(self.get(url='/?foo=barxx&x%=%')['status'], 404)
1107 assert self.get(url='/?foo=barxx&%=%')['status'] == 200
1108 assert self.get(url='/?foo=barxx&x%=%')['status'] == 404
1286
1287 def test_routes_match_arguments_negative(self):
1288 self.route_match({"arguments": {"foo": "!%25"}})
1109
1110 def test_routes_match_arguments_negative(self):
1111 self.route_match({"arguments": {"foo": "!%25"}})
1289 self.assertEqual(self.get(url='/?foo=blah')['status'], 200)
1290 self.assertEqual(self.get(url='/?foo=%')['status'], 404)
1112 assert self.get(url='/?foo=blah')['status'] == 200
1113 assert self.get(url='/?foo=%')['status'] == 404
1291
1292 self.route_match({"arguments": {"foo": "%21blah"}})
1114
1115 self.route_match({"arguments": {"foo": "%21blah"}})
1293 self.assertEqual(self.get(url='/?foo=%21blah')['status'], 200)
1294 self.assertEqual(self.get(url='/?foo=!blah')['status'], 200)
1295 self.assertEqual(self.get(url='/?foo=bar')['status'], 404)
1116 assert self.get(url='/?foo=%21blah')['status'] == 200
1117 assert self.get(url='/?foo=!blah')['status'] == 200
1118 assert self.get(url='/?foo=bar')['status'] == 404
1296
1297 self.route_match({"arguments": {"foo": "!!%21*a"}})
1119
1120 self.route_match({"arguments": {"foo": "!!%21*a"}})
1298 self.assertEqual(self.get(url='/?foo=blah')['status'], 200)
1299 self.assertEqual(self.get(url='/?foo=!blah')['status'], 200)
1300 self.assertEqual(self.get(url='/?foo=!!a')['status'], 404)
1301 self.assertEqual(self.get(url='/?foo=!!bla')['status'], 404)
1121 assert self.get(url='/?foo=blah')['status'] == 200
1122 assert self.get(url='/?foo=!blah')['status'] == 200
1123 assert self.get(url='/?foo=!!a')['status'] == 404
1124 assert self.get(url='/?foo=!!bla')['status'] == 404
1302
1303 def test_routes_match_arguments_percent(self):
1304 self.route_match({"arguments": {"%25": "%25"}})
1125
1126 def test_routes_match_arguments_percent(self):
1127 self.route_match({"arguments": {"%25": "%25"}})
1305 self.assertEqual(self.get(url='/?%=%')['status'], 200)
1306 self.assertEqual(self.get(url='/?%25=%25')['status'], 200)
1307 self.assertEqual(self.get(url='/?%25=%')['status'], 200)
1128 assert self.get(url='/?%=%')['status'] == 200
1129 assert self.get(url='/?%25=%25')['status'] == 200
1130 assert self.get(url='/?%25=%')['status'] == 200
1308
1309 self.route_match({"arguments": {"%251": "%252"}})
1131
1132 self.route_match({"arguments": {"%251": "%252"}})
1310 self.assertEqual(self.get(url='/?%1=%2')['status'], 200)
1311 self.assertEqual(self.get(url='/?%251=%252')['status'], 200)
1312 self.assertEqual(self.get(url='/?%251=%2')['status'], 200)
1133 assert self.get(url='/?%1=%2')['status'] == 200
1134 assert self.get(url='/?%251=%252')['status'] == 200
1135 assert self.get(url='/?%251=%2')['status'] == 200
1313
1314 self.route_match({"arguments": {"%25%21%251": "%25%24%252"}})
1136
1137 self.route_match({"arguments": {"%25%21%251": "%25%24%252"}})
1315 self.assertEqual(self.get(url='/?%!%1=%$%2')['status'], 200)
1316 self.assertEqual(self.get(url='/?%25!%251=%25$%252')['status'], 200)
1317 self.assertEqual(self.get(url='/?%25!%1=%$%2')['status'], 200)
1138 assert self.get(url='/?%!%1=%$%2')['status'] == 200
1139 assert self.get(url='/?%25!%251=%25$%252')['status'] == 200
1140 assert self.get(url='/?%25!%1=%$%2')['status'] == 200
1318
1319 def test_routes_match_arguments_ampersand(self):
1320 self.route_match({"arguments": {"foo": "&"}})
1141
1142 def test_routes_match_arguments_ampersand(self):
1143 self.route_match({"arguments": {"foo": "&"}})
1321 self.assertEqual(self.get(url='/?foo=%26')['status'], 200)
1322 self.assertEqual(self.get(url='/?foo=%26&')['status'], 200)
1323 self.assertEqual(self.get(url='/?foo=%26%26')['status'], 404)
1324 self.assertEqual(self.get(url='/?foo=&')['status'], 404)
1144 assert self.get(url='/?foo=%26')['status'] == 200
1145 assert self.get(url='/?foo=%26&')['status'] == 200
1146 assert self.get(url='/?foo=%26%26')['status'] == 404
1147 assert self.get(url='/?foo=&')['status'] == 404
1325
1326 self.route_match({"arguments": {"&": ""}})
1148
1149 self.route_match({"arguments": {"&": ""}})
1327 self.assertEqual(self.get(url='/?%26=')['status'], 200)
1328 self.assertEqual(self.get(url='/?%26=&')['status'], 200)
1329 self.assertEqual(self.get(url='/?%26=%26')['status'], 404)
1330 self.assertEqual(self.get(url='/?&=')['status'], 404)
1150 assert self.get(url='/?%26=')['status'] == 200
1151 assert self.get(url='/?%26=&')['status'] == 200
1152 assert self.get(url='/?%26=%26')['status'] == 404
1153 assert self.get(url='/?&=')['status'] == 404
1331
1332 def test_routes_match_arguments_complex(self):
1333 self.route_match({"arguments": {"foo": ""}})
1334
1154
1155 def test_routes_match_arguments_complex(self):
1156 self.route_match({"arguments": {"foo": ""}})
1157
1335 self.assertEqual(self.get(url='/?foo')['status'], 200, 'complex')
1336 self.assertEqual(
1337 self.get(url='/?blah=blah&foo=')['status'], 200, 'complex 2'
1338 )
1339 self.assertEqual(
1340 self.get(url='/?&&&foo&&&')['status'], 200, 'complex 3'
1341 )
1342 self.assertEqual(
1343 self.get(url='/?foo&foo=bar&foo')['status'], 404, 'complex 4'
1344 )
1345 self.assertEqual(
1346 self.get(url='/?foo=&foo')['status'], 200, 'complex 5'
1347 )
1348 self.assertEqual(
1349 self.get(url='/?&=&foo&==&')['status'], 200, 'complex 6'
1350 )
1351 self.assertEqual(
1352 self.get(url='/?&=&bar&==&')['status'], 404, 'complex 7'
1353 )
1158 assert self.get(url='/?foo')['status'] == 200, 'complex'
1159 assert self.get(url='/?blah=blah&foo=')['status'] == 200, 'complex 2'
1160 assert self.get(url='/?&&&foo&&&')['status'] == 200, 'complex 3'
1161 assert self.get(url='/?foo&foo=bar&foo')['status'] == 404, 'complex 4'
1162 assert self.get(url='/?foo=&foo')['status'] == 200, 'complex 5'
1163 assert self.get(url='/?&=&foo&==&')['status'] == 200, 'complex 6'
1164 assert self.get(url='/?&=&bar&==&')['status'] == 404, 'complex 7'
1354
1355 def test_routes_match_arguments_multiple(self):
1356 self.route_match({"arguments": {"foo": "bar", "blah": "test"}})
1357
1165
1166 def test_routes_match_arguments_multiple(self):
1167 self.route_match({"arguments": {"foo": "bar", "blah": "test"}})
1168
1358 self.assertEqual(self.get()['status'], 404, 'multiple')
1359 self.assertEqual(
1360 self.get(url='/?foo=bar&blah=test')['status'], 200, 'multiple 2'
1361 )
1362 self.assertEqual(
1363 self.get(url='/?foo=bar&blah')['status'], 404, 'multiple 3'
1364 )
1365 self.assertEqual(
1366 self.get(url='/?foo=bar&blah=tes')['status'], 404, 'multiple 4'
1367 )
1368 self.assertEqual(
1369 self.get(url='/?foo=b%61r&bl%61h=t%65st')['status'],
1370 200,
1371 'multiple 5',
1372 )
1169 assert self.get()['status'] == 404, 'multiple'
1170 assert (
1171 self.get(url='/?foo=bar&blah=test')['status'] == 200
1172 ), 'multiple 2'
1173 assert self.get(url='/?foo=bar&blah')['status'] == 404, 'multiple 3'
1174 assert (
1175 self.get(url='/?foo=bar&blah=tes')['status'] == 404
1176 ), 'multiple 4'
1177 assert (
1178 self.get(url='/?foo=b%61r&bl%61h=t%65st')['status'] == 200
1179 ), 'multiple 5'
1373
1374 def test_routes_match_arguments_multiple_rules(self):
1375 self.route_match({"arguments": {"foo": ["bar", "blah"]}})
1376
1180
1181 def test_routes_match_arguments_multiple_rules(self):
1182 self.route_match({"arguments": {"foo": ["bar", "blah"]}})
1183
1377 self.assertEqual(self.get()['status'], 404, 'rules')
1378 self.assertEqual(self.get(url='/?foo=bar')['status'], 200, 'rules 2')
1379 self.assertEqual(self.get(url='/?foo=blah')['status'], 200, 'rules 3')
1380 self.assertEqual(
1381 self.get(url='/?foo=blah&foo=bar&foo=blah')['status'],
1382 200,
1383 'rules 4',
1384 )
1385 self.assertEqual(
1386 self.get(url='/?foo=blah&foo=bar&foo=')['status'], 404, 'rules 5'
1387 )
1184 assert self.get()['status'] == 404, 'rules'
1185 assert self.get(url='/?foo=bar')['status'] == 200, 'rules 2'
1186 assert self.get(url='/?foo=blah')['status'] == 200, 'rules 3'
1187 assert (
1188 self.get(url='/?foo=blah&foo=bar&foo=blah')['status'] == 200
1189 ), 'rules 4'
1190 assert (
1191 self.get(url='/?foo=blah&foo=bar&foo=')['status'] == 404
1192 ), 'rules 5'
1388
1389 def test_routes_match_arguments_array(self):
1390 self.route_match(
1391 {
1392 "arguments": [
1393 {"var1": "val1*"},
1394 {"var2": "val2"},
1395 {"var3": ["foo", "bar"]},
1396 {"var1": "bar", "var4": "foo"},
1397 ]
1398 }
1399 )
1400
1193
1194 def test_routes_match_arguments_array(self):
1195 self.route_match(
1196 {
1197 "arguments": [
1198 {"var1": "val1*"},
1199 {"var2": "val2"},
1200 {"var3": ["foo", "bar"]},
1201 {"var1": "bar", "var4": "foo"},
1202 ]
1203 }
1204 )
1205
1401 self.assertEqual(self.get()['status'], 404, 'arr')
1402 self.assertEqual(self.get(url='/?var1=val123')['status'], 200, 'arr 2')
1403 self.assertEqual(self.get(url='/?var2=val2')['status'], 200, 'arr 3')
1404 self.assertEqual(self.get(url='/?var3=bar')['status'], 200, 'arr 4')
1405 self.assertEqual(self.get(url='/?var1=bar')['status'], 404, 'arr 5')
1406 self.assertEqual(
1407 self.get(url='/?var1=bar&var4=foo')['status'], 200, 'arr 6'
1408 )
1206 assert self.get()['status'] == 404, 'arr'
1207 assert self.get(url='/?var1=val123')['status'] == 200, 'arr 2'
1208 assert self.get(url='/?var2=val2')['status'] == 200, 'arr 3'
1209 assert self.get(url='/?var3=bar')['status'] == 200, 'arr 4'
1210 assert self.get(url='/?var1=bar')['status'] == 404, 'arr 5'
1211 assert self.get(url='/?var1=bar&var4=foo')['status'] == 200, 'arr 6'
1409
1212
1410 self.assertIn(
1411 'success',
1412 self.conf_delete('routes/0/match/arguments/1'),
1413 'match arguments array configure 2',
1414 )
1213 assert 'success' in self.conf_delete(
1214 'routes/0/match/arguments/1'
1215 ), 'match arguments array configure 2'
1415
1216
1416 self.assertEqual(self.get(url='/?var2=val2')['status'], 404, 'arr 7')
1417 self.assertEqual(self.get(url='/?var3=foo')['status'], 200, 'arr 8')
1217 assert self.get(url='/?var2=val2')['status'] == 404, 'arr 7'
1218 assert self.get(url='/?var3=foo')['status'] == 200, 'arr 8'
1418
1419 def test_routes_match_arguments_invalid(self):
1420 # TODO remove it after controller fixed
1219
1220 def test_routes_match_arguments_invalid(self):
1221 # TODO remove it after controller fixed
1421 self.skip_alerts.append(r'failed to apply new conf')
1222 skip_alert(r'failed to apply new conf')
1422
1423 self.route_match_invalid({"arguments": ["var"]})
1424 self.route_match_invalid({"arguments": [{"var1": {}}]})
1425 self.route_match_invalid({"arguments": {"": "bar"}})
1426 self.route_match_invalid({"arguments": {"foo": "%"}})
1427 self.route_match_invalid({"arguments": {"foo": "%1G"}})
1428 self.route_match_invalid({"arguments": {"%": "bar"}})
1429 self.route_match_invalid({"arguments": {"foo": "%0"}})
1430 self.route_match_invalid({"arguments": {"foo": "%%1F"}})
1431 self.route_match_invalid({"arguments": {"%%1F": ""}})
1432 self.route_match_invalid({"arguments": {"%7%F": ""}})
1433
1434 def test_routes_match_cookies(self):
1435 self.route_match({"cookies": {"foO": "bar"}})
1436
1223
1224 self.route_match_invalid({"arguments": ["var"]})
1225 self.route_match_invalid({"arguments": [{"var1": {}}]})
1226 self.route_match_invalid({"arguments": {"": "bar"}})
1227 self.route_match_invalid({"arguments": {"foo": "%"}})
1228 self.route_match_invalid({"arguments": {"foo": "%1G"}})
1229 self.route_match_invalid({"arguments": {"%": "bar"}})
1230 self.route_match_invalid({"arguments": {"foo": "%0"}})
1231 self.route_match_invalid({"arguments": {"foo": "%%1F"}})
1232 self.route_match_invalid({"arguments": {"%%1F": ""}})
1233 self.route_match_invalid({"arguments": {"%7%F": ""}})
1234
1235 def test_routes_match_cookies(self):
1236 self.route_match({"cookies": {"foO": "bar"}})
1237
1437 self.assertEqual(self.get()['status'], 404, 'cookie')
1238 assert self.get()['status'] == 404, 'cookie'
1438 self.cookie('foO=bar', 200)
1439 self.cookie('foO=bar;1', 200)
1440 self.cookie(['foO=bar', 'blah=blah'], 200)
1441 self.cookie('foO=bar; blah=blah', 200)
1442 self.cookie('Foo=bar', 404)
1443 self.cookie('foO=Bar', 404)
1444 self.cookie('foO=bar1', 404)
1445 self.cookie('1foO=bar;', 404)
1446
1447 def test_routes_match_cookies_empty(self):
1448 self.route_match({"cookies": {}})
1239 self.cookie('foO=bar', 200)
1240 self.cookie('foO=bar;1', 200)
1241 self.cookie(['foO=bar', 'blah=blah'], 200)
1242 self.cookie('foO=bar; blah=blah', 200)
1243 self.cookie('Foo=bar', 404)
1244 self.cookie('foO=Bar', 404)
1245 self.cookie('foO=bar1', 404)
1246 self.cookie('1foO=bar;', 404)
1247
1248 def test_routes_match_cookies_empty(self):
1249 self.route_match({"cookies": {}})
1449 self.assertEqual(self.get()['status'], 200, 'cookies empty')
1250 assert self.get()['status'] == 200, 'cookies empty'
1450
1451 self.route_match({"cookies": []})
1251
1252 self.route_match({"cookies": []})
1452 self.assertEqual(self.get()['status'], 200, 'cookies empty 2')
1253 assert self.get()['status'] == 200, 'cookies empty 2'
1453
1454 def test_routes_match_cookies_invalid(self):
1455 self.route_match_invalid({"cookies": ["var"]})
1456 self.route_match_invalid({"cookies": [{"foo": {}}]})
1457
1458 def test_routes_match_cookies_multiple(self):
1459 self.route_match({"cookies": {"foo": "bar", "blah": "blah"}})
1460
1254
1255 def test_routes_match_cookies_invalid(self):
1256 self.route_match_invalid({"cookies": ["var"]})
1257 self.route_match_invalid({"cookies": [{"foo": {}}]})
1258
1259 def test_routes_match_cookies_multiple(self):
1260 self.route_match({"cookies": {"foo": "bar", "blah": "blah"}})
1261
1461 self.assertEqual(self.get()['status'], 404, 'multiple')
1262 assert self.get()['status'] == 404, 'multiple'
1462 self.cookie('foo=bar; blah=blah', 200)
1463 self.cookie(['foo=bar', 'blah=blah'], 200)
1464 self.cookie(['foo=bar; blah', 'blah'], 404)
1465 self.cookie(['foo=bar; blah=test', 'blah=blah'], 404)
1466
1467 def test_routes_match_cookies_multiple_values(self):
1468 self.route_match({"cookies": {"blah": "blah"}})
1469
1470 self.cookie(['blah=blah', 'blah=blah', 'blah=blah'], 200)
1471 self.cookie(['blah=blah', 'blah=test', 'blah=blah'], 404)
1472 self.cookie(['blah=blah; blah=', 'blah=blah'], 404)
1473
1474 def test_routes_match_cookies_multiple_rules(self):
1475 self.route_match({"cookies": {"blah": ["test", "blah"]}})
1476
1263 self.cookie('foo=bar; blah=blah', 200)
1264 self.cookie(['foo=bar', 'blah=blah'], 200)
1265 self.cookie(['foo=bar; blah', 'blah'], 404)
1266 self.cookie(['foo=bar; blah=test', 'blah=blah'], 404)
1267
1268 def test_routes_match_cookies_multiple_values(self):
1269 self.route_match({"cookies": {"blah": "blah"}})
1270
1271 self.cookie(['blah=blah', 'blah=blah', 'blah=blah'], 200)
1272 self.cookie(['blah=blah', 'blah=test', 'blah=blah'], 404)
1273 self.cookie(['blah=blah; blah=', 'blah=blah'], 404)
1274
1275 def test_routes_match_cookies_multiple_rules(self):
1276 self.route_match({"cookies": {"blah": ["test", "blah"]}})
1277
1477 self.assertEqual(self.get()['status'], 404, 'multiple rules')
1278 assert self.get()['status'] == 404, 'multiple rules'
1478 self.cookie('blah=test', 200)
1479 self.cookie('blah=blah', 200)
1480 self.cookie(['blah=blah', 'blah=test', 'blah=blah'], 200)
1481 self.cookie(['blah=blah; blah=test', 'blah=blah'], 200)
1279 self.cookie('blah=test', 200)
1280 self.cookie('blah=blah', 200)
1281 self.cookie(['blah=blah', 'blah=test', 'blah=blah'], 200)
1282 self.cookie(['blah=blah; blah=test', 'blah=blah'], 200)
1482 self.cookie(['blah=blah', 'blah'], 200) # invalid cookie
1283 self.cookie(['blah=blah', 'blah'], 200) # invalid cookie
1483
1484 def test_routes_match_cookies_array(self):
1485 self.route_match(
1486 {
1487 "cookies": [
1488 {"var1": "val1*"},
1489 {"var2": "val2"},
1490 {"var3": ["foo", "bar"]},
1491 {"var1": "bar", "var4": "foo"},
1492 ]
1493 }
1494 )
1495
1284
1285 def test_routes_match_cookies_array(self):
1286 self.route_match(
1287 {
1288 "cookies": [
1289 {"var1": "val1*"},
1290 {"var2": "val2"},
1291 {"var3": ["foo", "bar"]},
1292 {"var1": "bar", "var4": "foo"},
1293 ]
1294 }
1295 )
1296
1496 self.assertEqual(self.get()['status'], 404, 'cookies array')
1297 assert self.get()['status'] == 404, 'cookies array'
1497 self.cookie('var1=val123', 200)
1498 self.cookie('var2=val2', 200)
1499 self.cookie(' var2=val2 ', 200)
1500 self.cookie('var3=bar', 200)
1501 self.cookie('var3=bar;', 200)
1502 self.cookie('var1=bar', 404)
1503 self.cookie('var1=bar; var4=foo;', 200)
1504 self.cookie(['var1=bar', 'var4=foo'], 200)
1505
1298 self.cookie('var1=val123', 200)
1299 self.cookie('var2=val2', 200)
1300 self.cookie(' var2=val2 ', 200)
1301 self.cookie('var3=bar', 200)
1302 self.cookie('var3=bar;', 200)
1303 self.cookie('var1=bar', 404)
1304 self.cookie('var1=bar; var4=foo;', 200)
1305 self.cookie(['var1=bar', 'var4=foo'], 200)
1306
1506 self.assertIn(
1507 'success',
1508 self.conf_delete('routes/0/match/cookies/1'),
1509 'match cookies array configure 2',
1510 )
1307 assert 'success' in self.conf_delete(
1308 'routes/0/match/cookies/1'
1309 ), 'match cookies array configure 2'
1511
1512 self.cookie('var2=val2', 404)
1513 self.cookie('var3=foo', 200)
1514
1515 def test_routes_match_scheme(self):
1516 self.route_match({"scheme": "http"})
1517 self.route_match({"scheme": "https"})
1518 self.route_match({"scheme": "HtTp"})

--- 11 unchanged lines hidden (view full) ---

1530 _, sock = self.http(b'', start=True, raw=True, no_recv=True)
1531 port = sock.getsockname()[1]
1532 return (sock, port)
1533
1534 sock, port = sock_port()
1535 sock2, port2 = sock_port()
1536
1537 self.route_match({"source": "127.0.0.1:" + str(port)})
1310
1311 self.cookie('var2=val2', 404)
1312 self.cookie('var3=foo', 200)
1313
1314 def test_routes_match_scheme(self):
1315 self.route_match({"scheme": "http"})
1316 self.route_match({"scheme": "https"})
1317 self.route_match({"scheme": "HtTp"})

--- 11 unchanged lines hidden (view full) ---

1329 _, sock = self.http(b'', start=True, raw=True, no_recv=True)
1330 port = sock.getsockname()[1]
1331 return (sock, port)
1332
1333 sock, port = sock_port()
1334 sock2, port2 = sock_port()
1335
1336 self.route_match({"source": "127.0.0.1:" + str(port)})
1538 self.assertEqual(self.get(sock=sock)['status'], 200, 'exact')
1539 self.assertEqual(self.get(sock=sock2)['status'], 404, 'exact 2')
1337 assert self.get(sock=sock)['status'] == 200, 'exact'
1338 assert self.get(sock=sock2)['status'] == 404, 'exact 2'
1540
1541 sock, port = sock_port()
1542 sock2, port2 = sock_port()
1543
1544 self.route_match({"source": "!127.0.0.1:" + str(port)})
1339
1340 sock, port = sock_port()
1341 sock2, port2 = sock_port()
1342
1343 self.route_match({"source": "!127.0.0.1:" + str(port)})
1545 self.assertEqual(self.get(sock=sock)['status'], 404, 'negative')
1546 self.assertEqual(self.get(sock=sock2)['status'], 200, 'negative 2')
1344 assert self.get(sock=sock)['status'] == 404, 'negative'
1345 assert self.get(sock=sock2)['status'] == 200, 'negative 2'
1547
1548 sock, port = sock_port()
1549 sock2, port2 = sock_port()
1550
1551 self.route_match({"source": ["*:" + str(port), "!127.0.0.1"]})
1346
1347 sock, port = sock_port()
1348 sock2, port2 = sock_port()
1349
1350 self.route_match({"source": ["*:" + str(port), "!127.0.0.1"]})
1552 self.assertEqual(self.get(sock=sock)['status'], 404, 'negative 3')
1553 self.assertEqual(self.get(sock=sock2)['status'], 404, 'negative 4')
1351 assert self.get(sock=sock)['status'] == 404, 'negative 3'
1352 assert self.get(sock=sock2)['status'] == 404, 'negative 4'
1554
1555 sock, port = sock_port()
1556 sock2, port2 = sock_port()
1557
1558 self.route_match(
1559 {"source": "127.0.0.1:" + str(port) + "-" + str(port)}
1560 )
1353
1354 sock, port = sock_port()
1355 sock2, port2 = sock_port()
1356
1357 self.route_match(
1358 {"source": "127.0.0.1:" + str(port) + "-" + str(port)}
1359 )
1561 self.assertEqual(self.get(sock=sock)['status'], 200, 'range single')
1562 self.assertEqual(self.get(sock=sock2)['status'], 404, 'range single 2')
1360 assert self.get(sock=sock)['status'] == 200, 'range single'
1361 assert self.get(sock=sock2)['status'] == 404, 'range single 2'
1563
1564 socks = [
1565 sock_port(),
1566 sock_port(),
1567 sock_port(),
1568 sock_port(),
1569 sock_port(),
1570 ]
1571 socks.sort(key=lambda sock: sock[1])
1572
1573 self.route_match(
1574 {
1575 "source": "127.0.0.1:"
1576 + str(socks[1][1]) # second port number
1577 + "-"
1578 + str(socks[3][1]) # fourth port number
1579 }
1580 )
1362
1363 socks = [
1364 sock_port(),
1365 sock_port(),
1366 sock_port(),
1367 sock_port(),
1368 sock_port(),
1369 ]
1370 socks.sort(key=lambda sock: sock[1])
1371
1372 self.route_match(
1373 {
1374 "source": "127.0.0.1:"
1375 + str(socks[1][1]) # second port number
1376 + "-"
1377 + str(socks[3][1]) # fourth port number
1378 }
1379 )
1581 self.assertEqual(self.get(sock=socks[0][0])['status'], 404, 'range')
1582 self.assertEqual(self.get(sock=socks[1][0])['status'], 200, 'range 2')
1583 self.assertEqual(self.get(sock=socks[2][0])['status'], 200, 'range 3')
1584 self.assertEqual(self.get(sock=socks[3][0])['status'], 200, 'range 4')
1585 self.assertEqual(self.get(sock=socks[4][0])['status'], 404, 'range 5')
1380 assert self.get(sock=socks[0][0])['status'] == 404, 'range'
1381 assert self.get(sock=socks[1][0])['status'] == 200, 'range 2'
1382 assert self.get(sock=socks[2][0])['status'] == 200, 'range 3'
1383 assert self.get(sock=socks[3][0])['status'] == 200, 'range 4'
1384 assert self.get(sock=socks[4][0])['status'] == 404, 'range 5'
1586
1587 socks = [
1588 sock_port(),
1589 sock_port(),
1590 sock_port(),
1591 ]
1592 socks.sort(key=lambda sock: sock[1])
1593
1594 self.route_match(
1595 {
1596 "source": [
1597 "127.0.0.1:" + str(socks[0][1]),
1598 "127.0.0.1:" + str(socks[2][1]),
1599 ]
1600 }
1601 )
1385
1386 socks = [
1387 sock_port(),
1388 sock_port(),
1389 sock_port(),
1390 ]
1391 socks.sort(key=lambda sock: sock[1])
1392
1393 self.route_match(
1394 {
1395 "source": [
1396 "127.0.0.1:" + str(socks[0][1]),
1397 "127.0.0.1:" + str(socks[2][1]),
1398 ]
1399 }
1400 )
1602 self.assertEqual(self.get(sock=socks[0][0])['status'], 200, 'array')
1603 self.assertEqual(self.get(sock=socks[1][0])['status'], 404, 'array 2')
1604 self.assertEqual(self.get(sock=socks[2][0])['status'], 200, 'array 3')
1401 assert self.get(sock=socks[0][0])['status'] == 200, 'array'
1402 assert self.get(sock=socks[1][0])['status'] == 404, 'array 2'
1403 assert self.get(sock=socks[2][0])['status'] == 200, 'array 3'
1605
1606 def test_routes_source_addr(self):
1404
1405 def test_routes_source_addr(self):
1607 self.assertIn(
1608 'success',
1609 self.conf(
1610 {
1611 "*:7080": {"pass": "routes"},
1612 "[::1]:7081": {"pass": "routes"},
1613 },
1614 'listeners',
1615 ),
1616 'source listeners configure',
1617 )
1406 assert 'success' in self.conf(
1407 {"*:7080": {"pass": "routes"}, "[::1]:7081": {"pass": "routes"},},
1408 'listeners',
1409 ), 'source listeners configure'
1618
1619 def get_ipv6():
1620 return self.get(sock_type='ipv6', port=7081)
1621
1622 self.route_match({"source": "127.0.0.1"})
1410
1411 def get_ipv6():
1412 return self.get(sock_type='ipv6', port=7081)
1413
1414 self.route_match({"source": "127.0.0.1"})
1623 self.assertEqual(self.get()['status'], 200, 'exact')
1624 self.assertEqual(get_ipv6()['status'], 404, 'exact ipv6')
1415 assert self.get()['status'] == 200, 'exact'
1416 assert get_ipv6()['status'] == 404, 'exact ipv6'
1625
1626 self.route_match({"source": ["127.0.0.1"]})
1417
1418 self.route_match({"source": ["127.0.0.1"]})
1627 self.assertEqual(self.get()['status'], 200, 'exact 2')
1628 self.assertEqual(get_ipv6()['status'], 404, 'exact 2 ipv6')
1419 assert self.get()['status'] == 200, 'exact 2'
1420 assert get_ipv6()['status'] == 404, 'exact 2 ipv6'
1629
1630 self.route_match({"source": "!127.0.0.1"})
1421
1422 self.route_match({"source": "!127.0.0.1"})
1631 self.assertEqual(self.get()['status'], 404, 'exact neg')
1632 self.assertEqual(get_ipv6()['status'], 200, 'exact neg ipv6')
1423 assert self.get()['status'] == 404, 'exact neg'
1424 assert get_ipv6()['status'] == 200, 'exact neg ipv6'
1633
1634 self.route_match({"source": "127.0.0.2"})
1425
1426 self.route_match({"source": "127.0.0.2"})
1635 self.assertEqual(self.get()['status'], 404, 'exact 3')
1636 self.assertEqual(get_ipv6()['status'], 404, 'exact 3 ipv6')
1427 assert self.get()['status'] == 404, 'exact 3'
1428 assert get_ipv6()['status'] == 404, 'exact 3 ipv6'
1637
1638 self.route_match({"source": "127.0.0.1-127.0.0.1"})
1429
1430 self.route_match({"source": "127.0.0.1-127.0.0.1"})
1639 self.assertEqual(self.get()['status'], 200, 'range single')
1640 self.assertEqual(get_ipv6()['status'], 404, 'range single ipv6')
1431 assert self.get()['status'] == 200, 'range single'
1432 assert get_ipv6()['status'] == 404, 'range single ipv6'
1641
1642 self.route_match({"source": "127.0.0.2-127.0.0.2"})
1433
1434 self.route_match({"source": "127.0.0.2-127.0.0.2"})
1643 self.assertEqual(self.get()['status'], 404, 'range single 2')
1644 self.assertEqual(get_ipv6()['status'], 404, 'range single 2 ipv6')
1435 assert self.get()['status'] == 404, 'range single 2'
1436 assert get_ipv6()['status'] == 404, 'range single 2 ipv6'
1645
1646 self.route_match({"source": "127.0.0.2-127.0.0.3"})
1437
1438 self.route_match({"source": "127.0.0.2-127.0.0.3"})
1647 self.assertEqual(self.get()['status'], 404, 'range')
1648 self.assertEqual(get_ipv6()['status'], 404, 'range ipv6')
1439 assert self.get()['status'] == 404, 'range'
1440 assert get_ipv6()['status'] == 404, 'range ipv6'
1649
1650 self.route_match({"source": "127.0.0.1-127.0.0.2"})
1441
1442 self.route_match({"source": "127.0.0.1-127.0.0.2"})
1651 self.assertEqual(self.get()['status'], 200, 'range 2')
1652 self.assertEqual(get_ipv6()['status'], 404, 'range 2 ipv6')
1443 assert self.get()['status'] == 200, 'range 2'
1444 assert get_ipv6()['status'] == 404, 'range 2 ipv6'
1653
1654 self.route_match({"source": "127.0.0.0-127.0.0.2"})
1445
1446 self.route_match({"source": "127.0.0.0-127.0.0.2"})
1655 self.assertEqual(self.get()['status'], 200, 'range 3')
1656 self.assertEqual(get_ipv6()['status'], 404, 'range 3 ipv6')
1447 assert self.get()['status'] == 200, 'range 3'
1448 assert get_ipv6()['status'] == 404, 'range 3 ipv6'
1657
1658 self.route_match({"source": "127.0.0.0-127.0.0.1"})
1449
1450 self.route_match({"source": "127.0.0.0-127.0.0.1"})
1659 self.assertEqual(self.get()['status'], 200, 'range 4')
1660 self.assertEqual(get_ipv6()['status'], 404, 'range 4 ipv6')
1451 assert self.get()['status'] == 200, 'range 4'
1452 assert get_ipv6()['status'] == 404, 'range 4 ipv6'
1661
1662 self.route_match({"source": "126.0.0.0-127.0.0.0"})
1453
1454 self.route_match({"source": "126.0.0.0-127.0.0.0"})
1663 self.assertEqual(self.get()['status'], 404, 'range 5')
1664 self.assertEqual(get_ipv6()['status'], 404, 'range 5 ipv6')
1455 assert self.get()['status'] == 404, 'range 5'
1456 assert get_ipv6()['status'] == 404, 'range 5 ipv6'
1665
1666 self.route_match({"source": "126.126.126.126-127.0.0.2"})
1457
1458 self.route_match({"source": "126.126.126.126-127.0.0.2"})
1667 self.assertEqual(self.get()['status'], 200, 'range 6')
1668 self.assertEqual(get_ipv6()['status'], 404, 'range 6 ipv6')
1459 assert self.get()['status'] == 200, 'range 6'
1460 assert get_ipv6()['status'] == 404, 'range 6 ipv6'
1669
1670 def test_routes_source_ipv6(self):
1461
1462 def test_routes_source_ipv6(self):
1671 self.assertIn(
1672 'success',
1673 self.conf(
1674 {
1675 "[::1]:7080": {"pass": "routes"},
1676 "127.0.0.1:7081": {"pass": "routes"},
1677 },
1678 'listeners',
1679 ),
1680 'source listeners configure',
1681 )
1463 assert 'success' in self.conf(
1464 {
1465 "[::1]:7080": {"pass": "routes"},
1466 "127.0.0.1:7081": {"pass": "routes"},
1467 },
1468 'listeners',
1469 ), 'source listeners configure'
1682
1683 self.route_match({"source": "::1"})
1470
1471 self.route_match({"source": "::1"})
1684 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, 'exact')
1685 self.assertEqual(self.get(port=7081)['status'], 404, 'exact ipv4')
1472 assert self.get(sock_type='ipv6')['status'] == 200, 'exact'
1473 assert self.get(port=7081)['status'] == 404, 'exact ipv4'
1686
1687 self.route_match({"source": ["::1"]})
1474
1475 self.route_match({"source": ["::1"]})
1688 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, 'exact 2')
1689 self.assertEqual(self.get(port=7081)['status'], 404, 'exact 2 ipv4')
1476 assert self.get(sock_type='ipv6')['status'] == 200, 'exact 2'
1477 assert self.get(port=7081)['status'] == 404, 'exact 2 ipv4'
1690
1691 self.route_match({"source": "!::1"})
1478
1479 self.route_match({"source": "!::1"})
1692 self.assertEqual(self.get(sock_type='ipv6')['status'], 404, 'exact neg')
1693 self.assertEqual(self.get(port=7081)['status'], 200, 'exact neg ipv4')
1480 assert self.get(sock_type='ipv6')['status'] == 404, 'exact neg'
1481 assert self.get(port=7081)['status'] == 200, 'exact neg ipv4'
1694
1695 self.route_match({"source": "::2"})
1482
1483 self.route_match({"source": "::2"})
1696 self.assertEqual(self.get(sock_type='ipv6')['status'], 404, 'exact 3')
1697 self.assertEqual(self.get(port=7081)['status'], 404, 'exact 3 ipv4')
1484 assert self.get(sock_type='ipv6')['status'] == 404, 'exact 3'
1485 assert self.get(port=7081)['status'] == 404, 'exact 3 ipv4'
1698
1699 self.route_match({"source": "::1-::1"})
1486
1487 self.route_match({"source": "::1-::1"})
1700 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, 'range')
1701 self.assertEqual(self.get(port=7081)['status'], 404, 'range ipv4')
1488 assert self.get(sock_type='ipv6')['status'] == 200, 'range'
1489 assert self.get(port=7081)['status'] == 404, 'range ipv4'
1702
1703 self.route_match({"source": "::2-::2"})
1490
1491 self.route_match({"source": "::2-::2"})
1704 self.assertEqual(self.get(sock_type='ipv6')['status'], 404, 'range 2')
1705 self.assertEqual(self.get(port=7081)['status'], 404, 'range 2 ipv4')
1492 assert self.get(sock_type='ipv6')['status'] == 404, 'range 2'
1493 assert self.get(port=7081)['status'] == 404, 'range 2 ipv4'
1706
1707 self.route_match({"source": "::2-::3"})
1494
1495 self.route_match({"source": "::2-::3"})
1708 self.assertEqual(self.get(sock_type='ipv6')['status'], 404, 'range 3')
1709 self.assertEqual(self.get(port=7081)['status'], 404, 'range 3 ipv4')
1496 assert self.get(sock_type='ipv6')['status'] == 404, 'range 3'
1497 assert self.get(port=7081)['status'] == 404, 'range 3 ipv4'
1710
1711 self.route_match({"source": "::1-::2"})
1498
1499 self.route_match({"source": "::1-::2"})
1712 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, 'range 4')
1713 self.assertEqual(self.get(port=7081)['status'], 404, 'range 4 ipv4')
1500 assert self.get(sock_type='ipv6')['status'] == 200, 'range 4'
1501 assert self.get(port=7081)['status'] == 404, 'range 4 ipv4'
1714
1715 self.route_match({"source": "::0-::2"})
1502
1503 self.route_match({"source": "::0-::2"})
1716 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, 'range 5')
1717 self.assertEqual(self.get(port=7081)['status'], 404, 'range 5 ipv4')
1504 assert self.get(sock_type='ipv6')['status'] == 200, 'range 5'
1505 assert self.get(port=7081)['status'] == 404, 'range 5 ipv4'
1718
1719 self.route_match({"source": "::0-::1"})
1506
1507 self.route_match({"source": "::0-::1"})
1720 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, 'range 6')
1721 self.assertEqual(self.get(port=7081)['status'], 404, 'range 6 ipv4')
1508 assert self.get(sock_type='ipv6')['status'] == 200, 'range 6'
1509 assert self.get(port=7081)['status'] == 404, 'range 6 ipv4'
1722
1723 def test_routes_source_cidr(self):
1510
1511 def test_routes_source_cidr(self):
1724 self.assertIn(
1725 'success',
1726 self.conf(
1727 {
1728 "*:7080": {"pass": "routes"},
1729 "[::1]:7081": {"pass": "routes"},
1730 },
1731 'listeners',
1732 ),
1733 'source listeners configure',
1734 )
1512 assert 'success' in self.conf(
1513 {"*:7080": {"pass": "routes"}, "[::1]:7081": {"pass": "routes"},},
1514 'listeners',
1515 ), 'source listeners configure'
1735
1736 def get_ipv6():
1737 return self.get(sock_type='ipv6', port=7081)
1738
1739 self.route_match({"source": "127.0.0.1/32"})
1516
1517 def get_ipv6():
1518 return self.get(sock_type='ipv6', port=7081)
1519
1520 self.route_match({"source": "127.0.0.1/32"})
1740 self.assertEqual(self.get()['status'], 200, '32')
1741 self.assertEqual(get_ipv6()['status'], 404, '32 ipv6')
1521 assert self.get()['status'] == 200, '32'
1522 assert get_ipv6()['status'] == 404, '32 ipv6'
1742
1743 self.route_match({"source": "127.0.0.0/32"})
1523
1524 self.route_match({"source": "127.0.0.0/32"})
1744 self.assertEqual(self.get()['status'], 404, '32 2')
1745 self.assertEqual(get_ipv6()['status'], 404, '32 2 ipv6')
1525 assert self.get()['status'] == 404, '32 2'
1526 assert get_ipv6()['status'] == 404, '32 2 ipv6'
1746
1747 self.route_match({"source": "127.0.0.0/31"})
1527
1528 self.route_match({"source": "127.0.0.0/31"})
1748 self.assertEqual(self.get()['status'], 200, '31')
1749 self.assertEqual(get_ipv6()['status'], 404, '31 ipv6')
1529 assert self.get()['status'] == 200, '31'
1530 assert get_ipv6()['status'] == 404, '31 ipv6'
1750
1751 self.route_match({"source": "0.0.0.0/1"})
1531
1532 self.route_match({"source": "0.0.0.0/1"})
1752 self.assertEqual(self.get()['status'], 200, '1')
1753 self.assertEqual(get_ipv6()['status'], 404, '1 ipv6')
1533 assert self.get()['status'] == 200, '1'
1534 assert get_ipv6()['status'] == 404, '1 ipv6'
1754
1755 self.route_match({"source": "0.0.0.0/0"})
1535
1536 self.route_match({"source": "0.0.0.0/0"})
1756 self.assertEqual(self.get()['status'], 200, '0')
1757 self.assertEqual(get_ipv6()['status'], 404, '0 ipv6')
1537 assert self.get()['status'] == 200, '0'
1538 assert get_ipv6()['status'] == 404, '0 ipv6'
1758
1759 def test_routes_source_cidr_ipv6(self):
1539
1540 def test_routes_source_cidr_ipv6(self):
1760 self.assertIn(
1761 'success',
1762 self.conf(
1763 {
1764 "[::1]:7080": {"pass": "routes"},
1765 "127.0.0.1:7081": {"pass": "routes"},
1766 },
1767 'listeners',
1768 ),
1769 'source listeners configure',
1770 )
1541 assert 'success' in self.conf(
1542 {
1543 "[::1]:7080": {"pass": "routes"},
1544 "127.0.0.1:7081": {"pass": "routes"},
1545 },
1546 'listeners',
1547 ), 'source listeners configure'
1771
1772 self.route_match({"source": "::1/128"})
1548
1549 self.route_match({"source": "::1/128"})
1773 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, '128')
1774 self.assertEqual(self.get(port=7081)['status'], 404, '128 ipv4')
1550 assert self.get(sock_type='ipv6')['status'] == 200, '128'
1551 assert self.get(port=7081)['status'] == 404, '128 ipv4'
1775
1776 self.route_match({"source": "::0/128"})
1552
1553 self.route_match({"source": "::0/128"})
1777 self.assertEqual(self.get(sock_type='ipv6')['status'], 404, '128 2')
1778 self.assertEqual(self.get(port=7081)['status'], 404, '128 ipv4')
1554 assert self.get(sock_type='ipv6')['status'] == 404, '128 2'
1555 assert self.get(port=7081)['status'] == 404, '128 ipv4'
1779
1780 self.route_match({"source": "::0/127"})
1556
1557 self.route_match({"source": "::0/127"})
1781 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, '127')
1782 self.assertEqual(self.get(port=7081)['status'], 404, '127 ipv4')
1558 assert self.get(sock_type='ipv6')['status'] == 200, '127'
1559 assert self.get(port=7081)['status'] == 404, '127 ipv4'
1783
1784 self.route_match({"source": "::0/32"})
1560
1561 self.route_match({"source": "::0/32"})
1785 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, '32')
1786 self.assertEqual(self.get(port=7081)['status'], 404, '32 ipv4')
1562 assert self.get(sock_type='ipv6')['status'] == 200, '32'
1563 assert self.get(port=7081)['status'] == 404, '32 ipv4'
1787
1788 self.route_match({"source": "::0/1"})
1564
1565 self.route_match({"source": "::0/1"})
1789 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, '1')
1790 self.assertEqual(self.get(port=7081)['status'], 404, '1 ipv4')
1566 assert self.get(sock_type='ipv6')['status'] == 200, '1'
1567 assert self.get(port=7081)['status'] == 404, '1 ipv4'
1791
1792 self.route_match({"source": "::/0"})
1568
1569 self.route_match({"source": "::/0"})
1793 self.assertEqual(self.get(sock_type='ipv6')['status'], 200, '0')
1794 self.assertEqual(self.get(port=7081)['status'], 404, '0 ipv4')
1570 assert self.get(sock_type='ipv6')['status'] == 200, '0'
1571 assert self.get(port=7081)['status'] == 404, '0 ipv4'
1795
1796 def test_routes_source_unix(self):
1572
1573 def test_routes_source_unix(self):
1797 addr = self.testdir + '/sock'
1574 addr = self.temp_dir + '/sock'
1798
1575
1799 self.assertIn(
1800 'success',
1801 self.conf({"unix:" + addr: {"pass": "routes"}}, 'listeners'),
1802 'source listeners configure',
1803 )
1576 assert 'success' in self.conf(
1577 {"unix:" + addr: {"pass": "routes"}}, 'listeners'
1578 ), 'source listeners configure'
1804
1805 self.route_match({"source": "!0.0.0.0/0"})
1579
1580 self.route_match({"source": "!0.0.0.0/0"})
1806 self.assertEqual(
1807 self.get(sock_type='unix', addr=addr)['status'], 200, 'unix ipv4'
1808 )
1581 assert (
1582 self.get(sock_type='unix', addr=addr)['status'] == 200
1583 ), 'unix ipv4'
1809
1810 self.route_match({"source": "!::/0"})
1584
1585 self.route_match({"source": "!::/0"})
1811 self.assertEqual(
1812 self.get(sock_type='unix', addr=addr)['status'], 200, 'unix ipv6'
1813 )
1586 assert (
1587 self.get(sock_type='unix', addr=addr)['status'] == 200
1588 ), 'unix ipv6'
1814
1815 def test_routes_match_source(self):
1816 self.route_match({"source": "::"})
1817 self.route_match(
1818 {
1819 "source": [
1820 "127.0.0.1",
1821 "192.168.0.10:8080",

--- 36 unchanged lines hidden (view full) ---

1858 "source": [
1859 "2001::/16",
1860 "[0ff::/64]:8000",
1861 "[fff0:abcd:ffff:ffff:ffff::/128]:8080-8090",
1862 ]
1863 }
1864 )
1865 self.route_match({"source": "*:0-65535"})
1589
1590 def test_routes_match_source(self):
1591 self.route_match({"source": "::"})
1592 self.route_match(
1593 {
1594 "source": [
1595 "127.0.0.1",
1596 "192.168.0.10:8080",

--- 36 unchanged lines hidden (view full) ---

1633 "source": [
1634 "2001::/16",
1635 "[0ff::/64]:8000",
1636 "[fff0:abcd:ffff:ffff:ffff::/128]:8080-8090",
1637 ]
1638 }
1639 )
1640 self.route_match({"source": "*:0-65535"})
1866 self.assertEqual(self.get()['status'], 200, 'source any')
1641 assert self.get()['status'] == 200, 'source any'
1867
1868 def test_routes_match_source_invalid(self):
1869 self.route_match_invalid({"source": "127"})
1870 self.route_match_invalid({"source": "256.0.0.1"})
1871 self.route_match_invalid({"source": "127.0.0."})
1872 self.route_match_invalid({"source": " 127.0.0.1"})
1873 self.route_match_invalid({"source": "127.0.0.1:"})
1874 self.route_match_invalid({"source": "127.0.0.1/"})

--- 8 unchanged lines hidden (view full) ---

1883 self.route_match_invalid({"source": "::FFFFF"})
1884 self.route_match_invalid({"source": "[::1]:"})
1885 self.route_match_invalid({"source": "[:::]:7080"})
1886 self.route_match_invalid({"source": "*:"})
1887 self.route_match_invalid({"source": "*:1-a"})
1888 self.route_match_invalid({"source": "*:65536"})
1889
1890 def test_routes_match_destination(self):
1642
1643 def test_routes_match_source_invalid(self):
1644 self.route_match_invalid({"source": "127"})
1645 self.route_match_invalid({"source": "256.0.0.1"})
1646 self.route_match_invalid({"source": "127.0.0."})
1647 self.route_match_invalid({"source": " 127.0.0.1"})
1648 self.route_match_invalid({"source": "127.0.0.1:"})
1649 self.route_match_invalid({"source": "127.0.0.1/"})

--- 8 unchanged lines hidden (view full) ---

1658 self.route_match_invalid({"source": "::FFFFF"})
1659 self.route_match_invalid({"source": "[::1]:"})
1660 self.route_match_invalid({"source": "[:::]:7080"})
1661 self.route_match_invalid({"source": "*:"})
1662 self.route_match_invalid({"source": "*:1-a"})
1663 self.route_match_invalid({"source": "*:65536"})
1664
1665 def test_routes_match_destination(self):
1891 self.assertIn(
1892 'success',
1893 self.conf(
1894 {"*:7080": {"pass": "routes"}, "*:7081": {"pass": "routes"}},
1895 'listeners',
1896 ),
1897 'listeners configure',
1898 )
1666 assert 'success' in self.conf(
1667 {"*:7080": {"pass": "routes"}, "*:7081": {"pass": "routes"}},
1668 'listeners',
1669 ), 'listeners configure'
1899
1900 self.route_match({"destination": "*:7080"})
1670
1671 self.route_match({"destination": "*:7080"})
1901 self.assertEqual(self.get()['status'], 200, 'dest')
1902 self.assertEqual(self.get(port=7081)['status'], 404, 'dest 2')
1672 assert self.get()['status'] == 200, 'dest'
1673 assert self.get(port=7081)['status'] == 404, 'dest 2'
1903
1904 self.route_match({"destination": ["127.0.0.1:7080"]})
1674
1675 self.route_match({"destination": ["127.0.0.1:7080"]})
1905 self.assertEqual(self.get()['status'], 200, 'dest 3')
1906 self.assertEqual(self.get(port=7081)['status'], 404, 'dest 4')
1676 assert self.get()['status'] == 200, 'dest 3'
1677 assert self.get(port=7081)['status'] == 404, 'dest 4'
1907
1908 self.route_match({"destination": "!*:7080"})
1678
1679 self.route_match({"destination": "!*:7080"})
1909 self.assertEqual(self.get()['status'], 404, 'dest neg')
1910 self.assertEqual(self.get(port=7081)['status'], 200, 'dest neg 2')
1680 assert self.get()['status'] == 404, 'dest neg'
1681 assert self.get(port=7081)['status'] == 200, 'dest neg 2'
1911
1912 self.route_match({"destination": ['!*:7080', '!*:7081']})
1682
1683 self.route_match({"destination": ['!*:7080', '!*:7081']})
1913 self.assertEqual(self.get()['status'], 404, 'dest neg 3')
1914 self.assertEqual(self.get(port=7081)['status'], 404, 'dest neg 4')
1684 assert self.get()['status'] == 404, 'dest neg 3'
1685 assert self.get(port=7081)['status'] == 404, 'dest neg 4'
1915
1916 self.route_match({"destination": ['!*:7081', '!*:7082']})
1686
1687 self.route_match({"destination": ['!*:7081', '!*:7082']})
1917 self.assertEqual(self.get()['status'], 200, 'dest neg 5')
1688 assert self.get()['status'] == 200, 'dest neg 5'
1918
1919 self.route_match({"destination": ['*:7080', '!*:7080']})
1689
1690 self.route_match({"destination": ['*:7080', '!*:7080']})
1920 self.assertEqual(self.get()['status'], 404, 'dest neg 6')
1691 assert self.get()['status'] == 404, 'dest neg 6'
1921
1922 self.route_match(
1923 {"destination": ['127.0.0.1:7080', '*:7081', '!*:7080']}
1924 )
1692
1693 self.route_match(
1694 {"destination": ['127.0.0.1:7080', '*:7081', '!*:7080']}
1695 )
1925 self.assertEqual(self.get()['status'], 404, 'dest neg 7')
1926 self.assertEqual(self.get(port=7081)['status'], 200, 'dest neg 8')
1696 assert self.get()['status'] == 404, 'dest neg 7'
1697 assert self.get(port=7081)['status'] == 200, 'dest neg 8'
1927
1928 self.route_match({"destination": ['!*:7081', '!*:7082', '*:7083']})
1698
1699 self.route_match({"destination": ['!*:7081', '!*:7082', '*:7083']})
1929 self.assertEqual(self.get()['status'], 404, 'dest neg 9')
1700 assert self.get()['status'] == 404, 'dest neg 9'
1930
1931 self.route_match(
1932 {"destination": ['*:7081', '!127.0.0.1:7080', '*:7080']}
1933 )
1701
1702 self.route_match(
1703 {"destination": ['*:7081', '!127.0.0.1:7080', '*:7080']}
1704 )
1934 self.assertEqual(self.get()['status'], 404, 'dest neg 10')
1935 self.assertEqual(self.get(port=7081)['status'], 200, 'dest neg 11')
1705 assert self.get()['status'] == 404, 'dest neg 10'
1706 assert self.get(port=7081)['status'] == 200, 'dest neg 11'
1936
1707
1937 self.assertIn(
1938 'success',
1939 self.conf_delete('routes/0/match/destination/0'),
1940 'remove destination rule',
1941 )
1942 self.assertEqual(self.get()['status'], 404, 'dest neg 12')
1943 self.assertEqual(self.get(port=7081)['status'], 404, 'dest neg 13')
1708 assert 'success' in self.conf_delete(
1709 'routes/0/match/destination/0'
1710 ), 'remove destination rule'
1711 assert self.get()['status'] == 404, 'dest neg 12'
1712 assert self.get(port=7081)['status'] == 404, 'dest neg 13'
1944
1713
1945 self.assertIn(
1946 'success',
1947 self.conf_delete('routes/0/match/destination/0'),
1948 'remove destination rule 2',
1949 )
1950 self.assertEqual(self.get()['status'], 200, 'dest neg 14')
1951 self.assertEqual(self.get(port=7081)['status'], 404, 'dest neg 15')
1714 assert 'success' in self.conf_delete(
1715 'routes/0/match/destination/0'
1716 ), 'remove destination rule 2'
1717 assert self.get()['status'] == 200, 'dest neg 14'
1718 assert self.get(port=7081)['status'] == 404, 'dest neg 15'
1952
1719
1953 self.assertIn(
1954 'success',
1955 self.conf_post("\"!127.0.0.1\"", 'routes/0/match/destination'),
1956 'add destination rule',
1957 )
1958 self.assertEqual(self.get()['status'], 404, 'dest neg 16')
1959 self.assertEqual(self.get(port=7081)['status'], 404, 'dest neg 17')
1720 assert 'success' in self.conf_post(
1721 "\"!127.0.0.1\"", 'routes/0/match/destination'
1722 ), 'add destination rule'
1723 assert self.get()['status'] == 404, 'dest neg 16'
1724 assert self.get(port=7081)['status'] == 404, 'dest neg 17'
1960
1961 def test_routes_match_destination_proxy(self):
1725
1726 def test_routes_match_destination_proxy(self):
1962 self.assertIn(
1963 'success',
1964 self.conf(
1965 {
1966 "listeners": {
1967 "*:7080": {"pass": "routes/first"},
1968 "*:7081": {"pass": "routes/second"},
1969 },
1970 "routes": {
1971 "first": [
1972 {"action": {"proxy": "http://127.0.0.1:7081"}}
1973 ],
1974 "second": [
1975 {
1976 "match": {"destination": ["127.0.0.1:7081"]},
1977 "action": {"return": 200},
1978 }
1979 ],
1980 },
1981 "applications": {},
1982 }
1983 ),
1984 'proxy configure',
1985 )
1727 assert 'success' in self.conf(
1728 {
1729 "listeners": {
1730 "*:7080": {"pass": "routes/first"},
1731 "*:7081": {"pass": "routes/second"},
1732 },
1733 "routes": {
1734 "first": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
1735 "second": [
1736 {
1737 "match": {"destination": ["127.0.0.1:7081"]},
1738 "action": {"return": 200},
1739 }
1740 ],
1741 },
1742 "applications": {},
1743 }
1744 ), 'proxy configure'
1986
1745
1987 self.assertEqual(self.get()['status'], 200, 'proxy')
1988
1989
1990if __name__ == '__main__':
1991 TestRouting.main()
1746 assert self.get()['status'] == 200, 'proxy'