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