xref: /unit/test/test_variables.py (revision 1588:c606d051f770)
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                        "localhost": [{"action": {"return": 208}}],
24                    },
25                },
26            ),
27            'configure routes',
28        )
29
30    def conf_routes(self, routes):
31        self.assertIn('success', self.conf(routes, 'listeners/*:7080/pass'))
32
33    def test_variables_method(self):
34        self.assertEqual(self.get()['status'], 201, 'method GET')
35        self.assertEqual(self.post()['status'], 202, 'method POST')
36
37    def test_variables_uri(self):
38        self.conf_routes("\"routes$uri\"")
39
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')
43
44    def test_variables_host(self):
45        self.conf_routes("\"routes/$host\"")
46
47        def check_host(host, status=208):
48            self.assertEqual(
49                self.get(headers={'Host': host, 'Connection': 'close'})[
50                    'status'
51                ],
52                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\"")
64        self.assertEqual(self.get(url='/5')['status'], 206, 'many')
65
66        self.conf_routes("\"routes${uri}${method}\"")
67        self.assertEqual(self.get(url='/5')['status'], 206, 'many 2')
68
69        self.conf_routes("\"routes${uri}$method\"")
70        self.assertEqual(self.get(url='/5')['status'], 206, 'many 3')
71
72        self.conf_routes("\"routes/$method$method\"")
73        self.assertEqual(self.get()['status'], 207, 'many 4')
74
75        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')
78
79    def test_variables_replace(self):
80        self.assertEqual(self.get()['status'], 201)
81
82        self.conf_routes("\"routes$uri\"")
83        self.assertEqual(self.get(url='/3')['status'], 203)
84
85        self.conf_routes("\"routes/${method}\"")
86        self.assertEqual(self.post()['status'], 202)
87
88        self.conf_routes("\"routes${uri}\"")
89        self.assertEqual(self.get(url='/4*')['status'], 204)
90
91        self.conf_routes("\"routes/blah$method}\"")
92        self.assertEqual(self.get()['status'], 205)
93
94    def test_variables_invalid(self):
95        def check_variables(routes):
96            self.assertIn(
97                'error',
98                self.conf(routes, 'listeners/*:7080/pass'),
99                'invalid variables',
100            )
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}\"")
109
110if __name__ == '__main__':
111    TestVariables.main()
112