xref: /unit/test/test_variables.py (revision 2066:242192963d93)
1from unit.applications.proto import TestApplicationProto
2
3
4class TestVariables(TestApplicationProto):
5    prerequisites = {}
6
7    def setup_method(self):
8        assert 'success' in self.conf(
9            {
10                "listeners": {"*:7080": {"pass": "routes/$method"}},
11                "routes": {
12                    "GET": [{"action": {"return": 201}}],
13                    "POST": [{"action": {"return": 202}}],
14                    "3": [{"action": {"return": 203}}],
15                    "4*": [{"action": {"return": 204}}],
16                    "blahGET}": [{"action": {"return": 205}}],
17                    "5GET": [{"action": {"return": 206}}],
18                    "GETGET": [{"action": {"return": 207}}],
19                    "localhost": [{"action": {"return": 208}}],
20                },
21            },
22        ), 'configure routes'
23
24    def conf_routes(self, routes):
25        assert 'success' in self.conf(routes, 'listeners/*:7080/pass')
26
27    def test_variables_method(self):
28        assert self.get()['status'] == 201, 'method GET'
29        assert self.post()['status'] == 202, 'method POST'
30
31    def test_variables_uri(self):
32        self.conf_routes("\"routes$uri\"")
33
34        assert self.get(url='/3')['status'] == 203, 'uri'
35        assert self.get(url='/4*')['status'] == 204, 'uri 2'
36        assert self.get(url='/4%2A')['status'] == 204, 'uri 3'
37
38    def test_variables_host(self):
39        self.conf_routes("\"routes/$host\"")
40
41        def check_host(host, status=208):
42            assert (
43                self.get(headers={'Host': host, 'Connection': 'close'})[
44                    'status'
45                ]
46                == status
47            )
48
49        check_host('localhost')
50        check_host('localhost.')
51        check_host('localhost:7080')
52        check_host('.localhost', 404)
53        check_host('www.localhost', 404)
54        check_host('localhost1', 404)
55
56    def test_variables_many(self):
57        self.conf_routes("\"routes$uri$method\"")
58        assert self.get(url='/5')['status'] == 206, 'many'
59
60        self.conf_routes("\"routes${uri}${method}\"")
61        assert self.get(url='/5')['status'] == 206, 'many 2'
62
63        self.conf_routes("\"routes${uri}$method\"")
64        assert self.get(url='/5')['status'] == 206, 'many 3'
65
66        self.conf_routes("\"routes/$method$method\"")
67        assert self.get()['status'] == 207, 'many 4'
68
69        self.conf_routes("\"routes/$method$uri\"")
70        assert self.get()['status'] == 404, 'no route'
71        assert self.get(url='/blah')['status'] == 404, 'no route 2'
72
73    def test_variables_replace(self):
74        assert self.get()['status'] == 201
75
76        self.conf_routes("\"routes$uri\"")
77        assert self.get(url='/3')['status'] == 203
78
79        self.conf_routes("\"routes/${method}\"")
80        assert self.post()['status'] == 202
81
82        self.conf_routes("\"routes${uri}\"")
83        assert self.get(url='/4*')['status'] == 204
84
85        self.conf_routes("\"routes/blah$method}\"")
86        assert self.get()['status'] == 205
87
88    def test_variables_upstream(self):
89        assert 'success' in self.conf(
90            {
91                "listeners": {
92                    "*:7080": {"pass": "upstreams$uri"},
93                    "*:7081": {"pass": "routes/one"},
94                },
95                "upstreams": {"1": {"servers": {"127.0.0.1:7081": {}}}},
96                "routes": {"one": [{"action": {"return": 200}}]},
97            },
98        ), 'upstreams initial configuration'
99
100        assert self.get(url='/1')['status'] == 200
101        assert self.get(url='/2')['status'] == 404
102
103    def test_variables_empty(self):
104        def update_pass(prefix):
105            assert 'success' in self.conf(
106                {"listeners": {"*:7080": {"pass": prefix + "/$method"}}},
107            ), 'variables empty'
108
109        update_pass("routes")
110        assert self.get(url='/1')['status'] == 404
111
112        update_pass("upstreams")
113        assert self.get(url='/2')['status'] == 404
114
115        update_pass("applications")
116        assert self.get(url='/3')['status'] == 404
117
118    def test_variables_invalid(self):
119        def check_variables(routes):
120            assert 'error' in self.conf(
121                routes, 'listeners/*:7080/pass'
122            ), 'invalid variables'
123
124        check_variables("\"routes$\"")
125        check_variables("\"routes${\"")
126        check_variables("\"routes${}\"")
127        check_variables("\"routes$ur\"")
128        check_variables("\"routes$uriblah\"")
129        check_variables("\"routes${uri\"")
130        check_variables("\"routes${{uri}\"")
131