xref: /unit/test/test_routing.py (revision 2073:bc6ad31ce286)
11475Saxel.duch@nginx.com# -*- coding: utf-8 -*-
21596Szelenkov@nginx.comimport pytest
32055Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
41730Szelenkov@nginx.comfrom unit.option import option
5972Szelenkov@nginx.com
61017Szelenkov@nginx.com
72055Szelenkov@nginx.comclass TestRouting(TestApplicationPython):
81467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
9972Szelenkov@nginx.com
101596Szelenkov@nginx.com    def setup_method(self):
111596Szelenkov@nginx.com        assert 'success' in self.conf(
121596Szelenkov@nginx.com            {
131596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes"}},
141596Szelenkov@nginx.com                "routes": [
15*2073Szelenkov@nginx.com                    {
16*2073Szelenkov@nginx.com                        "match": {"method": "GET"},
17*2073Szelenkov@nginx.com                        "action": {"return": 200},
18*2073Szelenkov@nginx.com                    }
191596Szelenkov@nginx.com                ],
201596Szelenkov@nginx.com                "applications": {},
211596Szelenkov@nginx.com            }
221596Szelenkov@nginx.com        ), 'routing configure'
23972Szelenkov@nginx.com
241101Szelenkov@nginx.com    def route(self, route):
251101Szelenkov@nginx.com        return self.conf([route], 'routes')
261101Szelenkov@nginx.com
271308Szelenkov@nginx.com    def route_match(self, match):
281596Szelenkov@nginx.com        assert 'success' in self.route(
291596Szelenkov@nginx.com            {"match": match, "action": {"return": 200}}
301596Szelenkov@nginx.com        ), 'route match configure'
311037Szelenkov@nginx.com
321308Szelenkov@nginx.com    def route_match_invalid(self, match):
331596Szelenkov@nginx.com        assert 'error' in self.route(
341596Szelenkov@nginx.com            {"match": match, "action": {"return": 200}}
351596Szelenkov@nginx.com        ), 'route match configure invalid'
361037Szelenkov@nginx.com
371308Szelenkov@nginx.com    def host(self, host, status):
381596Szelenkov@nginx.com        assert (
391596Szelenkov@nginx.com            self.get(headers={'Host': host, 'Connection': 'close'})['status']
401596Szelenkov@nginx.com            == status
411596Szelenkov@nginx.com        ), 'match host'
421308Szelenkov@nginx.com
431308Szelenkov@nginx.com    def cookie(self, cookie, status):
441596Szelenkov@nginx.com        assert (
451308Szelenkov@nginx.com            self.get(
461308Szelenkov@nginx.com                headers={
471308Szelenkov@nginx.com                    'Host': 'localhost',
481308Szelenkov@nginx.com                    'Cookie': cookie,
491308Szelenkov@nginx.com                    'Connection': 'close',
501308Szelenkov@nginx.com                },
511596Szelenkov@nginx.com            )['status']
521596Szelenkov@nginx.com            == status
531596Szelenkov@nginx.com        ), 'match cookie'
541037Szelenkov@nginx.com
551308Szelenkov@nginx.com    def test_routes_match_method_positive(self):
561596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
571596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
581308Szelenkov@nginx.com
591308Szelenkov@nginx.com    def test_routes_match_method_positive_many(self):
601308Szelenkov@nginx.com        self.route_match({"method": ["GET", "POST"]})
611308Szelenkov@nginx.com
621596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
631596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'POST'
641596Szelenkov@nginx.com        assert self.delete()['status'] == 404, 'DELETE'
651308Szelenkov@nginx.com
661308Szelenkov@nginx.com    def test_routes_match_method_negative(self):
671308Szelenkov@nginx.com        self.route_match({"method": "!GET"})
681308Szelenkov@nginx.com
691596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'GET'
701596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'POST'
711308Szelenkov@nginx.com
721308Szelenkov@nginx.com    def test_routes_match_method_negative_many(self):
731308Szelenkov@nginx.com        self.route_match({"method": ["!GET", "!POST"]})
741308Szelenkov@nginx.com
751596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'GET'
761596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
771596Szelenkov@nginx.com        assert self.delete()['status'] == 200, 'DELETE'
781308Szelenkov@nginx.com
791308Szelenkov@nginx.com    def test_routes_match_method_wildcard_left(self):
801308Szelenkov@nginx.com        self.route_match({"method": "*ET"})
811308Szelenkov@nginx.com
821596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
831596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
841037Szelenkov@nginx.com
851308Szelenkov@nginx.com    def test_routes_match_method_wildcard_right(self):
861308Szelenkov@nginx.com        self.route_match({"method": "GE*"})
871308Szelenkov@nginx.com
881596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
891596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
901308Szelenkov@nginx.com
911308Szelenkov@nginx.com    def test_routes_match_method_wildcard_left_right(self):
921308Szelenkov@nginx.com        self.route_match({"method": "*GET*"})
931308Szelenkov@nginx.com
941596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
951596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
961308Szelenkov@nginx.com
971308Szelenkov@nginx.com    def test_routes_match_method_wildcard(self):
981308Szelenkov@nginx.com        self.route_match({"method": "*"})
991308Szelenkov@nginx.com
1001596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1011308Szelenkov@nginx.com
1021308Szelenkov@nginx.com    def test_routes_match_invalid(self):
1031308Szelenkov@nginx.com        self.route_match_invalid({"method": "**"})
1041508Saxel.duch@nginx.com
1051508Saxel.duch@nginx.com    def test_routes_match_valid(self):
1061508Saxel.duch@nginx.com        self.route_match({"method": "blah*"})
1071508Saxel.duch@nginx.com        self.route_match({"host": "*blah*blah"})
1081508Saxel.duch@nginx.com        self.route_match({"host": "blah*blah*blah"})
1091508Saxel.duch@nginx.com        self.route_match({"host": "blah*blah*"})
1101508Saxel.duch@nginx.com
1111508Saxel.duch@nginx.com    def test_routes_match_empty_exact(self):
1121508Saxel.duch@nginx.com        self.route_match({"uri": ""})
1131596Szelenkov@nginx.com        assert self.get()['status'] == 404
1141508Saxel.duch@nginx.com
1151508Saxel.duch@nginx.com        self.route_match({"uri": "/"})
1161596Szelenkov@nginx.com        assert self.get()['status'] == 200
1171596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404
1181508Saxel.duch@nginx.com
1191508Saxel.duch@nginx.com    def test_routes_match_negative(self):
1201508Saxel.duch@nginx.com        self.route_match({"uri": "!"})
1211633Svbart@nginx.com        assert self.get()['status'] == 200
1221633Svbart@nginx.com
1231633Svbart@nginx.com        self.route_match({"uri": "!*"})
1241596Szelenkov@nginx.com        assert self.get()['status'] == 404
1251508Saxel.duch@nginx.com
1261508Saxel.duch@nginx.com        self.route_match({"uri": "!/"})
1271596Szelenkov@nginx.com        assert self.get()['status'] == 404
1281596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200
1291508Saxel.duch@nginx.com
1301508Saxel.duch@nginx.com        self.route_match({"uri": "!*blah"})
1311596Szelenkov@nginx.com        assert self.get()['status'] == 200
1321596Szelenkov@nginx.com        assert self.get(url='/bla')['status'] == 200
1331596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404
1341596Szelenkov@nginx.com        assert self.get(url='/blah1')['status'] == 200
1351508Saxel.duch@nginx.com
1361508Saxel.duch@nginx.com        self.route_match({"uri": "!/blah*1*"})
1371596Szelenkov@nginx.com        assert self.get()['status'] == 200
1381596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200
1391596Szelenkov@nginx.com        assert self.get(url='/blah1')['status'] == 404
1401596Szelenkov@nginx.com        assert self.get(url='/blah12')['status'] == 404
1411596Szelenkov@nginx.com        assert self.get(url='/blah2')['status'] == 200
1421308Szelenkov@nginx.com
1431308Szelenkov@nginx.com    def test_routes_match_wildcard_middle(self):
1441308Szelenkov@nginx.com        self.route_match({"host": "ex*le"})
1451308Szelenkov@nginx.com
1461308Szelenkov@nginx.com        self.host('example', 200)
1471308Szelenkov@nginx.com        self.host('www.example', 404)
1481308Szelenkov@nginx.com        self.host('example.com', 404)
1491308Szelenkov@nginx.com        self.host('exampl', 404)
1501037Szelenkov@nginx.com
151972Szelenkov@nginx.com    def test_routes_match_method_case_insensitive(self):
1521308Szelenkov@nginx.com        self.route_match({"method": "get"})
153972Szelenkov@nginx.com
1541596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
155972Szelenkov@nginx.com
1561037Szelenkov@nginx.com    def test_routes_match_wildcard_left_case_insensitive(self):
1571308Szelenkov@nginx.com        self.route_match({"method": "*get"})
1581596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1591037Szelenkov@nginx.com
1601308Szelenkov@nginx.com        self.route_match({"method": "*et"})
1611596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1621037Szelenkov@nginx.com
1631037Szelenkov@nginx.com    def test_routes_match_wildcard_middle_case_insensitive(self):
1641308Szelenkov@nginx.com        self.route_match({"method": "g*t"})
1651037Szelenkov@nginx.com
1661596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1671037Szelenkov@nginx.com
1681037Szelenkov@nginx.com    def test_routes_match_wildcard_right_case_insensitive(self):
1691308Szelenkov@nginx.com        self.route_match({"method": "get*"})
1701596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1711037Szelenkov@nginx.com
1721308Szelenkov@nginx.com        self.route_match({"method": "ge*"})
1731596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1741037Szelenkov@nginx.com
1751037Szelenkov@nginx.com    def test_routes_match_wildcard_substring_case_insensitive(self):
1761308Szelenkov@nginx.com        self.route_match({"method": "*et*"})
1771037Szelenkov@nginx.com
1781596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1791037Szelenkov@nginx.com
1801037Szelenkov@nginx.com    def test_routes_match_wildcard_left_case_sensitive(self):
1811308Szelenkov@nginx.com        self.route_match({"uri": "*blah"})
1821037Szelenkov@nginx.com
1831596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
1841596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
1851037Szelenkov@nginx.com
1861037Szelenkov@nginx.com    def test_routes_match_wildcard_middle_case_sensitive(self):
1871308Szelenkov@nginx.com        self.route_match({"uri": "/b*h"})
1881037Szelenkov@nginx.com
1891596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
1901596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
1911037Szelenkov@nginx.com
1921510Saxel.duch@nginx.com    def test_route_match_wildcards_ordered(self):
1931510Saxel.duch@nginx.com        self.route_match({"uri": "/a*x*y*"})
1941510Saxel.duch@nginx.com
1951596Szelenkov@nginx.com        assert self.get(url='/axy')['status'] == 200, '/axy'
1961596Szelenkov@nginx.com        assert self.get(url='/ayx')['status'] == 404, '/ayx'
1971510Saxel.duch@nginx.com
1981510Saxel.duch@nginx.com    def test_route_match_wildcards_adjust_start(self):
1991510Saxel.duch@nginx.com        self.route_match({"uri": "/bla*bla*"})
2001510Saxel.duch@nginx.com
2011596Szelenkov@nginx.com        assert self.get(url='/bla_foo')['status'] == 404, '/bla_foo'
2021510Saxel.duch@nginx.com
2031510Saxel.duch@nginx.com    def test_route_match_wildcards_adjust_start_substr(self):
2041510Saxel.duch@nginx.com        self.route_match({"uri": "*bla*bla*"})
2051510Saxel.duch@nginx.com
2061596Szelenkov@nginx.com        assert self.get(url='/bla_foo')['status'] == 404, '/bla_foo'
2071510Saxel.duch@nginx.com
2081510Saxel.duch@nginx.com    def test_route_match_wildcards_adjust_end(self):
2091510Saxel.duch@nginx.com        self.route_match({"uri": "/bla*bla"})
2101510Saxel.duch@nginx.com
2111596Szelenkov@nginx.com        assert self.get(url='/foo_bla')['status'] == 404, '/foo_bla'
2121510Saxel.duch@nginx.com
2131037Szelenkov@nginx.com    def test_routes_match_wildcard_right_case_sensitive(self):
2141308Szelenkov@nginx.com        self.route_match({"uri": "/bla*"})
2151037Szelenkov@nginx.com
2161596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
2171596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
2181037Szelenkov@nginx.com
2191037Szelenkov@nginx.com    def test_routes_match_wildcard_substring_case_sensitive(self):
2201308Szelenkov@nginx.com        self.route_match({"uri": "*bla*"})
2211037Szelenkov@nginx.com
2221596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
2231596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
2241037Szelenkov@nginx.com
2251508Saxel.duch@nginx.com    def test_routes_match_many_wildcard_substrings_case_sensitive(self):
2261508Saxel.duch@nginx.com        self.route_match({"uri": "*a*B*c*"})
2271508Saxel.duch@nginx.com
2281596Szelenkov@nginx.com        assert self.get(url='/blah-a-B-c-blah')['status'] == 200
2291596Szelenkov@nginx.com        assert self.get(url='/a-B-c')['status'] == 200
2301596Szelenkov@nginx.com        assert self.get(url='/aBc')['status'] == 200
2311596Szelenkov@nginx.com        assert self.get(url='/aBCaBbc')['status'] == 200
2321596Szelenkov@nginx.com        assert self.get(url='/ABc')['status'] == 404
2331508Saxel.duch@nginx.com
2341721Saxel.duch@nginx.com    def test_routes_empty_regex(self):
2351807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2361807Szelenkov@nginx.com            pytest.skip('requires regex')
2371807Szelenkov@nginx.com
2381848Szelenkov@nginx.com        self.route_match({"uri": "~"})
2391721Saxel.duch@nginx.com        assert self.get(url='/')['status'] == 200, 'empty regexp'
2401721Saxel.duch@nginx.com        assert self.get(url='/anything')['status'] == 200, '/anything'
2411721Saxel.duch@nginx.com
2421848Szelenkov@nginx.com        self.route_match({"uri": "!~"})
2431721Saxel.duch@nginx.com        assert self.get(url='/')['status'] == 404, 'empty regexp 2'
2441721Saxel.duch@nginx.com        assert self.get(url='/nothing')['status'] == 404, '/nothing'
2451721Saxel.duch@nginx.com
2461721Saxel.duch@nginx.com    def test_routes_bad_regex(self):
2471807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2481807Szelenkov@nginx.com            pytest.skip('requires regex')
2491807Szelenkov@nginx.com
2501721Saxel.duch@nginx.com        assert 'error' in self.route(
2511721Saxel.duch@nginx.com            {"match": {"uri": "~/bl[ah"}, "action": {"return": 200}}
2521721Saxel.duch@nginx.com        ), 'bad regex'
2531721Saxel.duch@nginx.com
2541721Saxel.duch@nginx.com        status = self.route(
2551721Saxel.duch@nginx.com            {"match": {"uri": "~(?R)?z"}, "action": {"return": 200}}
2561721Saxel.duch@nginx.com        )
2571721Saxel.duch@nginx.com        if 'error' not in status:
2581721Saxel.duch@nginx.com            assert self.get(url='/nothing_z')['status'] == 500, '/nothing_z'
2591721Saxel.duch@nginx.com
2601721Saxel.duch@nginx.com        status = self.route(
2611721Saxel.duch@nginx.com            {"match": {"uri": "~((?1)?z)"}, "action": {"return": 200}}
2621721Saxel.duch@nginx.com        )
2631721Saxel.duch@nginx.com        if 'error' not in status:
2641721Saxel.duch@nginx.com            assert self.get(url='/nothing_z')['status'] == 500, '/nothing_z'
2651721Saxel.duch@nginx.com
2661721Saxel.duch@nginx.com    def test_routes_match_regex_case_sensitive(self):
2671807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2681807Szelenkov@nginx.com            pytest.skip('requires regex')
2691807Szelenkov@nginx.com
2701721Saxel.duch@nginx.com        self.route_match({"uri": "~/bl[ah]"})
2711721Saxel.duch@nginx.com
2721721Saxel.duch@nginx.com        assert self.get(url='/rlah')['status'] == 404, '/rlah'
2731721Saxel.duch@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
2741721Saxel.duch@nginx.com        assert self.get(url='/blh')['status'] == 200, '/blh'
2751721Saxel.duch@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
2761721Saxel.duch@nginx.com
2771721Saxel.duch@nginx.com    def test_routes_match_regex_negative_case_sensitive(self):
2781807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2791807Szelenkov@nginx.com            pytest.skip('requires regex')
2801807Szelenkov@nginx.com
2811721Saxel.duch@nginx.com        self.route_match({"uri": "!~/bl[ah]"})
2821721Saxel.duch@nginx.com
2831721Saxel.duch@nginx.com        assert self.get(url='/rlah')['status'] == 200, '/rlah'
2841721Saxel.duch@nginx.com        assert self.get(url='/blah')['status'] == 404, '/blah'
2851721Saxel.duch@nginx.com        assert self.get(url='/blh')['status'] == 404, '/blh'
2861721Saxel.duch@nginx.com        assert self.get(url='/BLAH')['status'] == 200, '/BLAH'
2871721Saxel.duch@nginx.com
2881478Szelenkov@nginx.com    def test_routes_pass_encode(self):
2891478Szelenkov@nginx.com        def check_pass(path, name):
2901596Szelenkov@nginx.com            assert 'success' in self.conf(
2911596Szelenkov@nginx.com                {
2921596Szelenkov@nginx.com                    "listeners": {"*:7080": {"pass": "applications/" + path}},
2931596Szelenkov@nginx.com                    "applications": {
2941596Szelenkov@nginx.com                        name: {
2952055Szelenkov@nginx.com                            "type": self.get_application_type(),
2961596Szelenkov@nginx.com                            "processes": {"spare": 0},
2971596Szelenkov@nginx.com                            "path": option.test_dir + '/python/empty',
2981596Szelenkov@nginx.com                            "working_directory": option.test_dir
2991596Szelenkov@nginx.com                            + '/python/empty',
3001596Szelenkov@nginx.com                            "module": "wsgi",
3011596Szelenkov@nginx.com                        }
3021596Szelenkov@nginx.com                    },
3031596Szelenkov@nginx.com                }
3041478Szelenkov@nginx.com            )
3051478Szelenkov@nginx.com
3061596Szelenkov@nginx.com            assert self.get()['status'] == 200
3071478Szelenkov@nginx.com
3081478Szelenkov@nginx.com        check_pass("%25", "%")
3091478Szelenkov@nginx.com        check_pass("blah%2Fblah", "blah/blah")
3101478Szelenkov@nginx.com        check_pass("%2Fblah%2F%2Fblah%2F", "/blah//blah/")
3111478Szelenkov@nginx.com        check_pass("%20blah%252Fblah%7E", " blah%2Fblah~")
3121478Szelenkov@nginx.com
3131478Szelenkov@nginx.com        def check_pass_error(path, name):
3141596Szelenkov@nginx.com            assert 'error' in self.conf(
3151596Szelenkov@nginx.com                {
3161596Szelenkov@nginx.com                    "listeners": {"*:7080": {"pass": "applications/" + path}},
3171596Szelenkov@nginx.com                    "applications": {
3181596Szelenkov@nginx.com                        name: {
3192055Szelenkov@nginx.com                            "type": self.get_application_type(),
3201596Szelenkov@nginx.com                            "processes": {"spare": 0},
3211596Szelenkov@nginx.com                            "path": option.test_dir + '/python/empty',
3221596Szelenkov@nginx.com                            "working_directory": option.test_dir
3231596Szelenkov@nginx.com                            + '/python/empty',
3241596Szelenkov@nginx.com                            "module": "wsgi",
3251596Szelenkov@nginx.com                        }
3261596Szelenkov@nginx.com                    },
3271596Szelenkov@nginx.com                }
3281478Szelenkov@nginx.com            )
3291478Szelenkov@nginx.com
3301478Szelenkov@nginx.com        check_pass_error("%", "%")
3311478Szelenkov@nginx.com        check_pass_error("%1", "%1")
3321478Szelenkov@nginx.com
333972Szelenkov@nginx.com    def test_routes_absent(self):
3341775Szelenkov@nginx.com        assert 'success' in self.conf(
3351017Szelenkov@nginx.com            {
3361017Szelenkov@nginx.com                "listeners": {"*:7081": {"pass": "applications/empty"}},
3371017Szelenkov@nginx.com                "applications": {
3381017Szelenkov@nginx.com                    "empty": {
3392055Szelenkov@nginx.com                        "type": self.get_application_type(),
3401017Szelenkov@nginx.com                        "processes": {"spare": 0},
3411596Szelenkov@nginx.com                        "path": option.test_dir + '/python/empty',
3421848Szelenkov@nginx.com                        "working_directory": option.test_dir + '/python/empty',
3431017Szelenkov@nginx.com                        "module": "wsgi",
3441017Szelenkov@nginx.com                    }
3451017Szelenkov@nginx.com                },
346972Szelenkov@nginx.com            }
3471017Szelenkov@nginx.com        )
348972Szelenkov@nginx.com
3491596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 200, 'routes absent'
350972Szelenkov@nginx.com
351972Szelenkov@nginx.com    def test_routes_pass_invalid(self):
3521596Szelenkov@nginx.com        assert 'error' in self.conf(
3531596Szelenkov@nginx.com            {"pass": "routes/blah"}, 'listeners/*:7080'
3541596Szelenkov@nginx.com        ), 'routes invalid'
355972Szelenkov@nginx.com
356972Szelenkov@nginx.com    def test_route_empty(self):
3571596Szelenkov@nginx.com        assert 'success' in self.conf(
3581596Szelenkov@nginx.com            {
3591596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/main"}},
3601596Szelenkov@nginx.com                "routes": {"main": []},
3611596Szelenkov@nginx.com                "applications": {},
3621596Szelenkov@nginx.com            }
3631596Szelenkov@nginx.com        ), 'route empty configure'
364972Szelenkov@nginx.com
3651596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'route empty'
366972Szelenkov@nginx.com
367972Szelenkov@nginx.com    def test_routes_route_empty(self):
3681596Szelenkov@nginx.com        assert 'success' in self.conf(
3691596Szelenkov@nginx.com            {}, 'listeners'
3701596Szelenkov@nginx.com        ), 'routes empty listeners configure'
371972Szelenkov@nginx.com
3721596Szelenkov@nginx.com        assert 'success' in self.conf({}, 'routes'), 'routes empty configure'
373972Szelenkov@nginx.com
374972Szelenkov@nginx.com    def test_routes_route_match_absent(self):
3751596Szelenkov@nginx.com        assert 'success' in self.conf(
3761596Szelenkov@nginx.com            [{"action": {"return": 200}}], 'routes'
3771596Szelenkov@nginx.com        ), 'route match absent configure'
378972Szelenkov@nginx.com
3791596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'route match absent'
380972Szelenkov@nginx.com
3811736Szelenkov@nginx.com    def test_routes_route_action_absent(self, skip_alert):
3821596Szelenkov@nginx.com        skip_alert(r'failed to apply new conf')
383972Szelenkov@nginx.com
3841596Szelenkov@nginx.com        assert 'error' in self.conf(
3851596Szelenkov@nginx.com            [{"match": {"method": "GET"}}], 'routes'
3861596Szelenkov@nginx.com        ), 'route pass absent configure'
387972Szelenkov@nginx.com
3881597Shongzhidao@gmail.com    def test_routes_route_pass(self):
3891597Shongzhidao@gmail.com        assert 'success' in self.conf(
3901597Shongzhidao@gmail.com            {
3911597Shongzhidao@gmail.com                "applications": {
3921597Shongzhidao@gmail.com                    "app": {
3932055Szelenkov@nginx.com                        "type": self.get_application_type(),
3941597Shongzhidao@gmail.com                        "processes": {"spare": 0},
3951597Shongzhidao@gmail.com                        "path": "/app",
3961597Shongzhidao@gmail.com                        "module": "wsgi",
3971597Shongzhidao@gmail.com                    }
3981597Shongzhidao@gmail.com                },
3991597Shongzhidao@gmail.com                "upstreams": {
4001597Shongzhidao@gmail.com                    "one": {
4011597Shongzhidao@gmail.com                        "servers": {
4021597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4031597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4041597Shongzhidao@gmail.com                        },
4051597Shongzhidao@gmail.com                    },
4061597Shongzhidao@gmail.com                    "two": {
4071597Shongzhidao@gmail.com                        "servers": {
4081597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4091597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4101597Shongzhidao@gmail.com                        },
4111597Shongzhidao@gmail.com                    },
4121597Shongzhidao@gmail.com                },
4131597Shongzhidao@gmail.com            }
4141597Shongzhidao@gmail.com        )
4151597Shongzhidao@gmail.com
4161597Shongzhidao@gmail.com        assert 'success' in self.conf(
4171597Shongzhidao@gmail.com            [{"action": {"pass": "routes"}}], 'routes'
4181597Shongzhidao@gmail.com        )
4191597Shongzhidao@gmail.com        assert 'success' in self.conf(
4201597Shongzhidao@gmail.com            [{"action": {"pass": "applications/app"}}], 'routes'
4211597Shongzhidao@gmail.com        )
4221597Shongzhidao@gmail.com        assert 'success' in self.conf(
4231597Shongzhidao@gmail.com            [{"action": {"pass": "upstreams/one"}}], 'routes'
4241597Shongzhidao@gmail.com        )
4251597Shongzhidao@gmail.com
426972Szelenkov@nginx.com    def test_routes_route_pass_absent(self):
4271596Szelenkov@nginx.com        assert 'error' in self.conf(
4281596Szelenkov@nginx.com            [{"match": {"method": "GET"}, "action": {}}], 'routes'
4291596Szelenkov@nginx.com        ), 'route pass absent configure'
430972Szelenkov@nginx.com
4311597Shongzhidao@gmail.com    def test_routes_route_pass_invalid(self):
4321597Shongzhidao@gmail.com        assert 'success' in self.conf(
4331597Shongzhidao@gmail.com            {
4341597Shongzhidao@gmail.com                "applications": {
4351597Shongzhidao@gmail.com                    "app": {
4362055Szelenkov@nginx.com                        "type": self.get_application_type(),
4371597Shongzhidao@gmail.com                        "processes": {"spare": 0},
4381597Shongzhidao@gmail.com                        "path": "/app",
4391597Shongzhidao@gmail.com                        "module": "wsgi",
4401597Shongzhidao@gmail.com                    }
4411597Shongzhidao@gmail.com                },
4421597Shongzhidao@gmail.com                "upstreams": {
4431597Shongzhidao@gmail.com                    "one": {
4441597Shongzhidao@gmail.com                        "servers": {
4451597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4461597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4471597Shongzhidao@gmail.com                        },
4481597Shongzhidao@gmail.com                    },
4491597Shongzhidao@gmail.com                    "two": {
4501597Shongzhidao@gmail.com                        "servers": {
4511597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4521597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4531597Shongzhidao@gmail.com                        },
4541597Shongzhidao@gmail.com                    },
4551597Shongzhidao@gmail.com                },
4561597Shongzhidao@gmail.com            }
4571597Shongzhidao@gmail.com        )
4581597Shongzhidao@gmail.com
4591597Shongzhidao@gmail.com        assert 'error' in self.conf(
4601597Shongzhidao@gmail.com            [{"action": {"pass": "blah"}}], 'routes'
4611597Shongzhidao@gmail.com        ), 'route pass invalid'
4621597Shongzhidao@gmail.com        assert 'error' in self.conf(
4631597Shongzhidao@gmail.com            [{"action": {"pass": "routes/blah"}}], 'routes'
4641597Shongzhidao@gmail.com        ), 'route pass routes invalid'
4651597Shongzhidao@gmail.com        assert 'error' in self.conf(
4661597Shongzhidao@gmail.com            [{"action": {"pass": "applications/blah"}}], 'routes'
4671597Shongzhidao@gmail.com        ), 'route pass applications invalid'
4681597Shongzhidao@gmail.com        assert 'error' in self.conf(
4691597Shongzhidao@gmail.com            [{"action": {"pass": "upstreams/blah"}}], 'routes'
4701597Shongzhidao@gmail.com        ), 'route pass upstreams invalid'
4711597Shongzhidao@gmail.com
4721654Szelenkov@nginx.com    def test_routes_action_unique(self, temp_dir):
4731596Szelenkov@nginx.com        assert 'success' in self.conf(
4741596Szelenkov@nginx.com            {
4751596Szelenkov@nginx.com                "listeners": {
4761596Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
4771596Szelenkov@nginx.com                    "*:7081": {"pass": "applications/app"},
4781596Szelenkov@nginx.com                },
4791596Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
4801596Szelenkov@nginx.com                "applications": {
4811596Szelenkov@nginx.com                    "app": {
4822055Szelenkov@nginx.com                        "type": self.get_application_type(),
4831596Szelenkov@nginx.com                        "processes": {"spare": 0},
4841596Szelenkov@nginx.com                        "path": "/app",
4851596Szelenkov@nginx.com                        "module": "wsgi",
4861596Szelenkov@nginx.com                    }
4871596Szelenkov@nginx.com                },
4881596Szelenkov@nginx.com            }
4891379Szelenkov@nginx.com        )
4901379Szelenkov@nginx.com
4911596Szelenkov@nginx.com        assert 'error' in self.conf(
4921654Szelenkov@nginx.com            {"proxy": "http://127.0.0.1:7081", "share": temp_dir},
4931596Szelenkov@nginx.com            'routes/0/action',
4941596Szelenkov@nginx.com        ), 'proxy share'
4951596Szelenkov@nginx.com        assert 'error' in self.conf(
496*2073Szelenkov@nginx.com            {
497*2073Szelenkov@nginx.com                "proxy": "http://127.0.0.1:7081",
498*2073Szelenkov@nginx.com                "pass": "applications/app",
499*2073Szelenkov@nginx.com            },
5001596Szelenkov@nginx.com            'routes/0/action',
5011596Szelenkov@nginx.com        ), 'proxy pass'
5021596Szelenkov@nginx.com        assert 'error' in self.conf(
503*2073Szelenkov@nginx.com            {"share": temp_dir, "pass": "applications/app"},
504*2073Szelenkov@nginx.com            'routes/0/action',
5051596Szelenkov@nginx.com        ), 'share pass'
5061379Szelenkov@nginx.com
507972Szelenkov@nginx.com    def test_routes_rules_two(self):
5081596Szelenkov@nginx.com        assert 'success' in self.conf(
5091596Szelenkov@nginx.com            [
5101596Szelenkov@nginx.com                {"match": {"method": "GET"}, "action": {"return": 200}},
5111596Szelenkov@nginx.com                {"match": {"method": "POST"}, "action": {"return": 201}},
5121596Szelenkov@nginx.com            ],
5131596Szelenkov@nginx.com            'routes',
5141596Szelenkov@nginx.com        ), 'rules two configure'
515972Szelenkov@nginx.com
5161596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'rules two match first'
5171596Szelenkov@nginx.com        assert self.post()['status'] == 201, 'rules two match second'
518972Szelenkov@nginx.com
519972Szelenkov@nginx.com    def test_routes_two(self):
5201596Szelenkov@nginx.com        assert 'success' in self.conf(
5211596Szelenkov@nginx.com            {
5221596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/first"}},
5231596Szelenkov@nginx.com                "routes": {
5241596Szelenkov@nginx.com                    "first": [
5251596Szelenkov@nginx.com                        {
5261596Szelenkov@nginx.com                            "match": {"method": "GET"},
5271596Szelenkov@nginx.com                            "action": {"pass": "routes/second"},
5281596Szelenkov@nginx.com                        }
5291596Szelenkov@nginx.com                    ],
5301596Szelenkov@nginx.com                    "second": [
5311596Szelenkov@nginx.com                        {
5321596Szelenkov@nginx.com                            "match": {"host": "localhost"},
5331596Szelenkov@nginx.com                            "action": {"return": 200},
5341596Szelenkov@nginx.com                        }
5351596Szelenkov@nginx.com                    ],
5361596Szelenkov@nginx.com                },
5371596Szelenkov@nginx.com                "applications": {},
5381596Szelenkov@nginx.com            }
5391596Szelenkov@nginx.com        ), 'routes two configure'
540972Szelenkov@nginx.com
5411596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes two'
542972Szelenkov@nginx.com
543972Szelenkov@nginx.com    def test_routes_match_host_positive(self):
5441308Szelenkov@nginx.com        self.route_match({"host": "localhost"})
545972Szelenkov@nginx.com
5461596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'localhost'
5471308Szelenkov@nginx.com        self.host('localhost.', 200)
5481308Szelenkov@nginx.com        self.host('localhost.', 200)
5491308Szelenkov@nginx.com        self.host('.localhost', 404)
5501308Szelenkov@nginx.com        self.host('www.localhost', 404)
5511308Szelenkov@nginx.com        self.host('localhost1', 404)
552972Szelenkov@nginx.com
5531596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
5541065Szelenkov@nginx.com    def test_routes_match_host_absent(self):
5551308Szelenkov@nginx.com        self.route_match({"host": "localhost"})
5561065Szelenkov@nginx.com
5571596Szelenkov@nginx.com        assert (
5581596Szelenkov@nginx.com            self.get(headers={'Connection': 'close'})['status'] == 400
5591596Szelenkov@nginx.com        ), 'match host absent'
5601065Szelenkov@nginx.com
561972Szelenkov@nginx.com    def test_routes_match_host_ipv4(self):
5621308Szelenkov@nginx.com        self.route_match({"host": "127.0.0.1"})
563972Szelenkov@nginx.com
5641308Szelenkov@nginx.com        self.host('127.0.0.1', 200)
5651308Szelenkov@nginx.com        self.host('127.0.0.1:7080', 200)
566972Szelenkov@nginx.com
567972Szelenkov@nginx.com    def test_routes_match_host_ipv6(self):
5681308Szelenkov@nginx.com        self.route_match({"host": "[::1]"})
569972Szelenkov@nginx.com
5701308Szelenkov@nginx.com        self.host('[::1]', 200)
5711308Szelenkov@nginx.com        self.host('[::1]:7080', 200)
572972Szelenkov@nginx.com
573972Szelenkov@nginx.com    def test_routes_match_host_positive_many(self):
5741308Szelenkov@nginx.com        self.route_match({"host": ["localhost", "example.com"]})
575972Szelenkov@nginx.com
5761596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'localhost'
5771308Szelenkov@nginx.com        self.host('example.com', 200)
578972Szelenkov@nginx.com
579972Szelenkov@nginx.com    def test_routes_match_host_positive_and_negative(self):
5801308Szelenkov@nginx.com        self.route_match({"host": ["*example.com", "!www.example.com"]})
581972Szelenkov@nginx.com
5821596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'localhost'
5831308Szelenkov@nginx.com        self.host('example.com', 200)
5841308Szelenkov@nginx.com        self.host('www.example.com', 404)
5851308Szelenkov@nginx.com        self.host('!www.example.com', 200)
586972Szelenkov@nginx.com
587972Szelenkov@nginx.com    def test_routes_match_host_positive_and_negative_wildcard(self):
5881308Szelenkov@nginx.com        self.route_match({"host": ["*example*", "!www.example*"]})
589972Szelenkov@nginx.com
5901308Szelenkov@nginx.com        self.host('example.com', 200)
5911308Szelenkov@nginx.com        self.host('www.example.com', 404)
592972Szelenkov@nginx.com
593972Szelenkov@nginx.com    def test_routes_match_host_case_insensitive(self):
5941308Szelenkov@nginx.com        self.route_match({"host": "Example.com"})
595972Szelenkov@nginx.com
5961308Szelenkov@nginx.com        self.host('example.com', 200)
5971308Szelenkov@nginx.com        self.host('EXAMPLE.COM', 200)
598972Szelenkov@nginx.com
599972Szelenkov@nginx.com    def test_routes_match_host_port(self):
6001308Szelenkov@nginx.com        self.route_match({"host": "example.com"})
601972Szelenkov@nginx.com
6021308Szelenkov@nginx.com        self.host('example.com:7080', 200)
603972Szelenkov@nginx.com
6041053Szelenkov@nginx.com    def test_routes_match_host_empty(self):
6051308Szelenkov@nginx.com        self.route_match({"host": ""})
6061053Szelenkov@nginx.com
6071308Szelenkov@nginx.com        self.host('', 200)
6081596Szelenkov@nginx.com        assert (
6091596Szelenkov@nginx.com            self.get(http_10=True, headers={})['status'] == 200
6101596Szelenkov@nginx.com        ), 'match host empty 2'
6111596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match host empty 3'
6121053Szelenkov@nginx.com
613972Szelenkov@nginx.com    def test_routes_match_uri_positive(self):
6141308Szelenkov@nginx.com        self.route_match({"uri": ["/blah", "/slash/"]})
615972Szelenkov@nginx.com
6161596Szelenkov@nginx.com        assert self.get()['status'] == 404, '/'
6171596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
6181596Szelenkov@nginx.com        assert self.get(url='/blah#foo')['status'] == 200, '/blah#foo'
6191596Szelenkov@nginx.com        assert self.get(url='/blah?var')['status'] == 200, '/blah?var'
6201596Szelenkov@nginx.com        assert self.get(url='//blah')['status'] == 200, '//blah'
6211596Szelenkov@nginx.com        assert self.get(url='/slash/foo/../')['status'] == 200, 'relative'
6221596Szelenkov@nginx.com        assert self.get(url='/slash/./')['status'] == 200, '/slash/./'
6231596Szelenkov@nginx.com        assert self.get(url='/slash//.//')['status'] == 200, 'adjacent slashes'
6241596Szelenkov@nginx.com        assert self.get(url='/%')['status'] == 400, 'percent'
6251596Szelenkov@nginx.com        assert self.get(url='/%1')['status'] == 400, 'percent digit'
6261596Szelenkov@nginx.com        assert self.get(url='/%A')['status'] == 400, 'percent letter'
6271596Szelenkov@nginx.com        assert self.get(url='/slash/.?args')['status'] == 200, 'dot args'
6281596Szelenkov@nginx.com        assert self.get(url='/slash/.#frag')['status'] == 200, 'dot frag'
6291596Szelenkov@nginx.com        assert (
6301596Szelenkov@nginx.com            self.get(url='/slash/foo/..?args')['status'] == 200
6311596Szelenkov@nginx.com        ), 'dot dot args'
6321596Szelenkov@nginx.com        assert (
6331596Szelenkov@nginx.com            self.get(url='/slash/foo/..#frag')['status'] == 200
6341596Szelenkov@nginx.com        ), 'dot dot frag'
6351596Szelenkov@nginx.com        assert self.get(url='/slash/.')['status'] == 200, 'trailing dot'
6361596Szelenkov@nginx.com        assert (
6371596Szelenkov@nginx.com            self.get(url='/slash/foo/..')['status'] == 200
6381596Szelenkov@nginx.com        ), 'trailing dot dot'
639972Szelenkov@nginx.com
640972Szelenkov@nginx.com    def test_routes_match_uri_case_sensitive(self):
6411308Szelenkov@nginx.com        self.route_match({"uri": "/BLAH"})
6421308Szelenkov@nginx.com
6431596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404, '/blah'
6441596Szelenkov@nginx.com        assert self.get(url='/BlaH')['status'] == 404, '/BlaH'
6451596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 200, '/BLAH'
6461308Szelenkov@nginx.com
6471308Szelenkov@nginx.com    def test_routes_match_uri_normalize(self):
6481308Szelenkov@nginx.com        self.route_match({"uri": "/blah"})
649972Szelenkov@nginx.com
6501596Szelenkov@nginx.com        assert self.get(url='/%62%6c%61%68')['status'] == 200, 'normalize'
651972Szelenkov@nginx.com
6521053Szelenkov@nginx.com    def test_routes_match_empty_array(self):
6531308Szelenkov@nginx.com        self.route_match({"uri": []})
6541053Szelenkov@nginx.com
6551596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, 'empty array'
6561053Szelenkov@nginx.com
6571053Szelenkov@nginx.com    def test_routes_reconfigure(self):
6581596Szelenkov@nginx.com        assert 'success' in self.conf([], 'routes'), 'redefine'
6591596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'redefine request'
6601053Szelenkov@nginx.com
6611596Szelenkov@nginx.com        assert 'success' in self.conf(
6621596Szelenkov@nginx.com            [{"action": {"return": 200}}], 'routes'
6631596Szelenkov@nginx.com        ), 'redefine 2'
6641596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 2'
6651596Szelenkov@nginx.com
6661596Szelenkov@nginx.com        assert 'success' in self.conf([], 'routes'), 'redefine 3'
6671596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'redefine request 3'
6681053Szelenkov@nginx.com
6691596Szelenkov@nginx.com        assert 'success' in self.conf(
6701596Szelenkov@nginx.com            {
6711596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/main"}},
6721596Szelenkov@nginx.com                "routes": {"main": [{"action": {"return": 200}}]},
6731596Szelenkov@nginx.com                "applications": {},
6741596Szelenkov@nginx.com            }
6751596Szelenkov@nginx.com        ), 'redefine 4'
6761596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 4'
6771053Szelenkov@nginx.com
6781596Szelenkov@nginx.com        assert 'success' in self.conf_delete('routes/main/0'), 'redefine 5'
6791596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'redefine request 5'
6801053Szelenkov@nginx.com
6811596Szelenkov@nginx.com        assert 'success' in self.conf_post(
6821596Szelenkov@nginx.com            {"action": {"return": 200}}, 'routes/main'
6831596Szelenkov@nginx.com        ), 'redefine 6'
6841596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 6'
6851063Szelenkov@nginx.com
6861596Szelenkov@nginx.com        assert 'error' in self.conf(
6871596Szelenkov@nginx.com            {"action": {"return": 200}}, 'routes/main/2'
6881596Szelenkov@nginx.com        ), 'redefine 7'
6891596Szelenkov@nginx.com        assert 'success' in self.conf(
6901596Szelenkov@nginx.com            {"action": {"return": 201}}, 'routes/main/1'
6911596Szelenkov@nginx.com        ), 'redefine 8'
6921596Szelenkov@nginx.com
6931596Szelenkov@nginx.com        assert len(self.conf_get('routes/main')) == 2, 'redefine conf 8'
6941596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 8'
6951063Szelenkov@nginx.com
6961053Szelenkov@nginx.com    def test_routes_edit(self):
6971308Szelenkov@nginx.com        self.route_match({"method": "GET"})
6981053Szelenkov@nginx.com
6991596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes edit GET'
7001596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'routes edit POST'
7011053Szelenkov@nginx.com
7021596Szelenkov@nginx.com        assert 'success' in self.conf_post(
703*2073Szelenkov@nginx.com            {"match": {"method": "POST"}, "action": {"return": 200}},
704*2073Szelenkov@nginx.com            'routes',
7051596Szelenkov@nginx.com        ), 'routes edit configure 2'
7061596Szelenkov@nginx.com        assert 'GET' == self.conf_get(
7071596Szelenkov@nginx.com            'routes/0/match/method'
7081596Szelenkov@nginx.com        ), 'routes edit configure 2 check'
7091596Szelenkov@nginx.com        assert 'POST' == self.conf_get(
7101596Szelenkov@nginx.com            'routes/1/match/method'
7111596Szelenkov@nginx.com        ), 'routes edit configure 2 check 2'
7121053Szelenkov@nginx.com
7131596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes edit GET 2'
7141596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 2'
7151053Szelenkov@nginx.com
7161596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7171596Szelenkov@nginx.com            'routes/0'
7181596Szelenkov@nginx.com        ), 'routes edit configure 3'
7191053Szelenkov@nginx.com
7201596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 3'
7211596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 3'
7221053Szelenkov@nginx.com
7231596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7241596Szelenkov@nginx.com            'routes/1'
7251596Szelenkov@nginx.com        ), 'routes edit configure invalid'
7261596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7271596Szelenkov@nginx.com            'routes/-1'
7281596Szelenkov@nginx.com        ), 'routes edit configure invalid 2'
7291596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7301596Szelenkov@nginx.com            'routes/blah'
7311596Szelenkov@nginx.com        ), 'routes edit configure invalid 3'
7321053Szelenkov@nginx.com
7331596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 4'
7341596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 4'
7351053Szelenkov@nginx.com
7361596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7371596Szelenkov@nginx.com            'routes/0'
7381596Szelenkov@nginx.com        ), 'routes edit configure 5'
7391053Szelenkov@nginx.com
7401596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 5'
7411596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'routes edit POST 5'
7421053Szelenkov@nginx.com
7431596Szelenkov@nginx.com        assert 'success' in self.conf_post(
744*2073Szelenkov@nginx.com            {"match": {"method": "POST"}, "action": {"return": 200}},
745*2073Szelenkov@nginx.com            'routes',
7461596Szelenkov@nginx.com        ), 'routes edit configure 6'
7471053Szelenkov@nginx.com
7481596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 6'
7491596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 6'
7501053Szelenkov@nginx.com
7511596Szelenkov@nginx.com        assert 'success' in self.conf(
7521596Szelenkov@nginx.com            {
7531596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/main"}},
7541596Szelenkov@nginx.com                "routes": {"main": [{"action": {"return": 200}}]},
7551596Szelenkov@nginx.com                "applications": {},
7561596Szelenkov@nginx.com            }
7571596Szelenkov@nginx.com        ), 'route edit configure 7'
7581053Szelenkov@nginx.com
7591596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7601596Szelenkov@nginx.com            'routes/0'
7611596Szelenkov@nginx.com        ), 'routes edit configure invalid 4'
7621596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7631596Szelenkov@nginx.com            'routes/main'
7641596Szelenkov@nginx.com        ), 'routes edit configure invalid 5'
7651053Szelenkov@nginx.com
7661596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes edit GET 7'
7671053Szelenkov@nginx.com
7681596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7691596Szelenkov@nginx.com            'listeners/*:7080'
7701596Szelenkov@nginx.com        ), 'route edit configure 8'
7711596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7721596Szelenkov@nginx.com            'routes/main'
7731596Szelenkov@nginx.com        ), 'route edit configure 9'
7741053Szelenkov@nginx.com
7751736Szelenkov@nginx.com    def test_match_edit(self, skip_alert):
7761596Szelenkov@nginx.com        skip_alert(r'failed to apply new conf')
7771053Szelenkov@nginx.com
7781308Szelenkov@nginx.com        self.route_match({"method": ["GET", "POST"]})
7791053Szelenkov@nginx.com
7801596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET'
7811596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'match edit POST'
7821596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT'
7831053Szelenkov@nginx.com
7841596Szelenkov@nginx.com        assert 'success' in self.conf_post(
7851596Szelenkov@nginx.com            '\"PUT\"', 'routes/0/match/method'
7861596Szelenkov@nginx.com        ), 'match edit configure 2'
7871596Szelenkov@nginx.com        assert ['GET', 'POST', 'PUT'] == self.conf_get(
7881596Szelenkov@nginx.com            'routes/0/match/method'
7891596Szelenkov@nginx.com        ), 'match edit configure 2 check'
7901053Szelenkov@nginx.com
7911596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 2'
7921596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'match edit POST 2'
7931596Szelenkov@nginx.com        assert self.put()['status'] == 200, 'match edit PUT 2'
7941053Szelenkov@nginx.com
7951596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7961596Szelenkov@nginx.com            'routes/0/match/method/1'
7971596Szelenkov@nginx.com        ), 'match edit configure 3'
7981596Szelenkov@nginx.com        assert ['GET', 'PUT'] == self.conf_get(
7991596Szelenkov@nginx.com            'routes/0/match/method'
8001596Szelenkov@nginx.com        ), 'match edit configure 3 check'
8011053Szelenkov@nginx.com
8021596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 3'
8031596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 3'
8041596Szelenkov@nginx.com        assert self.put()['status'] == 200, 'match edit PUT 3'
8051053Szelenkov@nginx.com
8061596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
8071596Szelenkov@nginx.com            'routes/0/match/method/1'
8081596Szelenkov@nginx.com        ), 'match edit configure 4'
8091596Szelenkov@nginx.com        assert ['GET'] == self.conf_get(
8101596Szelenkov@nginx.com            'routes/0/match/method'
8111596Szelenkov@nginx.com        ), 'match edit configure 4 check'
8121053Szelenkov@nginx.com
8131596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 4'
8141596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 4'
8151596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT 4'
8161053Szelenkov@nginx.com
8171596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8181596Szelenkov@nginx.com            'routes/0/match/method/1'
8191596Szelenkov@nginx.com        ), 'match edit configure invalid'
8201596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8211596Szelenkov@nginx.com            'routes/0/match/method/-1'
8221596Szelenkov@nginx.com        ), 'match edit configure invalid 2'
8231596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8241596Szelenkov@nginx.com            'routes/0/match/method/blah'
8251596Szelenkov@nginx.com        ), 'match edit configure invalid 3'
8261596Szelenkov@nginx.com        assert ['GET'] == self.conf_get(
8271596Szelenkov@nginx.com            'routes/0/match/method'
8281596Szelenkov@nginx.com        ), 'match edit configure 5 check'
8291053Szelenkov@nginx.com
8301596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 5'
8311596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 5'
8321596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT 5'
8331596Szelenkov@nginx.com
8341596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
8351596Szelenkov@nginx.com            'routes/0/match/method/0'
8361596Szelenkov@nginx.com        ), 'match edit configure 6'
8371596Szelenkov@nginx.com        assert [] == self.conf_get(
8381596Szelenkov@nginx.com            'routes/0/match/method'
8391596Szelenkov@nginx.com        ), 'match edit configure 6 check'
8401053Szelenkov@nginx.com
8411596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 6'
8421596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'match edit POST 6'
8431596Szelenkov@nginx.com        assert self.put()['status'] == 200, 'match edit PUT 6'
8441053Szelenkov@nginx.com
8451596Szelenkov@nginx.com        assert 'success' in self.conf(
8461596Szelenkov@nginx.com            '"GET"', 'routes/0/match/method'
8471596Szelenkov@nginx.com        ), 'match edit configure 7'
8481053Szelenkov@nginx.com
8491596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 7'
8501596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 7'
8511596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT 7'
8521053Szelenkov@nginx.com
8531596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8541596Szelenkov@nginx.com            'routes/0/match/method/0'
8551596Szelenkov@nginx.com        ), 'match edit configure invalid 5'
8561596Szelenkov@nginx.com        assert 'error' in self.conf(
8571596Szelenkov@nginx.com            {}, 'routes/0/action'
8581596Szelenkov@nginx.com        ), 'match edit configure invalid 6'
8591053Szelenkov@nginx.com
8601596Szelenkov@nginx.com        assert 'success' in self.conf(
8611596Szelenkov@nginx.com            {}, 'routes/0/match'
8621596Szelenkov@nginx.com        ), 'match edit configure 8'
8631053Szelenkov@nginx.com
8641596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 8'
8651053Szelenkov@nginx.com
866972Szelenkov@nginx.com    def test_routes_match_rules(self):
8671308Szelenkov@nginx.com        self.route_match({"method": "GET", "host": "localhost", "uri": "/"})
868972Szelenkov@nginx.com
8691596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes match rules'
870972Szelenkov@nginx.com
871972Szelenkov@nginx.com    def test_routes_loop(self):
8721596Szelenkov@nginx.com        assert 'success' in self.route(
8731596Szelenkov@nginx.com            {"match": {"uri": "/"}, "action": {"pass": "routes"}}
8741596Szelenkov@nginx.com        ), 'routes loop configure'
875972Szelenkov@nginx.com
8761596Szelenkov@nginx.com        assert self.get()['status'] == 500, 'routes loop'
877972Szelenkov@nginx.com
8781066Szelenkov@nginx.com    def test_routes_match_headers(self):
8791308Szelenkov@nginx.com        self.route_match({"headers": {"host": "localhost"}})
8801066Szelenkov@nginx.com
8811596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match headers'
8821308Szelenkov@nginx.com        self.host('Localhost', 200)
8831308Szelenkov@nginx.com        self.host('localhost.com', 404)
8841308Szelenkov@nginx.com        self.host('llocalhost', 404)
8851308Szelenkov@nginx.com        self.host('host', 404)
8861066Szelenkov@nginx.com
8871066Szelenkov@nginx.com    def test_routes_match_headers_multiple(self):
8881308Szelenkov@nginx.com        self.route_match({"headers": {"host": "localhost", "x-blah": "test"}})
8891066Szelenkov@nginx.com
8901596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match headers multiple'
8911596Szelenkov@nginx.com        assert (
8921066Szelenkov@nginx.com            self.get(
8931066Szelenkov@nginx.com                headers={
8941066Szelenkov@nginx.com                    "Host": "localhost",
8951066Szelenkov@nginx.com                    "X-blah": "test",
8961066Szelenkov@nginx.com                    "Connection": "close",
8971066Szelenkov@nginx.com                }
8981596Szelenkov@nginx.com            )['status']
8991596Szelenkov@nginx.com            == 200
9001596Szelenkov@nginx.com        ), 'match headers multiple 2'
9011066Szelenkov@nginx.com
9021596Szelenkov@nginx.com        assert (
9031066Szelenkov@nginx.com            self.get(
9041066Szelenkov@nginx.com                headers={
9051066Szelenkov@nginx.com                    "Host": "localhost",
9061066Szelenkov@nginx.com                    "X-blah": "",
9071066Szelenkov@nginx.com                    "Connection": "close",
9081066Szelenkov@nginx.com                }
9091596Szelenkov@nginx.com            )['status']
9101596Szelenkov@nginx.com            == 404
9111596Szelenkov@nginx.com        ), 'match headers multiple 3'
9121066Szelenkov@nginx.com
9131066Szelenkov@nginx.com    def test_routes_match_headers_multiple_values(self):
9141308Szelenkov@nginx.com        self.route_match({"headers": {"x-blah": "test"}})
9151066Szelenkov@nginx.com
9161596Szelenkov@nginx.com        assert (
9171066Szelenkov@nginx.com            self.get(
9181066Szelenkov@nginx.com                headers={
9191066Szelenkov@nginx.com                    "Host": "localhost",
9201066Szelenkov@nginx.com                    "X-blah": ["test", "test", "test"],
9211066Szelenkov@nginx.com                    "Connection": "close",
9221066Szelenkov@nginx.com                }
9231596Szelenkov@nginx.com            )['status']
9241596Szelenkov@nginx.com            == 200
9251596Szelenkov@nginx.com        ), 'match headers multiple values'
9261596Szelenkov@nginx.com        assert (
9271066Szelenkov@nginx.com            self.get(
9281066Szelenkov@nginx.com                headers={
9291066Szelenkov@nginx.com                    "Host": "localhost",
9301066Szelenkov@nginx.com                    "X-blah": ["test", "blah", "test"],
9311066Szelenkov@nginx.com                    "Connection": "close",
9321066Szelenkov@nginx.com                }
9331596Szelenkov@nginx.com            )['status']
9341596Szelenkov@nginx.com            == 404
9351596Szelenkov@nginx.com        ), 'match headers multiple values 2'
9361596Szelenkov@nginx.com        assert (
9371066Szelenkov@nginx.com            self.get(
9381066Szelenkov@nginx.com                headers={
9391066Szelenkov@nginx.com                    "Host": "localhost",
9401066Szelenkov@nginx.com                    "X-blah": ["test", "", "test"],
9411066Szelenkov@nginx.com                    "Connection": "close",
9421066Szelenkov@nginx.com                }
9431596Szelenkov@nginx.com            )['status']
9441596Szelenkov@nginx.com            == 404
9451596Szelenkov@nginx.com        ), 'match headers multiple values 3'
9461066Szelenkov@nginx.com
9471066Szelenkov@nginx.com    def test_routes_match_headers_multiple_rules(self):
9481308Szelenkov@nginx.com        self.route_match({"headers": {"x-blah": ["test", "blah"]}})
9491066Szelenkov@nginx.com
9501596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match headers multiple rules'
9511596Szelenkov@nginx.com        assert (
9521066Szelenkov@nginx.com            self.get(
9531066Szelenkov@nginx.com                headers={
9541066Szelenkov@nginx.com                    "Host": "localhost",
9551066Szelenkov@nginx.com                    "X-blah": "test",
9561066Szelenkov@nginx.com                    "Connection": "close",
9571066Szelenkov@nginx.com                }
9581596Szelenkov@nginx.com            )['status']
9591596Szelenkov@nginx.com            == 200
9601596Szelenkov@nginx.com        ), 'match headers multiple rules 2'
9611596Szelenkov@nginx.com        assert (
9621066Szelenkov@nginx.com            self.get(
9631066Szelenkov@nginx.com                headers={
9641066Szelenkov@nginx.com                    "Host": "localhost",
9651066Szelenkov@nginx.com                    "X-blah": "blah",
9661066Szelenkov@nginx.com                    "Connection": "close",
9671066Szelenkov@nginx.com                }
9681596Szelenkov@nginx.com            )['status']
9691596Szelenkov@nginx.com            == 200
9701596Szelenkov@nginx.com        ), 'match headers multiple rules 3'
9711596Szelenkov@nginx.com        assert (
9721066Szelenkov@nginx.com            self.get(
9731066Szelenkov@nginx.com                headers={
9741066Szelenkov@nginx.com                    "Host": "localhost",
9751066Szelenkov@nginx.com                    "X-blah": ["test", "blah", "test"],
9761066Szelenkov@nginx.com                    "Connection": "close",
9771066Szelenkov@nginx.com                }
9781596Szelenkov@nginx.com            )['status']
9791596Szelenkov@nginx.com            == 200
9801596Szelenkov@nginx.com        ), 'match headers multiple rules 4'
9811066Szelenkov@nginx.com
9821596Szelenkov@nginx.com        assert (
9831066Szelenkov@nginx.com            self.get(
9841066Szelenkov@nginx.com                headers={
9851066Szelenkov@nginx.com                    "Host": "localhost",
9861066Szelenkov@nginx.com                    "X-blah": ["blah", ""],
9871066Szelenkov@nginx.com                    "Connection": "close",
9881066Szelenkov@nginx.com                }
9891596Szelenkov@nginx.com            )['status']
9901596Szelenkov@nginx.com            == 404
9911596Szelenkov@nginx.com        ), 'match headers multiple rules 5'
9921066Szelenkov@nginx.com
9931066Szelenkov@nginx.com    def test_routes_match_headers_case_insensitive(self):
9941308Szelenkov@nginx.com        self.route_match({"headers": {"X-BLAH": "TEST"}})
9951066Szelenkov@nginx.com
9961596Szelenkov@nginx.com        assert (
9971066Szelenkov@nginx.com            self.get(
9981066Szelenkov@nginx.com                headers={
9991066Szelenkov@nginx.com                    "Host": "localhost",
10001066Szelenkov@nginx.com                    "x-blah": "test",
10011066Szelenkov@nginx.com                    "Connection": "close",
10021066Szelenkov@nginx.com                }
10031596Szelenkov@nginx.com            )['status']
10041596Szelenkov@nginx.com            == 200
10051596Szelenkov@nginx.com        ), 'match headers case insensitive'
10061066Szelenkov@nginx.com
10071066Szelenkov@nginx.com    def test_routes_match_headers_invalid(self):
10081308Szelenkov@nginx.com        self.route_match_invalid({"headers": ["blah"]})
10091308Szelenkov@nginx.com        self.route_match_invalid({"headers": {"foo": ["bar", {}]}})
10101308Szelenkov@nginx.com        self.route_match_invalid({"headers": {"": "blah"}})
10111066Szelenkov@nginx.com
10121066Szelenkov@nginx.com    def test_routes_match_headers_empty_rule(self):
10131308Szelenkov@nginx.com        self.route_match({"headers": {"host": ""}})
10141066Szelenkov@nginx.com
10151596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'localhost'
10161308Szelenkov@nginx.com        self.host('', 200)
10171066Szelenkov@nginx.com
10181066Szelenkov@nginx.com    def test_routes_match_headers_empty(self):
10191308Szelenkov@nginx.com        self.route_match({"headers": {}})
10201596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'empty'
10211066Szelenkov@nginx.com
10221308Szelenkov@nginx.com        self.route_match({"headers": []})
10231596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'empty 2'
10241066Szelenkov@nginx.com
10251066Szelenkov@nginx.com    def test_routes_match_headers_rule_array_empty(self):
10261308Szelenkov@nginx.com        self.route_match({"headers": {"blah": []}})
10271066Szelenkov@nginx.com
10281596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'array empty'
10291596Szelenkov@nginx.com        assert (
10301066Szelenkov@nginx.com            self.get(
10311066Szelenkov@nginx.com                headers={
10321066Szelenkov@nginx.com                    "Host": "localhost",
10331066Szelenkov@nginx.com                    "blah": "foo",
10341066Szelenkov@nginx.com                    "Connection": "close",
10351066Szelenkov@nginx.com                }
10361596Szelenkov@nginx.com            )['status']
10371596Szelenkov@nginx.com            == 200
10381596Szelenkov@nginx.com        ), 'match headers rule array empty 2'
10391066Szelenkov@nginx.com
10401066Szelenkov@nginx.com    def test_routes_match_headers_array(self):
10411308Szelenkov@nginx.com        self.route_match(
10421308Szelenkov@nginx.com            {
10431308Szelenkov@nginx.com                "headers": [
10441308Szelenkov@nginx.com                    {"x-header1": "foo*"},
10451308Szelenkov@nginx.com                    {"x-header2": "bar"},
10461308Szelenkov@nginx.com                    {"x-header3": ["foo", "bar"]},
10471308Szelenkov@nginx.com                    {"x-header1": "bar", "x-header4": "foo"},
10481308Szelenkov@nginx.com                ]
10491308Szelenkov@nginx.com            }
10501066Szelenkov@nginx.com        )
10511066Szelenkov@nginx.com
10521875Szelenkov@nginx.com        def check_headers(hds):
10531875Szelenkov@nginx.com            hds = dict({"Host": "localhost", "Connection": "close"}, **hds)
1054*2073Szelenkov@nginx.com            assert self.get(headers=hds)['status'] == 200, 'headers array match'
10551875Szelenkov@nginx.com
10561875Szelenkov@nginx.com        def check_headers_404(hds):
10571875Szelenkov@nginx.com            hds = dict({"Host": "localhost", "Connection": "close"}, **hds)
10581875Szelenkov@nginx.com            assert (
10591875Szelenkov@nginx.com                self.get(headers=hds)['status'] == 404
10601875Szelenkov@nginx.com            ), 'headers array no match'
10611875Szelenkov@nginx.com
10621596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match headers array'
10631875Szelenkov@nginx.com        check_headers({"x-header1": "foo123"})
10641875Szelenkov@nginx.com        check_headers({"x-header2": "bar"})
10651875Szelenkov@nginx.com        check_headers({"x-header3": "bar"})
10661875Szelenkov@nginx.com        check_headers_404({"x-header1": "bar"})
10671875Szelenkov@nginx.com        check_headers({"x-header1": "bar", "x-header4": "foo"})
10681066Szelenkov@nginx.com
10691596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
10701596Szelenkov@nginx.com            'routes/0/match/headers/1'
10711596Szelenkov@nginx.com        ), 'match headers array configure 2'
10721066Szelenkov@nginx.com
10731875Szelenkov@nginx.com        check_headers_404({"x-header2": "bar"})
10741875Szelenkov@nginx.com        check_headers({"x-header3": "foo"})
10751017Szelenkov@nginx.com
10761067Szelenkov@nginx.com    def test_routes_match_arguments(self):
10771308Szelenkov@nginx.com        self.route_match({"arguments": {"foo": "bar"}})
10781067Szelenkov@nginx.com
10791596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'args'
10801596Szelenkov@nginx.com        assert self.get(url='/?foo=bar')['status'] == 200, 'args 2'
10811596Szelenkov@nginx.com