xref: /unit/test/test_variables.py (revision 1596:b7e2d4d92624)
1from unit.applications.proto import TestApplicationProto
2
3
4class TestVariables(TestApplicationProto):
5    prerequisites = {}
6
7    def setup_method(self):
8        super().setup_method()
9
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}}],
22                },
23            },
24        ), 'configure routes'
25
26    def conf_routes(self, routes):
27        assert 'success' in self.conf(routes, 'listeners/*:7080/pass')
28
29    def test_variables_method(self):
30        assert self.get()['status'] == 201, 'method GET'
31        assert self.post()['status'] == 202, 'method POST'
32
33    def test_variables_uri(self):
34        self.conf_routes("\"routes$uri\"")
35
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'
39
40    def test_variables_host(self):
41        self.conf_routes("\"routes/$host\"")
42
43        def check_host(host, status=208):
44            assert (
45                self.get(headers={'Host': host, 'Connection': 'close'})[
46                    'status'
47                ]
48                == status
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\"")
60        assert self.get(url='/5')['status'] == 206, 'many'
61
62        self.conf_routes("\"routes${uri}${method}\"")
63        assert self.get(url='/5')['status'] == 206, 'many 2'
64
65        self.conf_routes("\"routes${uri}$method\"")
66        assert self.get(url='/5')['status'] == 206, 'many 3'
67
68        self.conf_routes("\"routes/$method$method\"")
69        assert self.get()['status'] == 207, 'many 4'
70
71        self.conf_routes("\"routes/$method$uri\"")
72        assert self.get()['status'] == 404, 'no route'
73        assert self.get(url='/blah')['status'] == 404, 'no route 2'
74
75    def test_variables_replace(self):
76        assert self.get()['status'] == 201
77
78        self.conf_routes("\"routes$uri\"")
79        assert self.get(url='/3')['status'] == 203
80
81        self.conf_routes("\"routes/${method}\"")
82        assert self.post()['status'] == 202
83
84        self.conf_routes("\"routes${uri}\"")
85        assert self.get(url='/4*')['status'] == 204
86
87        self.conf_routes("\"routes/blah$method}\"")
88        assert self.get()['status'] == 205
89
90    def test_variables_invalid(self):
91        def check_variables(routes):
92            assert 'error' in self.conf(
93                routes, 'listeners/*:7080/pass'
94            ), 'invalid variables'
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}\"")
103