xref: /unit/test/test_variables.py (revision 1567:8c6ba94a2a96)
1from unit.applications.proto import TestApplicationProto
2
3
4class TestVariables(TestApplicationProto):
5    prerequisites = {}
6
7    def setUp(self):
8        super().setUp()
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                    },
24                },
25            ),
26            'configure routes',
27        )
28
29    def conf_routes(self, routes):
30        self.assertIn(
31            'success',
32            self.conf(routes, 'listeners/*:7080/pass')
33        )
34
35    def test_variables_method(self):
36        self.assertEqual(self.get()['status'], 201, 'method GET')
37        self.assertEqual(self.post()['status'], 202, 'method POST')
38
39    def test_variables_uri(self):
40        self.conf_routes("\"routes$uri\"")
41
42        self.assertEqual(self.get(url='/3')['status'], 203, 'uri')
43        self.assertEqual(self.get(url='/4')['status'], 204, 'uri 2')
44
45    def test_variables_many(self):
46        self.conf_routes("\"routes$uri$method\"")
47        self.assertEqual(self.get(url='/5')['status'], 206, 'many')
48
49        self.conf_routes("\"routes${uri}${method}\"")
50        self.assertEqual(self.get(url='/5')['status'], 206, 'many 2')
51
52        self.conf_routes("\"routes${uri}$method\"")
53        self.assertEqual(self.get(url='/5')['status'], 206, 'many 3')
54
55        self.conf_routes("\"routes/$method$method\"")
56        self.assertEqual(self.get()['status'], 207, 'many 4')
57
58        self.conf_routes("\"routes/$method$uri\"")
59        self.assertEqual(self.get()['status'], 404, 'no route')
60        self.assertEqual(self.get(url='/blah')['status'], 404, 'no route 2')
61
62    def test_variables_replace(self):
63        self.assertEqual(self.get()['status'], 201)
64
65        self.conf_routes("\"routes$uri\"")
66        self.assertEqual(self.get(url='/3')['status'], 203)
67
68        self.conf_routes("\"routes/${method}\"")
69        self.assertEqual(self.post()['status'], 202)
70
71        self.conf_routes("\"routes${uri}\"")
72        self.assertEqual(self.get(url='/4')['status'], 204)
73
74        self.conf_routes("\"routes/blah$method}\"")
75        self.assertEqual(self.get()['status'], 205)
76
77    def test_variables_invalid(self):
78        def check_variables(routes):
79            self.assertIn(
80                'error',
81                self.conf(routes, 'listeners/*:7080/pass'),
82                'invalid variables',
83            )
84
85        check_variables("\"routes$\"")
86        check_variables("\"routes${\"")
87        check_variables("\"routes${}\"")
88        check_variables("\"routes$ur\"")
89        check_variables("\"routes$uriblah\"")
90        check_variables("\"routes${uri\"")
91        check_variables("\"routes${{uri}\"")
92
93if __name__ == '__main__':
94    TestVariables.main()
95