xref: /unit/test/test_variables.py (revision 2114:c483df06e58c)
11567Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
21567Szelenkov@nginx.com
31567Szelenkov@nginx.com
41567Szelenkov@nginx.comclass TestVariables(TestApplicationProto):
51567Szelenkov@nginx.com    prerequisites = {}
61567Szelenkov@nginx.com
71596Szelenkov@nginx.com    def setup_method(self):
81596Szelenkov@nginx.com        assert 'success' in self.conf(
91596Szelenkov@nginx.com            {
101596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/$method"}},
111596Szelenkov@nginx.com                "routes": {
121596Szelenkov@nginx.com                    "GET": [{"action": {"return": 201}}],
131596Szelenkov@nginx.com                    "POST": [{"action": {"return": 202}}],
141596Szelenkov@nginx.com                    "3": [{"action": {"return": 203}}],
151596Szelenkov@nginx.com                    "4*": [{"action": {"return": 204}}],
161596Szelenkov@nginx.com                    "blahGET}": [{"action": {"return": 205}}],
171596Szelenkov@nginx.com                    "5GET": [{"action": {"return": 206}}],
181596Szelenkov@nginx.com                    "GETGET": [{"action": {"return": 207}}],
191596Szelenkov@nginx.com                    "localhost": [{"action": {"return": 208}}],
20*2114Szelenkov@nginx.com                    "9?q#a": [{"action": {"return": 209}}],
211567Szelenkov@nginx.com                },
221596Szelenkov@nginx.com            },
231596Szelenkov@nginx.com        ), 'configure routes'
241567Szelenkov@nginx.com
251567Szelenkov@nginx.com    def conf_routes(self, routes):
261596Szelenkov@nginx.com        assert 'success' in self.conf(routes, 'listeners/*:7080/pass')
271567Szelenkov@nginx.com
281567Szelenkov@nginx.com    def test_variables_method(self):
291596Szelenkov@nginx.com        assert self.get()['status'] == 201, 'method GET'
301596Szelenkov@nginx.com        assert self.post()['status'] == 202, 'method POST'
311567Szelenkov@nginx.com
322111Salx.manpages@gmail.com    def test_variables_request_uri(self):
332111Salx.manpages@gmail.com        self.conf_routes("\"routes$request_uri\"")
342111Salx.manpages@gmail.com
352111Salx.manpages@gmail.com        assert self.get(url='/3')['status'] == 203, 'request_uri'
362111Salx.manpages@gmail.com        assert self.get(url='/4*')['status'] == 204, 'request_uri 2'
372111Salx.manpages@gmail.com        assert self.get(url='/4%2A')['status'] == 204, 'request_uri 3'
38*2114Szelenkov@nginx.com        assert self.get(url='/9?q#a')['status'] == 209, 'request_uri query'
392111Salx.manpages@gmail.com
401567Szelenkov@nginx.com    def test_variables_uri(self):
411567Szelenkov@nginx.com        self.conf_routes("\"routes$uri\"")
421567Szelenkov@nginx.com
431596Szelenkov@nginx.com        assert self.get(url='/3')['status'] == 203, 'uri'
441596Szelenkov@nginx.com        assert self.get(url='/4*')['status'] == 204, 'uri 2'
451596Szelenkov@nginx.com        assert self.get(url='/4%2A')['status'] == 204, 'uri 3'
461588Szelenkov@nginx.com
471588Szelenkov@nginx.com    def test_variables_host(self):
481588Szelenkov@nginx.com        self.conf_routes("\"routes/$host\"")
491588Szelenkov@nginx.com
501588Szelenkov@nginx.com        def check_host(host, status=208):
511596Szelenkov@nginx.com            assert (
521588Szelenkov@nginx.com                self.get(headers={'Host': host, 'Connection': 'close'})[
531588Szelenkov@nginx.com                    'status'
541596Szelenkov@nginx.com                ]
551596Szelenkov@nginx.com                == status
561588Szelenkov@nginx.com            )
571588Szelenkov@nginx.com
581588Szelenkov@nginx.com        check_host('localhost')
591588Szelenkov@nginx.com        check_host('localhost.')
601588Szelenkov@nginx.com        check_host('localhost:7080')
611588Szelenkov@nginx.com        check_host('.localhost', 404)
621588Szelenkov@nginx.com        check_host('www.localhost', 404)
631588Szelenkov@nginx.com        check_host('localhost1', 404)
641567Szelenkov@nginx.com
651567Szelenkov@nginx.com    def test_variables_many(self):
661567Szelenkov@nginx.com        self.conf_routes("\"routes$uri$method\"")
671596Szelenkov@nginx.com        assert self.get(url='/5')['status'] == 206, 'many'
681567Szelenkov@nginx.com
691567Szelenkov@nginx.com        self.conf_routes("\"routes${uri}${method}\"")
701596Szelenkov@nginx.com        assert self.get(url='/5')['status'] == 206, 'many 2'
711567Szelenkov@nginx.com
721567Szelenkov@nginx.com        self.conf_routes("\"routes${uri}$method\"")
731596Szelenkov@nginx.com        assert self.get(url='/5')['status'] == 206, 'many 3'
741567Szelenkov@nginx.com
751567Szelenkov@nginx.com        self.conf_routes("\"routes/$method$method\"")
761596Szelenkov@nginx.com        assert self.get()['status'] == 207, 'many 4'
771567Szelenkov@nginx.com
781567Szelenkov@nginx.com        self.conf_routes("\"routes/$method$uri\"")
791596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'no route'
801596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404, 'no route 2'
811567Szelenkov@nginx.com
821567Szelenkov@nginx.com    def test_variables_replace(self):
831596Szelenkov@nginx.com        assert self.get()['status'] == 201
841567Szelenkov@nginx.com
851567Szelenkov@nginx.com        self.conf_routes("\"routes$uri\"")
861596Szelenkov@nginx.com        assert self.get(url='/3')['status'] == 203
871567Szelenkov@nginx.com
881567Szelenkov@nginx.com        self.conf_routes("\"routes/${method}\"")
891596Szelenkov@nginx.com        assert self.post()['status'] == 202
901567Szelenkov@nginx.com
911567Szelenkov@nginx.com        self.conf_routes("\"routes${uri}\"")
921596Szelenkov@nginx.com        assert self.get(url='/4*')['status'] == 204
931567Szelenkov@nginx.com
941567Szelenkov@nginx.com        self.conf_routes("\"routes/blah$method}\"")
951596Szelenkov@nginx.com        assert self.get()['status'] == 205
961567Szelenkov@nginx.com
971607Szelenkov@nginx.com    def test_variables_upstream(self):
981607Szelenkov@nginx.com        assert 'success' in self.conf(
991607Szelenkov@nginx.com            {
1001607Szelenkov@nginx.com                "listeners": {
1011607Szelenkov@nginx.com                    "*:7080": {"pass": "upstreams$uri"},
1021607Szelenkov@nginx.com                    "*:7081": {"pass": "routes/one"},
1031607Szelenkov@nginx.com                },
1041848Szelenkov@nginx.com                "upstreams": {"1": {"servers": {"127.0.0.1:7081": {}}}},
1051848Szelenkov@nginx.com                "routes": {"one": [{"action": {"return": 200}}]},
1061607Szelenkov@nginx.com            },
1071607Szelenkov@nginx.com        ), 'upstreams initial configuration'
1081607Szelenkov@nginx.com
1091607Szelenkov@nginx.com        assert self.get(url='/1')['status'] == 200
1101607Szelenkov@nginx.com        assert self.get(url='/2')['status'] == 404
1111607Szelenkov@nginx.com
1121928Sz.hong@f5.com    def test_variables_empty(self):
1131928Sz.hong@f5.com        def update_pass(prefix):
1141928Sz.hong@f5.com            assert 'success' in self.conf(
1152066Szelenkov@nginx.com                {"listeners": {"*:7080": {"pass": prefix + "/$method"}}},
1161928Sz.hong@f5.com            ), 'variables empty'
1171928Sz.hong@f5.com
1182066Szelenkov@nginx.com        update_pass("routes")
1191928Sz.hong@f5.com        assert self.get(url='/1')['status'] == 404
1201928Sz.hong@f5.com
1212066Szelenkov@nginx.com        update_pass("upstreams")
1221928Sz.hong@f5.com        assert self.get(url='/2')['status'] == 404
1231928Sz.hong@f5.com
1242066Szelenkov@nginx.com        update_pass("applications")
1251928Sz.hong@f5.com        assert self.get(url='/3')['status'] == 404
1261928Sz.hong@f5.com
1271567Szelenkov@nginx.com    def test_variables_invalid(self):
1281567Szelenkov@nginx.com        def check_variables(routes):
1291596Szelenkov@nginx.com            assert 'error' in self.conf(
1301596Szelenkov@nginx.com                routes, 'listeners/*:7080/pass'
1311596Szelenkov@nginx.com            ), 'invalid variables'
1321567Szelenkov@nginx.com
1331567Szelenkov@nginx.com        check_variables("\"routes$\"")
1341567Szelenkov@nginx.com        check_variables("\"routes${\"")
1351567Szelenkov@nginx.com        check_variables("\"routes${}\"")
1361567Szelenkov@nginx.com        check_variables("\"routes$ur\"")
1371567Szelenkov@nginx.com        check_variables("\"routes$uriblah\"")
1381567Szelenkov@nginx.com        check_variables("\"routes${uri\"")
1391567Szelenkov@nginx.com        check_variables("\"routes${{uri}\"")
140