1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
import unittest
import unit
class TestUnitRouting(unit.TestUnitApplicationProto):
def setUpClass():
unit.TestUnit().check_modules('python')
def setUp(self):
super().setUp()
self.conf({
"listeners": {
"*:7080": {
"pass": "routes"
}
},
"routes": [{
"match": { "method": "GET" },
"action": { "pass": "applications/empty" }
}],
"applications": {
"empty": {
"type": "python",
"processes": { "spare": 0 },
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir + '/python/empty',
"module": "wsgi"
},
"mirror": {
"type": "python",
"processes": { "spare": 0 },
"path": self.current_dir + '/python/mirror',
"working_directory": self.current_dir + '/python/mirror',
"module": "wsgi"
}
}
})
def test_routes_match_method_positive(self):
self.assertEqual(self.get()['status'], 200, 'method positive GET')
self.assertEqual(self.post()['status'], 404, 'method positive POST')
def test_routes_match_method_positive_many(self):
self.assertIn('success', self.conf([{
"match": { "method": ["GET", "POST"] },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method positive many configure')
self.assertEqual(self.get()['status'], 200, 'method positive many GET')
self.assertEqual(self.post()['status'], 200,
'method positive many POST')
self.assertEqual(self.delete()['status'], 404,
'method positive many DELETE')
def test_routes_match_method_negative(self):
self.assertIn('success', self.conf([{
"match": { "method": "!GET" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method negative configure')
self.assertEqual(self.get()['status'], 404, 'method negative GET')
self.assertEqual(self.post()['status'], 200, 'method negative POST')
def test_routes_match_method_negative_many(self):
self.assertIn('success', self.conf([{
"match": { "method": ["!GET", "!POST"] },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method negative many configure')
self.assertEqual(self.get()['status'], 404, 'method negative many GET')
self.assertEqual(self.post()['status'], 404,
'method negative many POST')
self.assertEqual(self.delete()['status'], 200,
'method negative many DELETE')
def test_routes_match_method_wildcard_left(self):
self.assertIn('success', self.conf([{
"match": { "method": "*ET" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method wildcard left configure')
self.assertEqual(self.get()['status'], 200, 'method wildcard left GET')
self.assertEqual(self.post()['status'], 404,
'method wildcard left POST')
def test_routes_match_method_wildcard_right(self):
self.assertIn('success', self.conf([{
"match": { "method": "GE*" },
"action": { "pass": "applications/empty"}
}], 'routes'), 'method wildcard right configure')
self.assertEqual(self.get()['status'], 200,
'method wildcard right GET')
self.assertEqual(self.post()['status'], 404,
'method wildcard right POST')
def test_routes_match_method_wildcard_left_right(self):
self.assertIn('success', self.conf([{
"match": { "method": "*GET*" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method wildcard left right configure')
self.assertEqual(self.get()['status'], 200,
'method wildcard right GET')
self.assertEqual(self.post()['status'], 404,
'method wildcard right POST')
def test_routes_match_method_wildcard(self):
self.assertIn('success', self.conf([{
"match": { "method": "*" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method wildcard configure')
self.assertEqual(self.get()['status'], 200, 'method wildcard')
def test_routes_match_method_case_insensitive(self):
self.assertIn('success', self.conf([{
"match": { "method": "get" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'method case insensitive configure')
self.assertEqual(self.get()['status'], 200, 'method case insensitive')
def test_routes_absent(self):
self.conf({
"listeners": {
"*:7081": {
"pass": "applications/empty"
}
},
"applications": {
"empty": {
"type": "python",
"processes": { "spare": 0 },
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir + '/python/empty',
"module": "wsgi"
}
}
})
self.assertEqual(self.get(port=7081)['status'], 200, 'routes absent')
def test_routes_pass_invalid(self):
self.assertIn('error', self.conf({ "pass": "routes/blah" },
'listeners/*:7080'), 'routes invalid')
def test_route_empty(self):
self.assertIn('success', self.conf({
"listeners": {
"*:7080": {
"pass": "routes/main"
}
},
"routes": {"main": []},
"applications": {
"empty": {
"type": "python",
"processes": { "spare": 0 },
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir + '/python/empty',
"module": "wsgi"
},
"mirror": {
"type": "python",
"processes": { "spare": 0 },
"path": self.current_dir + '/python/mirror',
"working_directory": self.current_dir + '/python/mirror',
"module": "wsgi"
}
}
}), 'route empty configure')
self.assertEqual(self.get()['status'], 404, 'route empty')
def test_routes_route_empty(self):
self.assertIn('success', self.conf({}, 'listeners'),
'routes empty listeners configure')
self.assertIn('success', self.conf({}, 'routes'),
'routes empty configure')
def test_routes_route_match_absent(self):
self.assertIn('success', self.conf([{
"action": { "pass": "applications/empty" }
}], 'routes'), 'route match absent configure')
self.assertEqual(self.get()['status'], 200, 'route match absent')
def test_routes_route_action_absent(self):
self.skip_alerts.append(r'failed to apply new conf')
self.assertIn('error', self.conf([{
"match": { "method": "GET" }
}], 'routes'), 'route pass absent configure')
def test_routes_route_pass_absent(self):
self.skip_alerts.append(r'failed to apply new conf')
self.assertIn('error', self.conf([{
"match": { "method": "GET" },
"action": {}
}], 'routes'), 'route pass absent configure')
def test_routes_rules_two(self):
self.assertIn('success', self.conf([{
"match": { "method": "GET" },
"action": { "pass": "applications/empty" }
},
{
"match": { "method": "POST" },
"action": { "pass": "applications/mirror" }
}], 'routes'), 'rules two configure')
self.assertEqual(self.get()['status'], 200, 'rules two match first')
self.assertEqual(self.post(headers={
'Host': 'localhost',
'Content-Type': 'text/html',
'Connection': 'close'
}, body='X')['status'], 200, 'rules two match second')
def test_routes_two(self):
self.assertIn('success', self.conf({
"listeners": {
"*:7080": {
"pass": "routes/first"
}
},
"routes": {
"first": [{
"match": { "method": "GET" },
"action": { "pass": "routes/second" }
}],
"second": [{
"match": { "host": "localhost" },
"action": { "pass": "applications/empty" }
}],
},
"applications": {
"empty": {
"type": "python",
"processes": { "spare": 0 },
"path": self.current_dir + '/python/empty',
"working_directory": self.current_dir + '/python/empty',
"module": "wsgi"
}
}
}), 'routes two configure')
self.assertEqual(self.get()['status'], 200, 'routes two')
def test_routes_match_host_positive(self):
self.assertIn('success', self.conf([{
"match": { "host": "localhost" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host positive configure')
self.assertEqual(self.get()['status'], 200,
'match host positive localhost')
self.assertEqual(self.get(headers={'Connection': 'close'})['status'],
404, 'match host positive empty')
self.assertEqual(self.get(headers={
'Host': 'localhost.',
'Connection': 'close'
})['status'], 200, 'match host positive trailing dot')
self.assertEqual(self.get(headers={
'Host': 'www.localhost',
'Connection': 'close'
})['status'], 404, 'match host positive www.localhost')
self.assertEqual(self.get(headers={
'Host': 'localhost1',
'Connection': 'close'
})['status'], 404, 'match host positive localhost1')
self.assertEqual(self.get(headers={
'Host': 'example.com',
'Connection': 'close'
})['status'], 404, 'match host positive example.com')
def test_routes_match_host_ipv4(self):
self.assertIn('success', self.conf([{
"match": { "host": "127.0.0.1" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host ipv4 configure')
self.assertEqual(self.get(headers={
'Host': '127.0.0.1',
'Connection': 'close'
})['status'], 200, 'match host ipv4')
def test_routes_match_host_ipv6(self):
self.assertIn('success', self.conf([{
"match": { "host": "[::1]" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host ipv6 configure')
self.assertEqual(self.get(headers={
'Host': '[::1]',
'Connection': 'close'
})['status'], 200, 'match host ipv6')
self.assertEqual(self.get(headers={
'Host': '[::1]:7080',
'Connection': 'close'
})['status'], 200, 'match host ipv6 port')
def test_routes_match_host_positive_many(self):
self.assertIn('success', self.conf([{
"match": { "host": ["localhost", "example.com"] },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host positive many configure')
self.assertEqual(self.get()['status'], 200,
'match host positive many localhost')
self.assertEqual(self.get(headers={
'Host': 'example.com',
'Connection': 'close'
})['status'], 200, 'match host positive many example.com')
def test_routes_match_host_positive_and_negative(self):
self.assertIn('success', self.conf([{
"match": { "host": ["*example.com", "!www.example.com"] },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host positive and negative configure')
self.assertEqual(self.get()['status'], 404,
'match host positive and negative localhost')
self.assertEqual(self.get(headers={
'Host': 'example.com',
'Connection': 'close'
})['status'], 200, 'match host positive and negative example.com')
self.assertEqual(self.get(headers={
'Host': 'www.example.com',
'Connection': 'close'
})['status'], 404, 'match host positive and negative www.example.com')
self.assertEqual(self.get(headers={
'Host': '!www.example.com',
'Connection': 'close'
})['status'], 200, 'match host positive and negative !www.example.com')
def test_routes_match_host_positive_and_negative_wildcard(self):
self.assertIn('success', self.conf([{
"match": { "host": ["*example*", "!www.example*"] },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host positive and negative wildcard configure')
self.assertEqual(self.get(headers={
'Host': 'example.com',
'Connection': 'close'
})['status'], 200,
'match host positive and negative wildcard example.com')
self.assertEqual(self.get(headers={
'Host': 'www.example.com',
'Connection': 'close'
})['status'], 404,
'match host positive and negative wildcard www.example.com')
def test_routes_match_host_case_insensitive(self):
self.assertIn('success', self.conf([{
"match": { "host": "Example.com" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'host case insensitive configure')
self.assertEqual(self.get(headers={
'Host': 'example.com',
'Connection': 'close'
})['status'], 200, 'host case insensitive example.com')
self.assertEqual(self.get(headers={
'Host': 'EXAMPLE.COM',
'Connection': 'close'
})['status'], 200, 'host case insensitive EXAMPLE.COM')
def test_routes_match_host_port(self):
self.assertIn('success', self.conf([{
"match": { "host": "example.com" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match host port configure')
self.assertEqual(self.get(headers={
'Host': 'example.com:7080',
'Connection': 'close'
})['status'], 200, 'match host port')
def test_routes_match_uri_positive(self):
self.assertIn('success', self.conf([{
"match": { "uri": "/" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match uri positive configure')
self.assertEqual(self.get()['status'], 200, 'match uri positive')
self.assertEqual(self.get(url='/blah')['status'], 404,
'match uri positive blah')
self.assertEqual(self.get(url='/#blah')['status'], 200,
'match uri positive #blah')
self.assertEqual(self.get(url='/?var')['status'], 200,
'match uri params')
self.assertEqual(self.get(url='//')['status'], 200,
'match uri adjacent slashes')
self.assertEqual(self.get(url='/blah/../')['status'], 200,
'match uri relative path')
self.assertEqual(self.get(url='/./')['status'], 200,
'match uri relative path')
def test_routes_match_uri_case_sensitive(self):
self.assertIn('success', self.conf([{
"match": { "uri": "/BLAH" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match uri case sensitive configure')
self.assertEqual(self.get(url='/blah')['status'], 404,
'match uri case sensitive blah')
self.assertEqual(self.get(url='/BlaH')['status'], 404,
'match uri case sensitive BlaH')
self.assertEqual(self.get(url='/BLAH')['status'], 200,
'match uri case sensitive BLAH')
def test_routes_match_uri_normalize(self):
self.assertIn('success', self.conf([{
"match": { "uri": "/blah" },
"action": { "pass": "applications/empty" }
}], 'routes'), 'match uri normalize configure')
self.assertEqual(self.get(url='/%62%6c%61%68')['status'], 200,
'match uri normalize')
def test_routes_match_rules(self):
self.assertIn('success', self.conf([{
"match": {
"method": "GET",
"host": "localhost",
"uri": "/"
},
"action": { "pass": "applications/empty" }
}], 'routes'), 'routes match rules configure')
self.assertEqual(self.get()['status'], 200, 'routes match rules')
def test_routes_loop(self):
self.assertIn('success', self.conf([{
"match": { "uri": "/" },
"action": { "pass": "routes" }
}], 'routes'), 'routes loop configure')
self.assertEqual(self.get()['status'], 500, 'routes loop')
if __name__ == '__main__':
TestUnitRouting.main()
|