test_variables.py (1588:c606d051f770) test_variables.py (1596:b7e2d4d92624)
1from unit.applications.proto import TestApplicationProto
2
3
4class TestVariables(TestApplicationProto):
5 prerequisites = {}
6
1from unit.applications.proto import TestApplicationProto
2
3
4class TestVariables(TestApplicationProto):
5 prerequisites = {}
6
7 def setUp(self):
8 super().setUp()
7 def setup_method(self):
8 super().setup_method()
9
9
10 self.assertIn(
11 'success',
12 self.conf(
13 {
14 "listeners": {"*:7080": {"pass": "routes/$method"}},
15 "routes": {
16 "GET": [{"action": {"return": 201}}],
17 "POST": [{"action": {"return": 202}}],
18 "3": [{"action": {"return": 203}}],
19 "4*": [{"action": {"return": 204}}],
20 "blahGET}": [{"action": {"return": 205}}],
21 "5GET": [{"action": {"return": 206}}],
22 "GETGET": [{"action": {"return": 207}}],
23 "localhost": [{"action": {"return": 208}}],
24 },
10 assert 'success' in self.conf(
11 {
12 "listeners": {"*:7080": {"pass": "routes/$method"}},
13 "routes": {
14 "GET": [{"action": {"return": 201}}],
15 "POST": [{"action": {"return": 202}}],
16 "3": [{"action": {"return": 203}}],
17 "4*": [{"action": {"return": 204}}],
18 "blahGET}": [{"action": {"return": 205}}],
19 "5GET": [{"action": {"return": 206}}],
20 "GETGET": [{"action": {"return": 207}}],
21 "localhost": [{"action": {"return": 208}}],
25 },
22 },
26 ),
27 'configure routes',
28 )
23 },
24 ), 'configure routes'
29
30 def conf_routes(self, routes):
25
26 def conf_routes(self, routes):
31 self.assertIn('success', self.conf(routes, 'listeners/*:7080/pass'))
27 assert 'success' in self.conf(routes, 'listeners/*:7080/pass')
32
33 def test_variables_method(self):
28
29 def test_variables_method(self):
34 self.assertEqual(self.get()['status'], 201, 'method GET')
35 self.assertEqual(self.post()['status'], 202, 'method POST')
30 assert self.get()['status'] == 201, 'method GET'
31 assert self.post()['status'] == 202, 'method POST'
36
37 def test_variables_uri(self):
38 self.conf_routes("\"routes$uri\"")
39
32
33 def test_variables_uri(self):
34 self.conf_routes("\"routes$uri\"")
35
40 self.assertEqual(self.get(url='/3')['status'], 203, 'uri')
41 self.assertEqual(self.get(url='/4*')['status'], 204, 'uri 2')
42 self.assertEqual(self.get(url='/4%2A')['status'], 204, 'uri 3')
36 assert self.get(url='/3')['status'] == 203, 'uri'
37 assert self.get(url='/4*')['status'] == 204, 'uri 2'
38 assert self.get(url='/4%2A')['status'] == 204, 'uri 3'
43
44 def test_variables_host(self):
45 self.conf_routes("\"routes/$host\"")
46
47 def check_host(host, status=208):
39
40 def test_variables_host(self):
41 self.conf_routes("\"routes/$host\"")
42
43 def check_host(host, status=208):
48 self.assertEqual(
44 assert (
49 self.get(headers={'Host': host, 'Connection': 'close'})[
50 'status'
45 self.get(headers={'Host': host, 'Connection': 'close'})[
46 'status'
51 ],
52 status,
47 ]
48 == status
53 )
54
55 check_host('localhost')
56 check_host('localhost.')
57 check_host('localhost:7080')
58 check_host('.localhost', 404)
59 check_host('www.localhost', 404)
60 check_host('localhost1', 404)
61
62 def test_variables_many(self):
63 self.conf_routes("\"routes$uri$method\"")
49 )
50
51 check_host('localhost')
52 check_host('localhost.')
53 check_host('localhost:7080')
54 check_host('.localhost', 404)
55 check_host('www.localhost', 404)
56 check_host('localhost1', 404)
57
58 def test_variables_many(self):
59 self.conf_routes("\"routes$uri$method\"")
64 self.assertEqual(self.get(url='/5')['status'], 206, 'many')
60 assert self.get(url='/5')['status'] == 206, 'many'
65
66 self.conf_routes("\"routes${uri}${method}\"")
61
62 self.conf_routes("\"routes${uri}${method}\"")
67 self.assertEqual(self.get(url='/5')['status'], 206, 'many 2')
63 assert self.get(url='/5')['status'] == 206, 'many 2'
68
69 self.conf_routes("\"routes${uri}$method\"")
64
65 self.conf_routes("\"routes${uri}$method\"")
70 self.assertEqual(self.get(url='/5')['status'], 206, 'many 3')
66 assert self.get(url='/5')['status'] == 206, 'many 3'
71
72 self.conf_routes("\"routes/$method$method\"")
67
68 self.conf_routes("\"routes/$method$method\"")
73 self.assertEqual(self.get()['status'], 207, 'many 4')
69 assert self.get()['status'] == 207, 'many 4'
74
75 self.conf_routes("\"routes/$method$uri\"")
70
71 self.conf_routes("\"routes/$method$uri\"")
76 self.assertEqual(self.get()['status'], 404, 'no route')
77 self.assertEqual(self.get(url='/blah')['status'], 404, 'no route 2')
72 assert self.get()['status'] == 404, 'no route'
73 assert self.get(url='/blah')['status'] == 404, 'no route 2'
78
79 def test_variables_replace(self):
74
75 def test_variables_replace(self):
80 self.assertEqual(self.get()['status'], 201)
76 assert self.get()['status'] == 201
81
82 self.conf_routes("\"routes$uri\"")
77
78 self.conf_routes("\"routes$uri\"")
83 self.assertEqual(self.get(url='/3')['status'], 203)
79 assert self.get(url='/3')['status'] == 203
84
85 self.conf_routes("\"routes/${method}\"")
80
81 self.conf_routes("\"routes/${method}\"")
86 self.assertEqual(self.post()['status'], 202)
82 assert self.post()['status'] == 202
87
88 self.conf_routes("\"routes${uri}\"")
83
84 self.conf_routes("\"routes${uri}\"")
89 self.assertEqual(self.get(url='/4*')['status'], 204)
85 assert self.get(url='/4*')['status'] == 204
90
91 self.conf_routes("\"routes/blah$method}\"")
86
87 self.conf_routes("\"routes/blah$method}\"")
92 self.assertEqual(self.get()['status'], 205)
88 assert self.get()['status'] == 205
93
94 def test_variables_invalid(self):
95 def check_variables(routes):
89
90 def test_variables_invalid(self):
91 def check_variables(routes):
96 self.assertIn(
97 'error',
98 self.conf(routes, 'listeners/*:7080/pass'),
99 'invalid variables',
100 )
92 assert 'error' in self.conf(
93 routes, 'listeners/*:7080/pass'
94 ), 'invalid variables'
101
102 check_variables("\"routes$\"")
103 check_variables("\"routes${\"")
104 check_variables("\"routes${}\"")
105 check_variables("\"routes$ur\"")
106 check_variables("\"routes$uriblah\"")
107 check_variables("\"routes${uri\"")
108 check_variables("\"routes${{uri}\"")
95
96 check_variables("\"routes$\"")
97 check_variables("\"routes${\"")
98 check_variables("\"routes${}\"")
99 check_variables("\"routes$ur\"")
100 check_variables("\"routes$uriblah\"")
101 check_variables("\"routes${uri\"")
102 check_variables("\"routes${{uri}\"")
109
110if __name__ == '__main__':
111 TestVariables.main()