xref: /unit/test/test_variables.py (revision 1654:fc7d0578e124)
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": {
96                    "1": {
97                        "servers": {
98                            "127.0.0.1:7081": {},
99                        },
100                    },
101                },
102                "routes": {
103                    "one": [{"action": {"return": 200}}],
104                },
105            },
106        ), 'upstreams initial configuration'
107
108        assert self.get(url='/1')['status'] == 200
109        assert self.get(url='/2')['status'] == 404
110
111    def test_variables_invalid(self):
112        def check_variables(routes):
113            assert 'error' in self.conf(
114                routes, 'listeners/*:7080/pass'
115            ), 'invalid variables'
116
117        check_variables("\"routes$\"")
118        check_variables("\"routes${\"")
119        check_variables("\"routes${}\"")
120        check_variables("\"routes$ur\"")
121        check_variables("\"routes$uriblah\"")
122        check_variables("\"routes${uri\"")
123        check_variables("\"routes${{uri}\"")
124