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