summaryrefslogtreecommitdiffhomepage
path: root/test/test_variables.py
blob: ccd0839b4ec7ce1373c89e9e7684b6c2adf3a302 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from unit.applications.proto import TestApplicationProto


class TestVariables(TestApplicationProto):
    prerequisites = {}

    def setup_method(self):
        assert 'success' in self.conf(
            {
                "listeners": {"*:7080": {"pass": "routes/$method"}},
                "routes": {
                    "GET": [{"action": {"return": 201}}],
                    "POST": [{"action": {"return": 202}}],
                    "3": [{"action": {"return": 203}}],
                    "4*": [{"action": {"return": 204}}],
                    "blahGET}": [{"action": {"return": 205}}],
                    "5GET": [{"action": {"return": 206}}],
                    "GETGET": [{"action": {"return": 207}}],
                    "localhost": [{"action": {"return": 208}}],
                    "9?q#a": [{"action": {"return": 209}}],
                    "blah": [{"action": {"return": 210}}],
                    "127.0.0.1": [{"action": {"return": 211}}],
                    "::1": [{"action": {"return": 212}}],
                    "referer-value": [{"action": {"return": 213}}],
                    "MSIE": [{"action": {"return": 214}}],
                },
            },
        ), 'configure routes'

    def conf_routes(self, routes):
        assert 'success' in self.conf(routes, 'listeners/*:7080/pass')

    def test_variables_method(self):
        assert self.get()['status'] == 201, 'method GET'
        assert self.post()['status'] == 202, 'method POST'

    def test_variables_request_uri(self):
        self.conf_routes("\"routes$request_uri\"")

        assert self.get(url='/3')['status'] == 203, 'request_uri'
        assert self.get(url='/4*')['status'] == 204, 'request_uri 2'
        assert self.get(url='/4%2A')['status'] == 204, 'request_uri 3'
        assert self.get(url='/9?q#a')['status'] == 209, 'request_uri query'

    def test_variables_uri(self):
        self.conf_routes("\"routes$uri\"")

        assert self.get(url='/3')['status'] == 203, 'uri'
        assert self.get(url='/4*')['status'] == 204, 'uri 2'
        assert self.get(url='/4%2A')['status'] == 204, 'uri 3'

    def test_variables_host(self):
        self.conf_routes("\"routes/$host\"")

        def check_host(host, status=208):
            assert (
                self.get(headers={'Host': host, 'Connection': 'close'})[
                    'status'
                ]
                == status
            )

        check_host('localhost')
        check_host('localhost.')
        check_host('localhost:7080')
        check_host('.localhost', 404)
        check_host('www.localhost', 404)
        check_host('localhost1', 404)

    def test_variables_remote_addr(self):
        self.conf_routes("\"routes/$remote_addr\"")
        assert self.get()['status'] == 211

        assert 'success' in self.conf(
            {"[::1]:7080": {"pass": "routes/$remote_addr"}}, 'listeners'
        )
        assert self.get(sock_type='ipv6')['status'] == 212

    def test_variables_header_referer(self):
        self.conf_routes("\"routes/$header_referer\"")

        def check_referer(referer, status=213):
            assert (
                self.get(
                    headers={
                        'Host': 'localhost',
                        'Connection': 'close',
                        'Referer': referer,
                    }
                )['status']
                == status
            )

        check_referer('referer-value')
        check_referer('', 404)
        check_referer('no', 404)

    def test_variables_header_user_agent(self):
        self.conf_routes("\"routes/$header_user_agent\"")

        def check_user_agent(user_agent, status=214):
            assert (
                self.get(
                    headers={
                        'Host': 'localhost',
                        'Connection': 'close',
                        'User-Agent': user_agent,
                    }
                )['status']
                == status
            )

        check_user_agent('MSIE')
        check_user_agent('', 404)
        check_user_agent('no', 404)

    def test_variables_many(self):
        self.conf_routes("\"routes$uri$method\"")
        assert self.get(url='/5')['status'] == 206, 'many'

        self.conf_routes("\"routes${uri}${method}\"")
        assert self.get(url='/5')['status'] == 206, 'many 2'

        self.conf_routes("\"routes${uri}$method\"")
        assert self.get(url='/5')['status'] == 206, 'many 3'

        self.conf_routes("\"routes/$method$method\"")
        assert self.get()['status'] == 207, 'many 4'

        self.conf_routes("\"routes/$method$uri\"")
        assert self.get()['status'] == 404, 'no route'
        assert self.get(url='/blah')['status'] == 404, 'no route 2'

    def test_variables_replace(self):
        assert self.get()['status'] == 201

        self.conf_routes("\"routes$uri\"")
        assert self.get(url='/3')['status'] == 203

        self.conf_routes("\"routes/${method}\"")
        assert self.post()['status'] == 202

        self.conf_routes("\"routes${uri}\"")
        assert self.get(url='/4*')['status'] == 204

        self.conf_routes("\"routes/blah$method}\"")
        assert self.get()['status'] == 205

    def test_variables_upstream(self):
        assert 'success' in self.conf(
            {
                "listeners": {
                    "*:7080": {"pass": "upstreams$uri"},
                    "*:7081": {"pass": "routes/one"},
                },
                "upstreams": {"1": {"servers": {"127.0.0.1:7081": {}}}},
                "routes": {"one": [{"action": {"return": 200}}]},
            },
        ), 'upstreams initial configuration'

        assert self.get(url='/1')['status'] == 200
        assert self.get(url='/2')['status'] == 404

    def test_variables_empty(self):
        def update_pass(prefix):
            assert 'success' in self.conf(
                {"listeners": {"*:7080": {"pass": prefix + "/$method"}}},
            ), 'variables empty'

        update_pass("routes")
        assert self.get(url='/1')['status'] == 404

        update_pass("upstreams")
        assert self.get(url='/2')['status'] == 404

        update_pass("applications")
        assert self.get(url='/3')['status'] == 404

    def test_variables_dynamic(self):
        self.conf_routes("\"routes/$header_foo$arg_foo$cookie_foo\"")

        self.get(
            url='/?foo=h',
            headers={'Foo': 'b', 'Cookie': 'foo=la', 'Connection': 'close'},
        )['status'] = 210

    def test_variables_dynamic_headers(self):
        def check_header(header, status=210):
            assert (
                self.get(headers={header: "blah", 'Connection': 'close'})[
                    'status'
                ]
                == status
            )

        self.conf_routes("\"routes/$header_foo_bar\"")
        check_header('foo-bar')
        check_header('Foo-Bar')
        check_header('foo_bar', 404)
        check_header('Foo', 404)
        check_header('Bar', 404)
        check_header('foobar', 404)

        self.conf_routes("\"routes/$header_Foo_Bar\"")
        check_header('Foo-Bar')
        check_header('foo-bar')
        check_header('foo_bar', 404)
        check_header('foobar', 404)

        self.conf_routes("\"routes/$header_foo-bar\"")
        check_header('foo_bar', 404)

    def test_variables_dynamic_arguments(self):
        self.conf_routes("\"routes/$arg_foo_bar\"")
        assert self.get(url='/?foo_bar=blah')['status'] == 210
        assert self.get(url='/?foo_b%61r=blah')['status'] == 210
        assert self.get(url='/?bar&foo_bar=blah&foo')['status'] == 210
        assert self.get(url='/?Foo_bar=blah')['status'] == 404
        assert self.get(url='/?foo-bar=blah')['status'] == 404
        assert self.get()['status'] == 404
        assert self.get(url='/?foo_bar=')['status'] == 404
        assert self.get(url='/?foo_bar=l&foo_bar=blah')['status'] == 210
        assert self.get(url='/?foo_bar=blah&foo_bar=l')['status'] == 404

        self.conf_routes("\"routes/$arg_foo_b%61r\"")
        assert self.get(url='/?foo_b=blah')['status'] == 404
        assert self.get(url='/?foo_bar=blah')['status'] == 404

        self.conf_routes("\"routes/$arg_f!~\"")
        assert self.get(url='/?f=blah')['status'] == 404
        assert self.get(url='/?f!~=blah')['status'] == 404

    def test_variables_dynamic_cookies(self):
        def check_cookie(cookie, status=210):
            assert (
                self.get(
                    headers={
                        'Host': 'localhost',
                        'Cookie': cookie,
                        'Connection': 'close',
                    },
                )['status']
                == status
            ), 'match cookie'

        self.conf_routes("\"routes/$cookie_foo_bar\"")
        check_cookie('foo_bar=blah', 210)
        check_cookie('fOo_bar=blah', 404)
        assert self.get()['status'] == 404
        check_cookie('foo_bar', 404)
        check_cookie('foo_bar=', 404)

    def test_variables_invalid(self):
        def check_variables(routes):
            assert 'error' in self.conf(
                routes, 'listeners/*:7080/pass'
            ), 'invalid variables'

        check_variables("\"routes$\"")
        check_variables("\"routes${\"")
        check_variables("\"routes${}\"")
        check_variables("\"routes$ur\"")
        check_variables("\"routes$uriblah\"")
        check_variables("\"routes${uri\"")
        check_variables("\"routes${{uri}\"")
        check_variables("\"routes$ar\"")
        check_variables("\"routes$arg\"")
        check_variables("\"routes$arg_\"")
        check_variables("\"routes$cookie\"")
        check_variables("\"routes$cookie_\"")
        check_variables("\"routes$header\"")
        check_variables("\"routes$header_\"")