xref: /unit/test/test_routing.py (revision 1875)
11475Saxel.duch@nginx.com# -*- coding: utf-8 -*-
21596Szelenkov@nginx.comimport pytest
31848Szelenkov@nginx.com
41019Szelenkov@nginx.comfrom unit.applications.proto import TestApplicationProto
51730Szelenkov@nginx.comfrom unit.option import option
6972Szelenkov@nginx.com
71017Szelenkov@nginx.com
81019Szelenkov@nginx.comclass TestRouting(TestApplicationProto):
91467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
10972Szelenkov@nginx.com
111596Szelenkov@nginx.com    def setup_method(self):
121596Szelenkov@nginx.com        assert 'success' in self.conf(
131596Szelenkov@nginx.com            {
141596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes"}},
151596Szelenkov@nginx.com                "routes": [
161596Szelenkov@nginx.com                    {"match": {"method": "GET"}, "action": {"return": 200},}
171596Szelenkov@nginx.com                ],
181596Szelenkov@nginx.com                "applications": {},
191596Szelenkov@nginx.com            }
201596Szelenkov@nginx.com        ), 'routing configure'
21972Szelenkov@nginx.com
221101Szelenkov@nginx.com    def route(self, route):
231101Szelenkov@nginx.com        return self.conf([route], 'routes')
241101Szelenkov@nginx.com
251308Szelenkov@nginx.com    def route_match(self, match):
261596Szelenkov@nginx.com        assert 'success' in self.route(
271596Szelenkov@nginx.com            {"match": match, "action": {"return": 200}}
281596Szelenkov@nginx.com        ), 'route match configure'
291037Szelenkov@nginx.com
301308Szelenkov@nginx.com    def route_match_invalid(self, match):
311596Szelenkov@nginx.com        assert 'error' in self.route(
321596Szelenkov@nginx.com            {"match": match, "action": {"return": 200}}
331596Szelenkov@nginx.com        ), 'route match configure invalid'
341037Szelenkov@nginx.com
351308Szelenkov@nginx.com    def host(self, host, status):
361596Szelenkov@nginx.com        assert (
371596Szelenkov@nginx.com            self.get(headers={'Host': host, 'Connection': 'close'})['status']
381596Szelenkov@nginx.com            == status
391596Szelenkov@nginx.com        ), 'match host'
401308Szelenkov@nginx.com
411308Szelenkov@nginx.com    def cookie(self, cookie, status):
421596Szelenkov@nginx.com        assert (
431308Szelenkov@nginx.com            self.get(
441308Szelenkov@nginx.com                headers={
451308Szelenkov@nginx.com                    'Host': 'localhost',
461308Szelenkov@nginx.com                    'Cookie': cookie,
471308Szelenkov@nginx.com                    'Connection': 'close',
481308Szelenkov@nginx.com                },
491596Szelenkov@nginx.com            )['status']
501596Szelenkov@nginx.com            == status
511596Szelenkov@nginx.com        ), 'match cookie'
521037Szelenkov@nginx.com
531308Szelenkov@nginx.com    def test_routes_match_method_positive(self):
541596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
551596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
561308Szelenkov@nginx.com
571308Szelenkov@nginx.com    def test_routes_match_method_positive_many(self):
581308Szelenkov@nginx.com        self.route_match({"method": ["GET", "POST"]})
591308Szelenkov@nginx.com
601596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
611596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'POST'
621596Szelenkov@nginx.com        assert self.delete()['status'] == 404, 'DELETE'
631308Szelenkov@nginx.com
641308Szelenkov@nginx.com    def test_routes_match_method_negative(self):
651308Szelenkov@nginx.com        self.route_match({"method": "!GET"})
661308Szelenkov@nginx.com
671596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'GET'
681596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'POST'
691308Szelenkov@nginx.com
701308Szelenkov@nginx.com    def test_routes_match_method_negative_many(self):
711308Szelenkov@nginx.com        self.route_match({"method": ["!GET", "!POST"]})
721308Szelenkov@nginx.com
731596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'GET'
741596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
751596Szelenkov@nginx.com        assert self.delete()['status'] == 200, 'DELETE'
761308Szelenkov@nginx.com
771308Szelenkov@nginx.com    def test_routes_match_method_wildcard_left(self):
781308Szelenkov@nginx.com        self.route_match({"method": "*ET"})
791308Szelenkov@nginx.com
801596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
811596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
821037Szelenkov@nginx.com
831308Szelenkov@nginx.com    def test_routes_match_method_wildcard_right(self):
841308Szelenkov@nginx.com        self.route_match({"method": "GE*"})
851308Szelenkov@nginx.com
861596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
871596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
881308Szelenkov@nginx.com
891308Szelenkov@nginx.com    def test_routes_match_method_wildcard_left_right(self):
901308Szelenkov@nginx.com        self.route_match({"method": "*GET*"})
911308Szelenkov@nginx.com
921596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
931596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'POST'
941308Szelenkov@nginx.com
951308Szelenkov@nginx.com    def test_routes_match_method_wildcard(self):
961308Szelenkov@nginx.com        self.route_match({"method": "*"})
971308Szelenkov@nginx.com
981596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
991308Szelenkov@nginx.com
1001308Szelenkov@nginx.com    def test_routes_match_invalid(self):
1011308Szelenkov@nginx.com        self.route_match_invalid({"method": "**"})
1021508Saxel.duch@nginx.com
1031508Saxel.duch@nginx.com    def test_routes_match_valid(self):
1041508Saxel.duch@nginx.com        self.route_match({"method": "blah*"})
1051508Saxel.duch@nginx.com        self.route_match({"host": "*blah*blah"})
1061508Saxel.duch@nginx.com        self.route_match({"host": "blah*blah*blah"})
1071508Saxel.duch@nginx.com        self.route_match({"host": "blah*blah*"})
1081508Saxel.duch@nginx.com
1091508Saxel.duch@nginx.com    def test_routes_match_empty_exact(self):
1101508Saxel.duch@nginx.com        self.route_match({"uri": ""})
1111596Szelenkov@nginx.com        assert self.get()['status'] == 404
1121508Saxel.duch@nginx.com
1131508Saxel.duch@nginx.com        self.route_match({"uri": "/"})
1141596Szelenkov@nginx.com        assert self.get()['status'] == 200
1151596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404
1161508Saxel.duch@nginx.com
1171508Saxel.duch@nginx.com    def test_routes_match_negative(self):
1181508Saxel.duch@nginx.com        self.route_match({"uri": "!"})
1191633Svbart@nginx.com        assert self.get()['status'] == 200
1201633Svbart@nginx.com
1211633Svbart@nginx.com        self.route_match({"uri": "!*"})
1221596Szelenkov@nginx.com        assert self.get()['status'] == 404
1231508Saxel.duch@nginx.com
1241508Saxel.duch@nginx.com        self.route_match({"uri": "!/"})
1251596Szelenkov@nginx.com        assert self.get()['status'] == 404
1261596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200
1271508Saxel.duch@nginx.com
1281508Saxel.duch@nginx.com        self.route_match({"uri": "!*blah"})
1291596Szelenkov@nginx.com        assert self.get()['status'] == 200
1301596Szelenkov@nginx.com        assert self.get(url='/bla')['status'] == 200
1311596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404
1321596Szelenkov@nginx.com        assert self.get(url='/blah1')['status'] == 200
1331508Saxel.duch@nginx.com
1341508Saxel.duch@nginx.com        self.route_match({"uri": "!/blah*1*"})
1351596Szelenkov@nginx.com        assert self.get()['status'] == 200
1361596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200
1371596Szelenkov@nginx.com        assert self.get(url='/blah1')['status'] == 404
1381596Szelenkov@nginx.com        assert self.get(url='/blah12')['status'] == 404
1391596Szelenkov@nginx.com        assert self.get(url='/blah2')['status'] == 200
1401308Szelenkov@nginx.com
1411308Szelenkov@nginx.com    def test_routes_match_wildcard_middle(self):
1421308Szelenkov@nginx.com        self.route_match({"host": "ex*le"})
1431308Szelenkov@nginx.com
1441308Szelenkov@nginx.com        self.host('example', 200)
1451308Szelenkov@nginx.com        self.host('www.example', 404)
1461308Szelenkov@nginx.com        self.host('example.com', 404)
1471308Szelenkov@nginx.com        self.host('exampl', 404)
1481037Szelenkov@nginx.com
149972Szelenkov@nginx.com    def test_routes_match_method_case_insensitive(self):
1501308Szelenkov@nginx.com        self.route_match({"method": "get"})
151972Szelenkov@nginx.com
1521596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
153972Szelenkov@nginx.com
1541037Szelenkov@nginx.com    def test_routes_match_wildcard_left_case_insensitive(self):
1551308Szelenkov@nginx.com        self.route_match({"method": "*get"})
1561596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1571037Szelenkov@nginx.com
1581308Szelenkov@nginx.com        self.route_match({"method": "*et"})
1591596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1601037Szelenkov@nginx.com
1611037Szelenkov@nginx.com    def test_routes_match_wildcard_middle_case_insensitive(self):
1621308Szelenkov@nginx.com        self.route_match({"method": "g*t"})
1631037Szelenkov@nginx.com
1641596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1651037Szelenkov@nginx.com
1661037Szelenkov@nginx.com    def test_routes_match_wildcard_right_case_insensitive(self):
1671308Szelenkov@nginx.com        self.route_match({"method": "get*"})
1681596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1691037Szelenkov@nginx.com
1701308Szelenkov@nginx.com        self.route_match({"method": "ge*"})
1711596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1721037Szelenkov@nginx.com
1731037Szelenkov@nginx.com    def test_routes_match_wildcard_substring_case_insensitive(self):
1741308Szelenkov@nginx.com        self.route_match({"method": "*et*"})
1751037Szelenkov@nginx.com
1761596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'GET'
1771037Szelenkov@nginx.com
1781037Szelenkov@nginx.com    def test_routes_match_wildcard_left_case_sensitive(self):
1791308Szelenkov@nginx.com        self.route_match({"uri": "*blah"})
1801037Szelenkov@nginx.com
1811596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
1821596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
1831037Szelenkov@nginx.com
1841037Szelenkov@nginx.com    def test_routes_match_wildcard_middle_case_sensitive(self):
1851308Szelenkov@nginx.com        self.route_match({"uri": "/b*h"})
1861037Szelenkov@nginx.com
1871596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
1881596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
1891037Szelenkov@nginx.com
1901510Saxel.duch@nginx.com    def test_route_match_wildcards_ordered(self):
1911510Saxel.duch@nginx.com        self.route_match({"uri": "/a*x*y*"})
1921510Saxel.duch@nginx.com
1931596Szelenkov@nginx.com        assert self.get(url='/axy')['status'] == 200, '/axy'
1941596Szelenkov@nginx.com        assert self.get(url='/ayx')['status'] == 404, '/ayx'
1951510Saxel.duch@nginx.com
1961510Saxel.duch@nginx.com    def test_route_match_wildcards_adjust_start(self):
1971510Saxel.duch@nginx.com        self.route_match({"uri": "/bla*bla*"})
1981510Saxel.duch@nginx.com
1991596Szelenkov@nginx.com        assert self.get(url='/bla_foo')['status'] == 404, '/bla_foo'
2001510Saxel.duch@nginx.com
2011510Saxel.duch@nginx.com    def test_route_match_wildcards_adjust_start_substr(self):
2021510Saxel.duch@nginx.com        self.route_match({"uri": "*bla*bla*"})
2031510Saxel.duch@nginx.com
2041596Szelenkov@nginx.com        assert self.get(url='/bla_foo')['status'] == 404, '/bla_foo'
2051510Saxel.duch@nginx.com
2061510Saxel.duch@nginx.com    def test_route_match_wildcards_adjust_end(self):
2071510Saxel.duch@nginx.com        self.route_match({"uri": "/bla*bla"})
2081510Saxel.duch@nginx.com
2091596Szelenkov@nginx.com        assert self.get(url='/foo_bla')['status'] == 404, '/foo_bla'
2101510Saxel.duch@nginx.com
2111037Szelenkov@nginx.com    def test_routes_match_wildcard_right_case_sensitive(self):
2121308Szelenkov@nginx.com        self.route_match({"uri": "/bla*"})
2131037Szelenkov@nginx.com
2141596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
2151596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
2161037Szelenkov@nginx.com
2171037Szelenkov@nginx.com    def test_routes_match_wildcard_substring_case_sensitive(self):
2181308Szelenkov@nginx.com        self.route_match({"uri": "*bla*"})
2191037Szelenkov@nginx.com
2201596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
2211596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
2221037Szelenkov@nginx.com
2231508Saxel.duch@nginx.com    def test_routes_match_many_wildcard_substrings_case_sensitive(self):
2241508Saxel.duch@nginx.com        self.route_match({"uri": "*a*B*c*"})
2251508Saxel.duch@nginx.com
2261596Szelenkov@nginx.com        assert self.get(url='/blah-a-B-c-blah')['status'] == 200
2271596Szelenkov@nginx.com        assert self.get(url='/a-B-c')['status'] == 200
2281596Szelenkov@nginx.com        assert self.get(url='/aBc')['status'] == 200
2291596Szelenkov@nginx.com        assert self.get(url='/aBCaBbc')['status'] == 200
2301596Szelenkov@nginx.com        assert self.get(url='/ABc')['status'] == 404
2311508Saxel.duch@nginx.com
2321721Saxel.duch@nginx.com    def test_routes_empty_regex(self):
2331807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2341807Szelenkov@nginx.com            pytest.skip('requires regex')
2351807Szelenkov@nginx.com
2361848Szelenkov@nginx.com        self.route_match({"uri": "~"})
2371721Saxel.duch@nginx.com        assert self.get(url='/')['status'] == 200, 'empty regexp'
2381721Saxel.duch@nginx.com        assert self.get(url='/anything')['status'] == 200, '/anything'
2391721Saxel.duch@nginx.com
2401848Szelenkov@nginx.com        self.route_match({"uri": "!~"})
2411721Saxel.duch@nginx.com        assert self.get(url='/')['status'] == 404, 'empty regexp 2'
2421721Saxel.duch@nginx.com        assert self.get(url='/nothing')['status'] == 404, '/nothing'
2431721Saxel.duch@nginx.com
2441721Saxel.duch@nginx.com    def test_routes_bad_regex(self):
2451807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2461807Szelenkov@nginx.com            pytest.skip('requires regex')
2471807Szelenkov@nginx.com
2481721Saxel.duch@nginx.com        assert 'error' in self.route(
2491721Saxel.duch@nginx.com            {"match": {"uri": "~/bl[ah"}, "action": {"return": 200}}
2501721Saxel.duch@nginx.com        ), 'bad regex'
2511721Saxel.duch@nginx.com
2521721Saxel.duch@nginx.com        status = self.route(
2531721Saxel.duch@nginx.com            {"match": {"uri": "~(?R)?z"}, "action": {"return": 200}}
2541721Saxel.duch@nginx.com        )
2551721Saxel.duch@nginx.com        if 'error' not in status:
2561721Saxel.duch@nginx.com            assert self.get(url='/nothing_z')['status'] == 500, '/nothing_z'
2571721Saxel.duch@nginx.com
2581721Saxel.duch@nginx.com        status = self.route(
2591721Saxel.duch@nginx.com            {"match": {"uri": "~((?1)?z)"}, "action": {"return": 200}}
2601721Saxel.duch@nginx.com        )
2611721Saxel.duch@nginx.com        if 'error' not in status:
2621721Saxel.duch@nginx.com            assert self.get(url='/nothing_z')['status'] == 500, '/nothing_z'
2631721Saxel.duch@nginx.com
2641721Saxel.duch@nginx.com    def test_routes_match_regex_case_sensitive(self):
2651807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2661807Szelenkov@nginx.com            pytest.skip('requires regex')
2671807Szelenkov@nginx.com
2681721Saxel.duch@nginx.com        self.route_match({"uri": "~/bl[ah]"})
2691721Saxel.duch@nginx.com
2701721Saxel.duch@nginx.com        assert self.get(url='/rlah')['status'] == 404, '/rlah'
2711721Saxel.duch@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
2721721Saxel.duch@nginx.com        assert self.get(url='/blh')['status'] == 200, '/blh'
2731721Saxel.duch@nginx.com        assert self.get(url='/BLAH')['status'] == 404, '/BLAH'
2741721Saxel.duch@nginx.com
2751721Saxel.duch@nginx.com    def test_routes_match_regex_negative_case_sensitive(self):
2761807Szelenkov@nginx.com        if not option.available['modules']['regex']:
2771807Szelenkov@nginx.com            pytest.skip('requires regex')
2781807Szelenkov@nginx.com
2791721Saxel.duch@nginx.com        self.route_match({"uri": "!~/bl[ah]"})
2801721Saxel.duch@nginx.com
2811721Saxel.duch@nginx.com        assert self.get(url='/rlah')['status'] == 200, '/rlah'
2821721Saxel.duch@nginx.com        assert self.get(url='/blah')['status'] == 404, '/blah'
2831721Saxel.duch@nginx.com        assert self.get(url='/blh')['status'] == 404, '/blh'
2841721Saxel.duch@nginx.com        assert self.get(url='/BLAH')['status'] == 200, '/BLAH'
2851721Saxel.duch@nginx.com
2861478Szelenkov@nginx.com    def test_routes_pass_encode(self):
2871478Szelenkov@nginx.com        def check_pass(path, name):
2881596Szelenkov@nginx.com            assert 'success' in self.conf(
2891596Szelenkov@nginx.com                {
2901596Szelenkov@nginx.com                    "listeners": {"*:7080": {"pass": "applications/" + path}},
2911596Szelenkov@nginx.com                    "applications": {
2921596Szelenkov@nginx.com                        name: {
2931596Szelenkov@nginx.com                            "type": "python",
2941596Szelenkov@nginx.com                            "processes": {"spare": 0},
2951596Szelenkov@nginx.com                            "path": option.test_dir + '/python/empty',
2961596Szelenkov@nginx.com                            "working_directory": option.test_dir
2971596Szelenkov@nginx.com                            + '/python/empty',
2981596Szelenkov@nginx.com                            "module": "wsgi",
2991596Szelenkov@nginx.com                        }
3001596Szelenkov@nginx.com                    },
3011596Szelenkov@nginx.com                }
3021478Szelenkov@nginx.com            )
3031478Szelenkov@nginx.com
3041596Szelenkov@nginx.com            assert self.get()['status'] == 200
3051478Szelenkov@nginx.com
3061478Szelenkov@nginx.com        check_pass("%25", "%")
3071478Szelenkov@nginx.com        check_pass("blah%2Fblah", "blah/blah")
3081478Szelenkov@nginx.com        check_pass("%2Fblah%2F%2Fblah%2F", "/blah//blah/")
3091478Szelenkov@nginx.com        check_pass("%20blah%252Fblah%7E", " blah%2Fblah~")
3101478Szelenkov@nginx.com
3111478Szelenkov@nginx.com        def check_pass_error(path, name):
3121596Szelenkov@nginx.com            assert 'error' in self.conf(
3131596Szelenkov@nginx.com                {
3141596Szelenkov@nginx.com                    "listeners": {"*:7080": {"pass": "applications/" + path}},
3151596Szelenkov@nginx.com                    "applications": {
3161596Szelenkov@nginx.com                        name: {
3171596Szelenkov@nginx.com                            "type": "python",
3181596Szelenkov@nginx.com                            "processes": {"spare": 0},
3191596Szelenkov@nginx.com                            "path": option.test_dir + '/python/empty',
3201596Szelenkov@nginx.com                            "working_directory": option.test_dir
3211596Szelenkov@nginx.com                            + '/python/empty',
3221596Szelenkov@nginx.com                            "module": "wsgi",
3231596Szelenkov@nginx.com                        }
3241596Szelenkov@nginx.com                    },
3251596Szelenkov@nginx.com                }
3261478Szelenkov@nginx.com            )
3271478Szelenkov@nginx.com
3281478Szelenkov@nginx.com        check_pass_error("%", "%")
3291478Szelenkov@nginx.com        check_pass_error("%1", "%1")
3301478Szelenkov@nginx.com
331972Szelenkov@nginx.com    def test_routes_absent(self):
3321775Szelenkov@nginx.com        assert 'success' in self.conf(
3331017Szelenkov@nginx.com            {
3341017Szelenkov@nginx.com                "listeners": {"*:7081": {"pass": "applications/empty"}},
3351017Szelenkov@nginx.com                "applications": {
3361017Szelenkov@nginx.com                    "empty": {
3371017Szelenkov@nginx.com                        "type": "python",
3381017Szelenkov@nginx.com                        "processes": {"spare": 0},
3391596Szelenkov@nginx.com                        "path": option.test_dir + '/python/empty',
3401848Szelenkov@nginx.com                        "working_directory": option.test_dir + '/python/empty',
3411017Szelenkov@nginx.com                        "module": "wsgi",
3421017Szelenkov@nginx.com                    }
3431017Szelenkov@nginx.com                },
344972Szelenkov@nginx.com            }
3451017Szelenkov@nginx.com        )
346972Szelenkov@nginx.com
3471596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 200, 'routes absent'
348972Szelenkov@nginx.com
349972Szelenkov@nginx.com    def test_routes_pass_invalid(self):
3501596Szelenkov@nginx.com        assert 'error' in self.conf(
3511596Szelenkov@nginx.com            {"pass": "routes/blah"}, 'listeners/*:7080'
3521596Szelenkov@nginx.com        ), 'routes invalid'
353972Szelenkov@nginx.com
354972Szelenkov@nginx.com    def test_route_empty(self):
3551596Szelenkov@nginx.com        assert 'success' in self.conf(
3561596Szelenkov@nginx.com            {
3571596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/main"}},
3581596Szelenkov@nginx.com                "routes": {"main": []},
3591596Szelenkov@nginx.com                "applications": {},
3601596Szelenkov@nginx.com            }
3611596Szelenkov@nginx.com        ), 'route empty configure'
362972Szelenkov@nginx.com
3631596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'route empty'
364972Szelenkov@nginx.com
365972Szelenkov@nginx.com    def test_routes_route_empty(self):
3661596Szelenkov@nginx.com        assert 'success' in self.conf(
3671596Szelenkov@nginx.com            {}, 'listeners'
3681596Szelenkov@nginx.com        ), 'routes empty listeners configure'
369972Szelenkov@nginx.com
3701596Szelenkov@nginx.com        assert 'success' in self.conf({}, 'routes'), 'routes empty configure'
371972Szelenkov@nginx.com
372972Szelenkov@nginx.com    def test_routes_route_match_absent(self):
3731596Szelenkov@nginx.com        assert 'success' in self.conf(
3741596Szelenkov@nginx.com            [{"action": {"return": 200}}], 'routes'
3751596Szelenkov@nginx.com        ), 'route match absent configure'
376972Szelenkov@nginx.com
3771596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'route match absent'
378972Szelenkov@nginx.com
3791736Szelenkov@nginx.com    def test_routes_route_action_absent(self, skip_alert):
3801596Szelenkov@nginx.com        skip_alert(r'failed to apply new conf')
381972Szelenkov@nginx.com
3821596Szelenkov@nginx.com        assert 'error' in self.conf(
3831596Szelenkov@nginx.com            [{"match": {"method": "GET"}}], 'routes'
3841596Szelenkov@nginx.com        ), 'route pass absent configure'
385972Szelenkov@nginx.com
3861597Shongzhidao@gmail.com    def test_routes_route_pass(self):
3871597Shongzhidao@gmail.com        assert 'success' in self.conf(
3881597Shongzhidao@gmail.com            {
3891597Shongzhidao@gmail.com                "applications": {
3901597Shongzhidao@gmail.com                    "app": {
3911597Shongzhidao@gmail.com                        "type": "python",
3921597Shongzhidao@gmail.com                        "processes": {"spare": 0},
3931597Shongzhidao@gmail.com                        "path": "/app",
3941597Shongzhidao@gmail.com                        "module": "wsgi",
3951597Shongzhidao@gmail.com                    }
3961597Shongzhidao@gmail.com                },
3971597Shongzhidao@gmail.com                "upstreams": {
3981597Shongzhidao@gmail.com                    "one": {
3991597Shongzhidao@gmail.com                        "servers": {
4001597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4011597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4021597Shongzhidao@gmail.com                        },
4031597Shongzhidao@gmail.com                    },
4041597Shongzhidao@gmail.com                    "two": {
4051597Shongzhidao@gmail.com                        "servers": {
4061597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4071597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4081597Shongzhidao@gmail.com                        },
4091597Shongzhidao@gmail.com                    },
4101597Shongzhidao@gmail.com                },
4111597Shongzhidao@gmail.com            }
4121597Shongzhidao@gmail.com        )
4131597Shongzhidao@gmail.com
4141597Shongzhidao@gmail.com        assert 'success' in self.conf(
4151597Shongzhidao@gmail.com            [{"action": {"pass": "routes"}}], 'routes'
4161597Shongzhidao@gmail.com        )
4171597Shongzhidao@gmail.com        assert 'success' in self.conf(
4181597Shongzhidao@gmail.com            [{"action": {"pass": "applications/app"}}], 'routes'
4191597Shongzhidao@gmail.com        )
4201597Shongzhidao@gmail.com        assert 'success' in self.conf(
4211597Shongzhidao@gmail.com            [{"action": {"pass": "upstreams/one"}}], 'routes'
4221597Shongzhidao@gmail.com        )
4231597Shongzhidao@gmail.com
424972Szelenkov@nginx.com    def test_routes_route_pass_absent(self):
4251596Szelenkov@nginx.com        assert 'error' in self.conf(
4261596Szelenkov@nginx.com            [{"match": {"method": "GET"}, "action": {}}], 'routes'
4271596Szelenkov@nginx.com        ), 'route pass absent configure'
428972Szelenkov@nginx.com
4291597Shongzhidao@gmail.com    def test_routes_route_pass_invalid(self):
4301597Shongzhidao@gmail.com        assert 'success' in self.conf(
4311597Shongzhidao@gmail.com            {
4321597Shongzhidao@gmail.com                "applications": {
4331597Shongzhidao@gmail.com                    "app": {
4341597Shongzhidao@gmail.com                        "type": "python",
4351597Shongzhidao@gmail.com                        "processes": {"spare": 0},
4361597Shongzhidao@gmail.com                        "path": "/app",
4371597Shongzhidao@gmail.com                        "module": "wsgi",
4381597Shongzhidao@gmail.com                    }
4391597Shongzhidao@gmail.com                },
4401597Shongzhidao@gmail.com                "upstreams": {
4411597Shongzhidao@gmail.com                    "one": {
4421597Shongzhidao@gmail.com                        "servers": {
4431597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4441597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4451597Shongzhidao@gmail.com                        },
4461597Shongzhidao@gmail.com                    },
4471597Shongzhidao@gmail.com                    "two": {
4481597Shongzhidao@gmail.com                        "servers": {
4491597Shongzhidao@gmail.com                            "127.0.0.1:7081": {},
4501597Shongzhidao@gmail.com                            "127.0.0.1:7082": {},
4511597Shongzhidao@gmail.com                        },
4521597Shongzhidao@gmail.com                    },
4531597Shongzhidao@gmail.com                },
4541597Shongzhidao@gmail.com            }
4551597Shongzhidao@gmail.com        )
4561597Shongzhidao@gmail.com
4571597Shongzhidao@gmail.com        assert 'error' in self.conf(
4581597Shongzhidao@gmail.com            [{"action": {"pass": "blah"}}], 'routes'
4591597Shongzhidao@gmail.com        ), 'route pass invalid'
4601597Shongzhidao@gmail.com        assert 'error' in self.conf(
4611597Shongzhidao@gmail.com            [{"action": {"pass": "routes/blah"}}], 'routes'
4621597Shongzhidao@gmail.com        ), 'route pass routes invalid'
4631597Shongzhidao@gmail.com        assert 'error' in self.conf(
4641597Shongzhidao@gmail.com            [{"action": {"pass": "applications/blah"}}], 'routes'
4651597Shongzhidao@gmail.com        ), 'route pass applications invalid'
4661597Shongzhidao@gmail.com        assert 'error' in self.conf(
4671597Shongzhidao@gmail.com            [{"action": {"pass": "upstreams/blah"}}], 'routes'
4681597Shongzhidao@gmail.com        ), 'route pass upstreams invalid'
4691597Shongzhidao@gmail.com
4701654Szelenkov@nginx.com    def test_routes_action_unique(self, temp_dir):
4711596Szelenkov@nginx.com        assert 'success' in self.conf(
4721596Szelenkov@nginx.com            {
4731596Szelenkov@nginx.com                "listeners": {
4741596Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
4751596Szelenkov@nginx.com                    "*:7081": {"pass": "applications/app"},
4761596Szelenkov@nginx.com                },
4771596Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
4781596Szelenkov@nginx.com                "applications": {
4791596Szelenkov@nginx.com                    "app": {
4801596Szelenkov@nginx.com                        "type": "python",
4811596Szelenkov@nginx.com                        "processes": {"spare": 0},
4821596Szelenkov@nginx.com                        "path": "/app",
4831596Szelenkov@nginx.com                        "module": "wsgi",
4841596Szelenkov@nginx.com                    }
4851596Szelenkov@nginx.com                },
4861596Szelenkov@nginx.com            }
4871379Szelenkov@nginx.com        )
4881379Szelenkov@nginx.com
4891596Szelenkov@nginx.com        assert 'error' in self.conf(
4901654Szelenkov@nginx.com            {"proxy": "http://127.0.0.1:7081", "share": temp_dir},
4911596Szelenkov@nginx.com            'routes/0/action',
4921596Szelenkov@nginx.com        ), 'proxy share'
4931596Szelenkov@nginx.com        assert 'error' in self.conf(
4941596Szelenkov@nginx.com            {"proxy": "http://127.0.0.1:7081", "pass": "applications/app",},
4951596Szelenkov@nginx.com            'routes/0/action',
4961596Szelenkov@nginx.com        ), 'proxy pass'
4971596Szelenkov@nginx.com        assert 'error' in self.conf(
4981848Szelenkov@nginx.com            {"share": temp_dir, "pass": "applications/app"}, 'routes/0/action',
4991596Szelenkov@nginx.com        ), 'share pass'
5001379Szelenkov@nginx.com
501972Szelenkov@nginx.com    def test_routes_rules_two(self):
5021596Szelenkov@nginx.com        assert 'success' in self.conf(
5031596Szelenkov@nginx.com            [
5041596Szelenkov@nginx.com                {"match": {"method": "GET"}, "action": {"return": 200}},
5051596Szelenkov@nginx.com                {"match": {"method": "POST"}, "action": {"return": 201}},
5061596Szelenkov@nginx.com            ],
5071596Szelenkov@nginx.com            'routes',
5081596Szelenkov@nginx.com        ), 'rules two configure'
509972Szelenkov@nginx.com
5101596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'rules two match first'
5111596Szelenkov@nginx.com        assert self.post()['status'] == 201, 'rules two match second'
512972Szelenkov@nginx.com
513972Szelenkov@nginx.com    def test_routes_two(self):
5141596Szelenkov@nginx.com        assert 'success' in self.conf(
5151596Szelenkov@nginx.com            {
5161596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/first"}},
5171596Szelenkov@nginx.com                "routes": {
5181596Szelenkov@nginx.com                    "first": [
5191596Szelenkov@nginx.com                        {
5201596Szelenkov@nginx.com                            "match": {"method": "GET"},
5211596Szelenkov@nginx.com                            "action": {"pass": "routes/second"},
5221596Szelenkov@nginx.com                        }
5231596Szelenkov@nginx.com                    ],
5241596Szelenkov@nginx.com                    "second": [
5251596Szelenkov@nginx.com                        {
5261596Szelenkov@nginx.com                            "match": {"host": "localhost"},
5271596Szelenkov@nginx.com                            "action": {"return": 200},
5281596Szelenkov@nginx.com                        }
5291596Szelenkov@nginx.com                    ],
5301596Szelenkov@nginx.com                },
5311596Szelenkov@nginx.com                "applications": {},
5321596Szelenkov@nginx.com            }
5331596Szelenkov@nginx.com        ), 'routes two configure'
534972Szelenkov@nginx.com
5351596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes two'
536972Szelenkov@nginx.com
537972Szelenkov@nginx.com    def test_routes_match_host_positive(self):
5381308Szelenkov@nginx.com        self.route_match({"host": "localhost"})
539972Szelenkov@nginx.com
5401596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'localhost'
5411308Szelenkov@nginx.com        self.host('localhost.', 200)
5421308Szelenkov@nginx.com        self.host('localhost.', 200)
5431308Szelenkov@nginx.com        self.host('.localhost', 404)
5441308Szelenkov@nginx.com        self.host('www.localhost', 404)
5451308Szelenkov@nginx.com        self.host('localhost1', 404)
546972Szelenkov@nginx.com
5471596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
5481065Szelenkov@nginx.com    def test_routes_match_host_absent(self):
5491308Szelenkov@nginx.com        self.route_match({"host": "localhost"})
5501065Szelenkov@nginx.com
5511596Szelenkov@nginx.com        assert (
5521596Szelenkov@nginx.com            self.get(headers={'Connection': 'close'})['status'] == 400
5531596Szelenkov@nginx.com        ), 'match host absent'
5541065Szelenkov@nginx.com
555972Szelenkov@nginx.com    def test_routes_match_host_ipv4(self):
5561308Szelenkov@nginx.com        self.route_match({"host": "127.0.0.1"})
557972Szelenkov@nginx.com
5581308Szelenkov@nginx.com        self.host('127.0.0.1', 200)
5591308Szelenkov@nginx.com        self.host('127.0.0.1:7080', 200)
560972Szelenkov@nginx.com
561972Szelenkov@nginx.com    def test_routes_match_host_ipv6(self):
5621308Szelenkov@nginx.com        self.route_match({"host": "[::1]"})
563972Szelenkov@nginx.com
5641308Szelenkov@nginx.com        self.host('[::1]', 200)
5651308Szelenkov@nginx.com        self.host('[::1]:7080', 200)
566972Szelenkov@nginx.com
567972Szelenkov@nginx.com    def test_routes_match_host_positive_many(self):
5681308Szelenkov@nginx.com        self.route_match({"host": ["localhost", "example.com"]})
569972Szelenkov@nginx.com
5701596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'localhost'
5711308Szelenkov@nginx.com        self.host('example.com', 200)
572972Szelenkov@nginx.com
573972Szelenkov@nginx.com    def test_routes_match_host_positive_and_negative(self):
5741308Szelenkov@nginx.com        self.route_match({"host": ["*example.com", "!www.example.com"]})
575972Szelenkov@nginx.com
5761596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'localhost'
5771308Szelenkov@nginx.com        self.host('example.com', 200)
5781308Szelenkov@nginx.com        self.host('www.example.com', 404)
5791308Szelenkov@nginx.com        self.host('!www.example.com', 200)
580972Szelenkov@nginx.com
581972Szelenkov@nginx.com    def test_routes_match_host_positive_and_negative_wildcard(self):
5821308Szelenkov@nginx.com        self.route_match({"host": ["*example*", "!www.example*"]})
583972Szelenkov@nginx.com
5841308Szelenkov@nginx.com        self.host('example.com', 200)
5851308Szelenkov@nginx.com        self.host('www.example.com', 404)
586972Szelenkov@nginx.com
587972Szelenkov@nginx.com    def test_routes_match_host_case_insensitive(self):
5881308Szelenkov@nginx.com        self.route_match({"host": "Example.com"})
589972Szelenkov@nginx.com
5901308Szelenkov@nginx.com        self.host('example.com', 200)
5911308Szelenkov@nginx.com        self.host('EXAMPLE.COM', 200)
592972Szelenkov@nginx.com
593972Szelenkov@nginx.com    def test_routes_match_host_port(self):
5941308Szelenkov@nginx.com        self.route_match({"host": "example.com"})
595972Szelenkov@nginx.com
5961308Szelenkov@nginx.com        self.host('example.com:7080', 200)
597972Szelenkov@nginx.com
5981053Szelenkov@nginx.com    def test_routes_match_host_empty(self):
5991308Szelenkov@nginx.com        self.route_match({"host": ""})
6001053Szelenkov@nginx.com
6011308Szelenkov@nginx.com        self.host('', 200)
6021596Szelenkov@nginx.com        assert (
6031596Szelenkov@nginx.com            self.get(http_10=True, headers={})['status'] == 200
6041596Szelenkov@nginx.com        ), 'match host empty 2'
6051596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match host empty 3'
6061053Szelenkov@nginx.com
607972Szelenkov@nginx.com    def test_routes_match_uri_positive(self):
6081308Szelenkov@nginx.com        self.route_match({"uri": ["/blah", "/slash/"]})
609972Szelenkov@nginx.com
6101596Szelenkov@nginx.com        assert self.get()['status'] == 404, '/'
6111596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, '/blah'
6121596Szelenkov@nginx.com        assert self.get(url='/blah#foo')['status'] == 200, '/blah#foo'
6131596Szelenkov@nginx.com        assert self.get(url='/blah?var')['status'] == 200, '/blah?var'
6141596Szelenkov@nginx.com        assert self.get(url='//blah')['status'] == 200, '//blah'
6151596Szelenkov@nginx.com        assert self.get(url='/slash/foo/../')['status'] == 200, 'relative'
6161596Szelenkov@nginx.com        assert self.get(url='/slash/./')['status'] == 200, '/slash/./'
6171596Szelenkov@nginx.com        assert self.get(url='/slash//.//')['status'] == 200, 'adjacent slashes'
6181596Szelenkov@nginx.com        assert self.get(url='/%')['status'] == 400, 'percent'
6191596Szelenkov@nginx.com        assert self.get(url='/%1')['status'] == 400, 'percent digit'
6201596Szelenkov@nginx.com        assert self.get(url='/%A')['status'] == 400, 'percent letter'
6211596Szelenkov@nginx.com        assert self.get(url='/slash/.?args')['status'] == 200, 'dot args'
6221596Szelenkov@nginx.com        assert self.get(url='/slash/.#frag')['status'] == 200, 'dot frag'
6231596Szelenkov@nginx.com        assert (
6241596Szelenkov@nginx.com            self.get(url='/slash/foo/..?args')['status'] == 200
6251596Szelenkov@nginx.com        ), 'dot dot args'
6261596Szelenkov@nginx.com        assert (
6271596Szelenkov@nginx.com            self.get(url='/slash/foo/..#frag')['status'] == 200
6281596Szelenkov@nginx.com        ), 'dot dot frag'
6291596Szelenkov@nginx.com        assert self.get(url='/slash/.')['status'] == 200, 'trailing dot'
6301596Szelenkov@nginx.com        assert (
6311596Szelenkov@nginx.com            self.get(url='/slash/foo/..')['status'] == 200
6321596Szelenkov@nginx.com        ), 'trailing dot dot'
633972Szelenkov@nginx.com
634972Szelenkov@nginx.com    def test_routes_match_uri_case_sensitive(self):
6351308Szelenkov@nginx.com        self.route_match({"uri": "/BLAH"})
6361308Szelenkov@nginx.com
6371596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 404, '/blah'
6381596Szelenkov@nginx.com        assert self.get(url='/BlaH')['status'] == 404, '/BlaH'
6391596Szelenkov@nginx.com        assert self.get(url='/BLAH')['status'] == 200, '/BLAH'
6401308Szelenkov@nginx.com
6411308Szelenkov@nginx.com    def test_routes_match_uri_normalize(self):
6421308Szelenkov@nginx.com        self.route_match({"uri": "/blah"})
643972Szelenkov@nginx.com
6441596Szelenkov@nginx.com        assert self.get(url='/%62%6c%61%68')['status'] == 200, 'normalize'
645972Szelenkov@nginx.com
6461053Szelenkov@nginx.com    def test_routes_match_empty_array(self):
6471308Szelenkov@nginx.com        self.route_match({"uri": []})
6481053Szelenkov@nginx.com
6491596Szelenkov@nginx.com        assert self.get(url='/blah')['status'] == 200, 'empty array'
6501053Szelenkov@nginx.com
6511053Szelenkov@nginx.com    def test_routes_reconfigure(self):
6521596Szelenkov@nginx.com        assert 'success' in self.conf([], 'routes'), 'redefine'
6531596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'redefine request'
6541053Szelenkov@nginx.com
6551596Szelenkov@nginx.com        assert 'success' in self.conf(
6561596Szelenkov@nginx.com            [{"action": {"return": 200}}], 'routes'
6571596Szelenkov@nginx.com        ), 'redefine 2'
6581596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 2'
6591596Szelenkov@nginx.com
6601596Szelenkov@nginx.com        assert 'success' in self.conf([], 'routes'), 'redefine 3'
6611596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'redefine request 3'
6621053Szelenkov@nginx.com
6631596Szelenkov@nginx.com        assert 'success' in self.conf(
6641596Szelenkov@nginx.com            {
6651596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/main"}},
6661596Szelenkov@nginx.com                "routes": {"main": [{"action": {"return": 200}}]},
6671596Szelenkov@nginx.com                "applications": {},
6681596Szelenkov@nginx.com            }
6691596Szelenkov@nginx.com        ), 'redefine 4'
6701596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 4'
6711053Szelenkov@nginx.com
6721596Szelenkov@nginx.com        assert 'success' in self.conf_delete('routes/main/0'), 'redefine 5'
6731596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'redefine request 5'
6741053Szelenkov@nginx.com
6751596Szelenkov@nginx.com        assert 'success' in self.conf_post(
6761596Szelenkov@nginx.com            {"action": {"return": 200}}, 'routes/main'
6771596Szelenkov@nginx.com        ), 'redefine 6'
6781596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 6'
6791063Szelenkov@nginx.com
6801596Szelenkov@nginx.com        assert 'error' in self.conf(
6811596Szelenkov@nginx.com            {"action": {"return": 200}}, 'routes/main/2'
6821596Szelenkov@nginx.com        ), 'redefine 7'
6831596Szelenkov@nginx.com        assert 'success' in self.conf(
6841596Szelenkov@nginx.com            {"action": {"return": 201}}, 'routes/main/1'
6851596Szelenkov@nginx.com        ), 'redefine 8'
6861596Szelenkov@nginx.com
6871596Szelenkov@nginx.com        assert len(self.conf_get('routes/main')) == 2, 'redefine conf 8'
6881596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'redefine request 8'
6891063Szelenkov@nginx.com
6901053Szelenkov@nginx.com    def test_routes_edit(self):
6911308Szelenkov@nginx.com        self.route_match({"method": "GET"})
6921053Szelenkov@nginx.com
6931596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes edit GET'
6941596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'routes edit POST'
6951053Szelenkov@nginx.com
6961596Szelenkov@nginx.com        assert 'success' in self.conf_post(
6971596Szelenkov@nginx.com            {"match": {"method": "POST"}, "action": {"return": 200}}, 'routes',
6981596Szelenkov@nginx.com        ), 'routes edit configure 2'
6991596Szelenkov@nginx.com        assert 'GET' == self.conf_get(
7001596Szelenkov@nginx.com            'routes/0/match/method'
7011596Szelenkov@nginx.com        ), 'routes edit configure 2 check'
7021596Szelenkov@nginx.com        assert 'POST' == self.conf_get(
7031596Szelenkov@nginx.com            'routes/1/match/method'
7041596Szelenkov@nginx.com        ), 'routes edit configure 2 check 2'
7051053Szelenkov@nginx.com
7061596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes edit GET 2'
7071596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 2'
7081053Szelenkov@nginx.com
7091596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7101596Szelenkov@nginx.com            'routes/0'
7111596Szelenkov@nginx.com        ), 'routes edit configure 3'
7121053Szelenkov@nginx.com
7131596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 3'
7141596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 3'
7151053Szelenkov@nginx.com
7161596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7171596Szelenkov@nginx.com            'routes/1'
7181596Szelenkov@nginx.com        ), 'routes edit configure invalid'
7191596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7201596Szelenkov@nginx.com            'routes/-1'
7211596Szelenkov@nginx.com        ), 'routes edit configure invalid 2'
7221596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7231596Szelenkov@nginx.com            'routes/blah'
7241596Szelenkov@nginx.com        ), 'routes edit configure invalid 3'
7251053Szelenkov@nginx.com
7261596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 4'
7271596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 4'
7281053Szelenkov@nginx.com
7291596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7301596Szelenkov@nginx.com            'routes/0'
7311596Szelenkov@nginx.com        ), 'routes edit configure 5'
7321053Szelenkov@nginx.com
7331596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 5'
7341596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'routes edit POST 5'
7351053Szelenkov@nginx.com
7361596Szelenkov@nginx.com        assert 'success' in self.conf_post(
7371596Szelenkov@nginx.com            {"match": {"method": "POST"}, "action": {"return": 200}}, 'routes',
7381596Szelenkov@nginx.com        ), 'routes edit configure 6'
7391053Szelenkov@nginx.com
7401596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'routes edit GET 6'
7411596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'routes edit POST 6'
7421053Szelenkov@nginx.com
7431596Szelenkov@nginx.com        assert 'success' in self.conf(
7441596Szelenkov@nginx.com            {
7451596Szelenkov@nginx.com                "listeners": {"*:7080": {"pass": "routes/main"}},
7461596Szelenkov@nginx.com                "routes": {"main": [{"action": {"return": 200}}]},
7471596Szelenkov@nginx.com                "applications": {},
7481596Szelenkov@nginx.com            }
7491596Szelenkov@nginx.com        ), 'route edit configure 7'
7501053Szelenkov@nginx.com
7511596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7521596Szelenkov@nginx.com            'routes/0'
7531596Szelenkov@nginx.com        ), 'routes edit configure invalid 4'
7541596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
7551596Szelenkov@nginx.com            'routes/main'
7561596Szelenkov@nginx.com        ), 'routes edit configure invalid 5'
7571053Szelenkov@nginx.com
7581596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes edit GET 7'
7591053Szelenkov@nginx.com
7601596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7611596Szelenkov@nginx.com            'listeners/*:7080'
7621596Szelenkov@nginx.com        ), 'route edit configure 8'
7631596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7641596Szelenkov@nginx.com            'routes/main'
7651596Szelenkov@nginx.com        ), 'route edit configure 9'
7661053Szelenkov@nginx.com
7671736Szelenkov@nginx.com    def test_match_edit(self, skip_alert):
7681596Szelenkov@nginx.com        skip_alert(r'failed to apply new conf')
7691053Szelenkov@nginx.com
7701308Szelenkov@nginx.com        self.route_match({"method": ["GET", "POST"]})
7711053Szelenkov@nginx.com
7721596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET'
7731596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'match edit POST'
7741596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT'
7751053Szelenkov@nginx.com
7761596Szelenkov@nginx.com        assert 'success' in self.conf_post(
7771596Szelenkov@nginx.com            '\"PUT\"', 'routes/0/match/method'
7781596Szelenkov@nginx.com        ), 'match edit configure 2'
7791596Szelenkov@nginx.com        assert ['GET', 'POST', 'PUT'] == self.conf_get(
7801596Szelenkov@nginx.com            'routes/0/match/method'
7811596Szelenkov@nginx.com        ), 'match edit configure 2 check'
7821053Szelenkov@nginx.com
7831596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 2'
7841596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'match edit POST 2'
7851596Szelenkov@nginx.com        assert self.put()['status'] == 200, 'match edit PUT 2'
7861053Szelenkov@nginx.com
7871596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7881596Szelenkov@nginx.com            'routes/0/match/method/1'
7891596Szelenkov@nginx.com        ), 'match edit configure 3'
7901596Szelenkov@nginx.com        assert ['GET', 'PUT'] == self.conf_get(
7911596Szelenkov@nginx.com            'routes/0/match/method'
7921596Szelenkov@nginx.com        ), 'match edit configure 3 check'
7931053Szelenkov@nginx.com
7941596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 3'
7951596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 3'
7961596Szelenkov@nginx.com        assert self.put()['status'] == 200, 'match edit PUT 3'
7971053Szelenkov@nginx.com
7981596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
7991596Szelenkov@nginx.com            'routes/0/match/method/1'
8001596Szelenkov@nginx.com        ), 'match edit configure 4'
8011596Szelenkov@nginx.com        assert ['GET'] == self.conf_get(
8021596Szelenkov@nginx.com            'routes/0/match/method'
8031596Szelenkov@nginx.com        ), 'match edit configure 4 check'
8041053Szelenkov@nginx.com
8051596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 4'
8061596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 4'
8071596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT 4'
8081053Szelenkov@nginx.com
8091596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8101596Szelenkov@nginx.com            'routes/0/match/method/1'
8111596Szelenkov@nginx.com        ), 'match edit configure invalid'
8121596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8131596Szelenkov@nginx.com            'routes/0/match/method/-1'
8141596Szelenkov@nginx.com        ), 'match edit configure invalid 2'
8151596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8161596Szelenkov@nginx.com            'routes/0/match/method/blah'
8171596Szelenkov@nginx.com        ), 'match edit configure invalid 3'
8181596Szelenkov@nginx.com        assert ['GET'] == self.conf_get(
8191596Szelenkov@nginx.com            'routes/0/match/method'
8201596Szelenkov@nginx.com        ), 'match edit configure 5 check'
8211053Szelenkov@nginx.com
8221596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 5'
8231596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 5'
8241596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT 5'
8251596Szelenkov@nginx.com
8261596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
8271596Szelenkov@nginx.com            'routes/0/match/method/0'
8281596Szelenkov@nginx.com        ), 'match edit configure 6'
8291596Szelenkov@nginx.com        assert [] == self.conf_get(
8301596Szelenkov@nginx.com            'routes/0/match/method'
8311596Szelenkov@nginx.com        ), 'match edit configure 6 check'
8321053Szelenkov@nginx.com
8331596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 6'
8341596Szelenkov@nginx.com        assert self.post()['status'] == 200, 'match edit POST 6'
8351596Szelenkov@nginx.com        assert self.put()['status'] == 200, 'match edit PUT 6'
8361053Szelenkov@nginx.com
8371596Szelenkov@nginx.com        assert 'success' in self.conf(
8381596Szelenkov@nginx.com            '"GET"', 'routes/0/match/method'
8391596Szelenkov@nginx.com        ), 'match edit configure 7'
8401053Szelenkov@nginx.com
8411596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 7'
8421596Szelenkov@nginx.com        assert self.post()['status'] == 404, 'match edit POST 7'
8431596Szelenkov@nginx.com        assert self.put()['status'] == 404, 'match edit PUT 7'
8441053Szelenkov@nginx.com
8451596Szelenkov@nginx.com        assert 'error' in self.conf_delete(
8461596Szelenkov@nginx.com            'routes/0/match/method/0'
8471596Szelenkov@nginx.com        ), 'match edit configure invalid 5'
8481596Szelenkov@nginx.com        assert 'error' in self.conf(
8491596Szelenkov@nginx.com            {}, 'routes/0/action'
8501596Szelenkov@nginx.com        ), 'match edit configure invalid 6'
8511053Szelenkov@nginx.com
8521596Szelenkov@nginx.com        assert 'success' in self.conf(
8531596Szelenkov@nginx.com            {}, 'routes/0/match'
8541596Szelenkov@nginx.com        ), 'match edit configure 8'
8551053Szelenkov@nginx.com
8561596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match edit GET 8'
8571053Szelenkov@nginx.com
858972Szelenkov@nginx.com    def test_routes_match_rules(self):
8591308Szelenkov@nginx.com        self.route_match({"method": "GET", "host": "localhost", "uri": "/"})
860972Szelenkov@nginx.com
8611596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'routes match rules'
862972Szelenkov@nginx.com
863972Szelenkov@nginx.com    def test_routes_loop(self):
8641596Szelenkov@nginx.com        assert 'success' in self.route(
8651596Szelenkov@nginx.com            {"match": {"uri": "/"}, "action": {"pass": "routes"}}
8661596Szelenkov@nginx.com        ), 'routes loop configure'
867972Szelenkov@nginx.com
8681596Szelenkov@nginx.com        assert self.get()['status'] == 500, 'routes loop'
869972Szelenkov@nginx.com
8701066Szelenkov@nginx.com    def test_routes_match_headers(self):
8711308Szelenkov@nginx.com        self.route_match({"headers": {"host": "localhost"}})
8721066Szelenkov@nginx.com
8731596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'match headers'
8741308Szelenkov@nginx.com        self.host('Localhost', 200)
8751308Szelenkov@nginx.com        self.host('localhost.com', 404)
8761308Szelenkov@nginx.com        self.host('llocalhost', 404)
8771308Szelenkov@nginx.com        self.host('host', 404)
8781066Szelenkov@nginx.com
8791066Szelenkov@nginx.com    def test_routes_match_headers_multiple(self):
8801308Szelenkov@nginx.com        self.route_match({"headers": {"host": "localhost", "x-blah": "test"}})
8811066Szelenkov@nginx.com
8821596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match headers multiple'
8831596Szelenkov@nginx.com        assert (
8841066Szelenkov@nginx.com            self.get(
8851066Szelenkov@nginx.com                headers={
8861066Szelenkov@nginx.com                    "Host": "localhost",
8871066Szelenkov@nginx.com                    "X-blah": "test",
8881066Szelenkov@nginx.com                    "Connection": "close",
8891066Szelenkov@nginx.com                }
8901596Szelenkov@nginx.com            )['status']
8911596Szelenkov@nginx.com            == 200
8921596Szelenkov@nginx.com        ), 'match headers multiple 2'
8931066Szelenkov@nginx.com
8941596Szelenkov@nginx.com        assert (
8951066Szelenkov@nginx.com            self.get(
8961066Szelenkov@nginx.com                headers={
8971066Szelenkov@nginx.com                    "Host": "localhost",
8981066Szelenkov@nginx.com                    "X-blah": "",
8991066Szelenkov@nginx.com                    "Connection": "close",
9001066Szelenkov@nginx.com                }
9011596Szelenkov@nginx.com            )['status']
9021596Szelenkov@nginx.com            == 404
9031596Szelenkov@nginx.com        ), 'match headers multiple 3'
9041066Szelenkov@nginx.com
9051066Szelenkov@nginx.com    def test_routes_match_headers_multiple_values(self):
9061308Szelenkov@nginx.com        self.route_match({"headers": {"x-blah": "test"}})
9071066Szelenkov@nginx.com
9081596Szelenkov@nginx.com        assert (
9091066Szelenkov@nginx.com            self.get(
9101066Szelenkov@nginx.com                headers={
9111066Szelenkov@nginx.com                    "Host": "localhost",
9121066Szelenkov@nginx.com                    "X-blah": ["test", "test", "test"],
9131066Szelenkov@nginx.com                    "Connection": "close",
9141066Szelenkov@nginx.com                }
9151596Szelenkov@nginx.com            )['status']
9161596Szelenkov@nginx.com            == 200
9171596Szelenkov@nginx.com        ), 'match headers multiple values'
9181596Szelenkov@nginx.com        assert (
9191066Szelenkov@nginx.com            self.get(
9201066Szelenkov@nginx.com                headers={
9211066Szelenkov@nginx.com                    "Host": "localhost",
9221066Szelenkov@nginx.com                    "X-blah": ["test", "blah", "test"],
9231066Szelenkov@nginx.com                    "Connection": "close",
9241066Szelenkov@nginx.com                }
9251596Szelenkov@nginx.com            )['status']
9261596Szelenkov@nginx.com            == 404
9271596Szelenkov@nginx.com        ), 'match headers multiple values 2'
9281596Szelenkov@nginx.com        assert (
9291066Szelenkov@nginx.com            self.get(
9301066Szelenkov@nginx.com                headers={
9311066Szelenkov@nginx.com                    "Host": "localhost",
9321066Szelenkov@nginx.com                    "X-blah": ["test", "", "test"],
9331066Szelenkov@nginx.com                    "Connection": "close",
9341066Szelenkov@nginx.com                }
9351596Szelenkov@nginx.com            )['status']
9361596Szelenkov@nginx.com            == 404
9371596Szelenkov@nginx.com        ), 'match headers multiple values 3'
9381066Szelenkov@nginx.com
9391066Szelenkov@nginx.com    def test_routes_match_headers_multiple_rules(self):
9401308Szelenkov@nginx.com        self.route_match({"headers": {"x-blah": ["test", "blah"]}})
9411066Szelenkov@nginx.com
9421596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match headers multiple rules'
9431596Szelenkov@nginx.com        assert (
9441066Szelenkov@nginx.com            self.get(
9451066Szelenkov@nginx.com                headers={
9461066Szelenkov@nginx.com                    "Host": "localhost",
9471066Szelenkov@nginx.com                    "X-blah": "test",
9481066Szelenkov@nginx.com                    "Connection": "close",
9491066Szelenkov@nginx.com                }
9501596Szelenkov@nginx.com            )['status']
9511596Szelenkov@nginx.com            == 200
9521596Szelenkov@nginx.com        ), 'match headers multiple rules 2'
9531596Szelenkov@nginx.com        assert (
9541066Szelenkov@nginx.com            self.get(
9551066Szelenkov@nginx.com                headers={
9561066Szelenkov@nginx.com                    "Host": "localhost",
9571066Szelenkov@nginx.com                    "X-blah": "blah",
9581066Szelenkov@nginx.com                    "Connection": "close",
9591066Szelenkov@nginx.com                }
9601596Szelenkov@nginx.com            )['status']
9611596Szelenkov@nginx.com            == 200
9621596Szelenkov@nginx.com        ), 'match headers multiple rules 3'
9631596Szelenkov@nginx.com        assert (
9641066Szelenkov@nginx.com            self.get(
9651066Szelenkov@nginx.com                headers={
9661066Szelenkov@nginx.com                    "Host": "localhost",
9671066Szelenkov@nginx.com                    "X-blah": ["test", "blah", "test"],
9681066Szelenkov@nginx.com                    "Connection": "close",
9691066Szelenkov@nginx.com                }
9701596Szelenkov@nginx.com            )['status']
9711596Szelenkov@nginx.com            == 200
9721596Szelenkov@nginx.com        ), 'match headers multiple rules 4'
9731066Szelenkov@nginx.com
9741596Szelenkov@nginx.com        assert (
9751066Szelenkov@nginx.com            self.get(
9761066Szelenkov@nginx.com                headers={
9771066Szelenkov@nginx.com                    "Host": "localhost",
9781066Szelenkov@nginx.com                    "X-blah": ["blah", ""],
9791066Szelenkov@nginx.com                    "Connection": "close",
9801066Szelenkov@nginx.com                }
9811596Szelenkov@nginx.com            )['status']
9821596Szelenkov@nginx.com            == 404
9831596Szelenkov@nginx.com        ), 'match headers multiple rules 5'
9841066Szelenkov@nginx.com
9851066Szelenkov@nginx.com    def test_routes_match_headers_case_insensitive(self):
9861308Szelenkov@nginx.com        self.route_match({"headers": {"X-BLAH": "TEST"}})
9871066Szelenkov@nginx.com
9881596Szelenkov@nginx.com        assert (
9891066Szelenkov@nginx.com            self.get(
9901066Szelenkov@nginx.com                headers={
9911066Szelenkov@nginx.com                    "Host": "localhost",
9921066Szelenkov@nginx.com                    "x-blah": "test",
9931066Szelenkov@nginx.com                    "Connection": "close",
9941066Szelenkov@nginx.com                }
9951596Szelenkov@nginx.com            )['status']
9961596Szelenkov@nginx.com            == 200
9971596Szelenkov@nginx.com        ), 'match headers case insensitive'
9981066Szelenkov@nginx.com
9991066Szelenkov@nginx.com    def test_routes_match_headers_invalid(self):
10001308Szelenkov@nginx.com        self.route_match_invalid({"headers": ["blah"]})
10011308Szelenkov@nginx.com        self.route_match_invalid({"headers": {"foo": ["bar", {}]}})
10021308Szelenkov@nginx.com        self.route_match_invalid({"headers": {"": "blah"}})
10031066Szelenkov@nginx.com
10041066Szelenkov@nginx.com    def test_routes_match_headers_empty_rule(self):
10051308Szelenkov@nginx.com        self.route_match({"headers": {"host": ""}})
10061066Szelenkov@nginx.com
10071596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'localhost'
10081308Szelenkov@nginx.com        self.host('', 200)
10091066Szelenkov@nginx.com
10101066Szelenkov@nginx.com    def test_routes_match_headers_empty(self):
10111308Szelenkov@nginx.com        self.route_match({"headers": {}})
10121596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'empty'
10131066Szelenkov@nginx.com
10141308Szelenkov@nginx.com        self.route_match({"headers": []})
10151596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'empty 2'
10161066Szelenkov@nginx.com
10171066Szelenkov@nginx.com    def test_routes_match_headers_rule_array_empty(self):
10181308Szelenkov@nginx.com        self.route_match({"headers": {"blah": []}})
10191066Szelenkov@nginx.com
10201596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'array empty'
10211596Szelenkov@nginx.com        assert (
10221066Szelenkov@nginx.com            self.get(
10231066Szelenkov@nginx.com                headers={
10241066Szelenkov@nginx.com                    "Host": "localhost",
10251066Szelenkov@nginx.com                    "blah": "foo",
10261066Szelenkov@nginx.com                    "Connection": "close",
10271066Szelenkov@nginx.com                }
10281596Szelenkov@nginx.com            )['status']
10291596Szelenkov@nginx.com            == 200
10301596Szelenkov@nginx.com        ), 'match headers rule array empty 2'
10311066Szelenkov@nginx.com
10321066Szelenkov@nginx.com    def test_routes_match_headers_array(self):
10331308Szelenkov@nginx.com        self.route_match(
10341308Szelenkov@nginx.com            {
10351308Szelenkov@nginx.com                "headers": [
10361308Szelenkov@nginx.com                    {"x-header1": "foo*"},
10371308Szelenkov@nginx.com                    {"x-header2": "bar"},
10381308Szelenkov@nginx.com                    {"x-header3": ["foo", "bar"]},
10391308Szelenkov@nginx.com                    {"x-header1": "bar", "x-header4": "foo"},
10401308Szelenkov@nginx.com                ]
10411308Szelenkov@nginx.com            }
10421066Szelenkov@nginx.com        )
10431066Szelenkov@nginx.com
1044*1875Szelenkov@nginx.com        def check_headers(hds):
1045*1875Szelenkov@nginx.com            hds = dict({"Host": "localhost", "Connection": "close"}, **hds)
1046*1875Szelenkov@nginx.com            assert (
1047*1875Szelenkov@nginx.com                self.get(headers=hds)['status'] == 200
1048*1875Szelenkov@nginx.com            ), 'headers array match'
1049*1875Szelenkov@nginx.com
1050*1875Szelenkov@nginx.com        def check_headers_404(hds):
1051*1875Szelenkov@nginx.com            hds = dict({"Host": "localhost", "Connection": "close"}, **hds)
1052*1875Szelenkov@nginx.com            assert (
1053*1875Szelenkov@nginx.com                self.get(headers=hds)['status'] == 404
1054*1875Szelenkov@nginx.com            ), 'headers array no match'
1055*1875Szelenkov@nginx.com
10561596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'match headers array'
1057*1875Szelenkov@nginx.com        check_headers({"x-header1": "foo123"})
1058*1875Szelenkov@nginx.com        check_headers({"x-header2": "bar"})
1059*1875Szelenkov@nginx.com        check_headers({"x-header3": "bar"})
1060*1875Szelenkov@nginx.com        check_headers_404({"x-header1": "bar"})
1061*1875Szelenkov@nginx.com        check_headers({"x-header1": "bar", "x-header4": "foo"})
10621066Szelenkov@nginx.com
10631596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
10641596Szelenkov@nginx.com            'routes/0/match/headers/1'
10651596Szelenkov@nginx.com        ), 'match headers array configure 2'
10661066Szelenkov@nginx.com
1067*1875Szelenkov@nginx.com        check_headers_404({"x-header2": "bar"})
1068*1875Szelenkov@nginx.com        check_headers({"x-header3": "foo"})
10691017Szelenkov@nginx.com
10701067Szelenkov@nginx.com    def test_routes_match_arguments(self):
10711308Szelenkov@nginx.com        self.route_match({"arguments": {"foo": "bar"}})
10721067Szelenkov@nginx.com
10731596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'args'
10741596Szelenkov@nginx.com        assert self.get(url='/?foo=bar')['status'] == 200, 'args 2'
10751596Szelenkov@nginx.com        assert self.get(url='/?foo=bar1')['status'] == 404, 'args 3'
10761596Szelenkov@nginx.com        assert self.get(url='/?1foo=bar')['status'] == 404, 'args 4'
10771596Szelenkov@nginx.com        assert self.get(url='/?Foo=bar')['status'] == 404, 'case'
10781596Szelenkov@nginx.com        assert self.get(url='/?foo=Bar')['status'] == 404, 'case 2'
10791067Szelenkov@nginx.com
10801475Saxel.duch@nginx.com    def test_routes_match_arguments_chars(self):
10811475Saxel.duch@nginx.com        chars = (
10821475Saxel.duch@nginx.com            " !\"%23$%25%26'()*%2B,-./0123456789:;<%3D>?@"
10831475Saxel.duch@nginx.com            "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
10841475Saxel.duch@nginx.com        )
10851475Saxel.duch@nginx.com
10861475Saxel.duch@nginx.com        chars_enc = ""
10871475Saxel.duch@nginx.com        for h1 in ["2", "3", "4", "5", "6", "7"]:
10881596Szelenkov@nginx.com            for h2 in [
10891596Szelenkov@nginx.com                "0",
10901596Szelenkov@nginx.com                "1",
10911596Szelenkov@nginx.com                "2",
10921596Szelenkov@nginx.com                "3",
10931596Szelenkov@nginx.com                "4",
10941596Szelenkov@nginx.com                "5",
10951596Szelenkov@nginx.com                "6",
10961596Szelenkov@nginx.com                "7",
10971596Szelenkov@nginx.com                "8",
10981596Szelenkov@nginx.com                "9",
10991596Szelenkov@nginx.com                "A",
11001596Szelenkov@nginx.com                "B",
11011596Szelenkov@nginx.com                "C",
11021596Szelenkov@nginx.com                "D",
11031596Szelenkov@nginx.com                "E",
11041596Szelenkov@nginx.com                "F",
11051475Saxel.duch@nginx.com            ]:
11061475Saxel.duch@nginx.com                chars_enc += "%" + h1 + h2
11071475Saxel.duch@nginx.com        chars_enc = chars_enc[:-3]
11081475Saxel.duch@nginx.com
11091475Saxel.duch@nginx.com        def check_args(args, query):
11101475Saxel.duch@nginx.com            self.route_match({"arguments": args})
11111596Szelenkov@nginx.com            assert self.get(url='/?' + query)['status'] == 200
11121475Saxel.duch@nginx.com
11131475Saxel.duch@nginx.com        check_args({chars: chars}, chars + '=' + chars)
11141475Saxel.duch@nginx.com        check_args({chars: chars}, chars + '=' + chars_enc)
11151475Saxel.duch@nginx.com        check_args({chars: chars}, chars_enc + '=' + chars)
11161475Saxel.duch@nginx.com        check_args({chars: chars}, chars_enc + '=' + chars_enc)
11171475Saxel.duch@nginx.com        check_args({chars_enc: chars_enc}, chars + '=' + chars)
11181475Saxel.duch@nginx.com        check_args({chars_enc: chars_enc}, chars + '=' + chars_enc)
11191475Saxel.duch@nginx.com        check_args({chars_enc: chars_enc}, chars_enc + '=' + chars)
11201475Saxel.duch@nginx.com        check_args({chars_enc: chars_enc}, chars_enc + '=' + chars_enc)
11211475Saxel.duch@nginx.com
11221067Szelenkov@nginx.com    def test_routes_match_arguments_empty(self):
11231308Szelenkov@nginx.com        self.route_match({"arguments": {}})
11241596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'arguments empty'
11251067Szelenkov@nginx.com
11261308Szelenkov@nginx.com        self.route_match({"arguments": []})
11271596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'arguments empty 2'
11281067Szelenkov@nginx.com
11291067Szelenkov@nginx.com    def test_routes_match_arguments_space(self):
11301475Saxel.duch@nginx.com        self.route_match({"arguments": {"+fo o%20": "%20b+a r"}})
11311596Szelenkov@nginx.com        assert self.get(url='/? fo o = b a r&')['status'] == 200
11321596Szelenkov@nginx.com        assert self.get(url='/?+fo+o+=+b+a+r&')['status'] == 200
11331596Szelenkov@nginx.com        assert self.get(url='/?%20fo%20o%20=%20b%20a%20r&')['status'] == 200
11341067Szelenkov@nginx.com
11351475Saxel.duch@nginx.com        self.route_match({"arguments": {"%20foo": " bar"}})
11361596Szelenkov@nginx.com        assert self.get(url='/? foo= bar')['status'] == 200
11371596Szelenkov@nginx.com        assert self.get(url='/?+foo=+bar')['status'] == 200
11381596Szelenkov@nginx.com        assert self.get(url='/?%20foo=%20bar')['status'] == 200
11391596Szelenkov@nginx.com        assert self.get(url='/?+foo= bar')['status'] == 200
11401596Szelenkov@nginx.com        assert self.get(url='/?%20foo=+bar')['status'] == 200
11411475Saxel.duch@nginx.com
11421475Saxel.duch@nginx.com    def test_routes_match_arguments_equal(self):
11431475Saxel.duch@nginx.com        self.route_match({"arguments": {"=": "="}})
11441596Szelenkov@nginx.com        assert self.get(url='/?%3D=%3D')['status'] == 200
11451596Szelenkov@nginx.com        assert self.get(url='/?%3D==')['status'] == 200
11461596Szelenkov@nginx.com        assert self.get(url='/?===')['status'] == 404
11471596Szelenkov@nginx.com        assert self.get(url='/?%3D%3D%3D')['status'] == 404
11481596Szelenkov@nginx.com        assert self.get(url='/?==%3D')['status'] == 404
11491475Saxel.duch@nginx.com
11501475Saxel.duch@nginx.com    def test_routes_match_arguments_enc(self):
11511475Saxel.duch@nginx.com        self.route_match({"arguments": {"Ю": "н"}})
11521596Szelenkov@nginx.com        assert self.get(url='/?%D0%AE=%D0%BD')['status'] == 200
11531596Szelenkov@nginx.com        assert self.get(url='/?%d0%ae=%d0%Bd')['status'] == 200
11541475Saxel.duch@nginx.com
11551475Saxel.duch@nginx.com    def test_routes_match_arguments_hash(self):
11561475Saxel.duch@nginx.com        self.route_match({"arguments": {"#": "#"}})
11571596Szelenkov@nginx.com        assert self.get(url='/?%23=%23')['status'] == 200
11581596Szelenkov@nginx.com        assert self.get(url='/?%23=%23#')['status'] == 200
11591596Szelenkov@nginx.com        assert self.get(url='/?#=#')['status'] == 404
11601596Szelenkov@nginx.com        assert self.get(url='/?%23=#')['status'] == 404
11611475Saxel.duch@nginx.com
11621475Saxel.duch@nginx.com    def test_routes_match_arguments_wildcard(self):
11631475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "*"}})
11641596Szelenkov@nginx.com        assert self.get(url='/?foo')['status'] == 200
11651596Szelenkov@nginx.com        assert self.get(url='/?foo=')['status'] == 200
11661596Szelenkov@nginx.com        assert self.get(url='/?foo=blah')['status'] == 200
11671596Szelenkov@nginx.com        assert self.get(url='/?blah=foo')['status'] == 404
11681475Saxel.duch@nginx.com
11691475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "%25*"}})
11701596Szelenkov@nginx.com        assert self.get(url='/?foo=%xx')['status'] == 200
11711475Saxel.duch@nginx.com
11721475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "%2A*"}})
11731596Szelenkov@nginx.com        assert self.get(url='/?foo=*xx')['status'] == 200
11741596Szelenkov@nginx.com        assert self.get(url='/?foo=xx')['status'] == 404
11751475Saxel.duch@nginx.com
11761475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "*%2A"}})
11771596Szelenkov@nginx.com        assert self.get(url='/?foo=xx*')['status'] == 200
11781596Szelenkov@nginx.com        assert self.get(url='/?foo=xx*x')['status'] == 404
11791067Szelenkov@nginx.com
11801475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "1*2"}})
11811596Szelenkov@nginx.com        assert self.get(url='/?foo=12')['status'] == 200
11821596Szelenkov@nginx.com        assert self.get(url='/?foo=1blah2')['status'] == 200
11831596Szelenkov@nginx.com        assert self.get(url='/?foo=1%2A2')['status'] == 200
11841596Szelenkov@nginx.com        assert self.get(url='/?foo=x12')['status'] == 404
11851475Saxel.duch@nginx.com
11861475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "bar*", "%25": "%25"}})
11871596Szelenkov@nginx.com        assert self.get(url='/?foo=barxx&%=%')['status'] == 200
11881596Szelenkov@nginx.com        assert self.get(url='/?foo=barxx&x%=%')['status'] == 404
11891475Saxel.duch@nginx.com
11901475Saxel.duch@nginx.com    def test_routes_match_arguments_negative(self):
11911633Svbart@nginx.com        self.route_match({"arguments": {"foo": "!"}})
11921633Svbart@nginx.com        assert self.get(url='/?bar')['status'] == 404
11931633Svbart@nginx.com        assert self.get(url='/?foo')['status'] == 404
11941633Svbart@nginx.com        assert self.get(url='/?foo=')['status'] == 404
11951633Svbart@nginx.com        assert self.get(url='/?foo=%25')['status'] == 200
11961633Svbart@nginx.com
11971633Svbart@nginx.com        self.route_match({"arguments": {"foo": "!*"}})
11981633Svbart@nginx.com        assert self.get(url='/?bar')['status'] == 404
11991633Svbart@nginx.com        assert self.get(url='/?foo')['status'] == 404
12001633Svbart@nginx.com        assert self.get(url='/?foo=')['status'] == 404
12011633Svbart@nginx.com        assert self.get(url='/?foo=blah')['status'] == 404
12021633Svbart@nginx.com
12031475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "!%25"}})
12041596Szelenkov@nginx.com        assert self.get(url='/?foo=blah')['status'] == 200
12051596Szelenkov@nginx.com        assert self.get(url='/?foo=%')['status'] == 404
12061475Saxel.duch@nginx.com
12071475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "%21blah"}})
12081596Szelenkov@nginx.com        assert self.get(url='/?foo=%21blah')['status'] == 200
12091596Szelenkov@nginx.com        assert self.get(url='/?foo=!blah')['status'] == 200
12101596Szelenkov@nginx.com        assert self.get(url='/?foo=bar')['status'] == 404
12111475Saxel.duch@nginx.com
12121475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "!!%21*a"}})
12131596Szelenkov@nginx.com        assert self.get(url='/?foo=blah')['status'] == 200
12141596Szelenkov@nginx.com        assert self.get(url='/?foo=!blah')['status'] == 200
12151596Szelenkov@nginx.com        assert self.get(url='/?foo=!!a')['status'] == 404
12161596Szelenkov@nginx.com        assert self.get(url='/?foo=!!bla')['status'] == 404
12171067Szelenkov@nginx.com
12181475Saxel.duch@nginx.com    def test_routes_match_arguments_percent(self):
12191475Saxel.duch@nginx.com        self.route_match({"arguments": {"%25": "%25"}})
12201596Szelenkov@nginx.com        assert self.get(url='/?%=%')['status'] == 200
12211596Szelenkov@nginx.com        assert self.get(url='/?%25=%25')['status'] == 200
12221596Szelenkov@nginx.com        assert self.get(url='/?%25=%')['status'] == 200
12231475Saxel.duch@nginx.com
12241475Saxel.duch@nginx.com        self.route_match({"arguments": {"%251": "%252"}})
12251596Szelenkov@nginx.com        assert self.get(url='/?%1=%2')['status'] == 200
12261596Szelenkov@nginx.com        assert self.get(url='/?%251=%252')['status'] == 200
12271596Szelenkov@nginx.com        assert self.get(url='/?%251=%2')['status'] == 200
12281067Szelenkov@nginx.com
12291475Saxel.duch@nginx.com        self.route_match({"arguments": {"%25%21%251": "%25%24%252"}})
12301596Szelenkov@nginx.com        assert self.get(url='/?%!%1=%$%2')['status'] == 200
12311596Szelenkov@nginx.com        assert self.get(url='/?%25!%251=%25$%252')['status'] == 200
12321596Szelenkov@nginx.com        assert self.get(url='/?%25!%1=%$%2')['status'] == 200
12331475Saxel.duch@nginx.com
12341475Saxel.duch@nginx.com    def test_routes_match_arguments_ampersand(self):
12351475Saxel.duch@nginx.com        self.route_match({"arguments": {"foo": "&"}})
12361596Szelenkov@nginx.com        assert self.get(url='/?foo=%26')['status'] == 200
12371596Szelenkov@nginx.com        assert self.get(url='/?foo=%26&')['status'] == 200
12381596Szelenkov@nginx.com        assert self.get(url='/?foo=%26%26')['status'] == 404
12391596Szelenkov@nginx.com        assert self.get(url='/?foo=&')['status'] == 404
12401475Saxel.duch@nginx.com
12411475Saxel.duch@nginx.com        self.route_match({"arguments": {"&": ""}})
12421596Szelenkov@nginx.com        assert self.get(url='/?%26=')['status'] == 200
12431596Szelenkov@nginx.com        assert self.get(url='/?%26=&')['status'] == 200
12441596Szelenkov@nginx.com        assert self.get(url='/?%26=%26')['status'] == 404
12451596Szelenkov@nginx.com        assert self.get(url='/?&=')['status'] == 404
12461067Szelenkov@nginx.com
12471067Szelenkov@nginx.com    def test_routes_match_arguments_complex(self):
12481308Szelenkov@nginx.com        self.route_match({"arguments": {"foo": ""}})
12491067Szelenkov@nginx.com
12501596Szelenkov@nginx.com        assert self.get(url='/?foo')['status'] == 200, 'complex'
12511596Szelenkov@nginx.com        assert self.get(url='/?blah=blah&foo=')['status'] == 200, 'complex 2'
12521596Szelenkov@nginx.com        assert self.get(url='/?&&&foo&&&')['status'] == 200, 'complex 3'
12531596Szelenkov@nginx.com        assert self.get(url='/?foo&foo=bar&foo')['status'] == 404, 'complex 4'
12541596Szelenkov@nginx.com        assert self.get(url='/?foo=&foo')['status'] == 200, 'complex 5'
12551596Szelenkov@nginx.com        assert self.get(url='/?&=&foo&==&')['status'] == 200, 'complex 6'
12561596Szelenkov@nginx.com        assert self.get(url='/?&=&bar&==&')['status'] == 404, 'complex 7'
12571067Szelenkov@nginx.com
12581067Szelenkov@nginx.com    def test_routes_match_arguments_multiple(self):
12591308Szelenkov@nginx.com        self.route_match({"arguments": {"foo": "bar", "blah": "test"}})
12601067Szelenkov@nginx.com
12611596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'multiple'
12621596Szelenkov@nginx.com        assert (
12631596Szelenkov@nginx.com            self.get(url='/?foo=bar&blah=test')['status'] == 200
12641596Szelenkov@nginx.com        ), 'multiple 2'
12651596Szelenkov@nginx.com        assert self.get(url='/?foo=bar&blah')['status'] == 404, 'multiple 3'
12661596Szelenkov@nginx.com        assert (
12671596Szelenkov@nginx.com            self.get(url='/?foo=bar&blah=tes')['status'] == 404
12681596Szelenkov@nginx.com        ), 'multiple 4'
12691596Szelenkov@nginx.com        assert (
12701596Szelenkov@nginx.com            self.get(url='/?foo=b%61r&bl%61h=t%65st')['status'] == 200
12711596Szelenkov@nginx.com        ), 'multiple 5'
12721067Szelenkov@nginx.com
12731067Szelenkov@nginx.com    def test_routes_match_arguments_multiple_rules(self):
12741308Szelenkov@nginx.com        self.route_match({"arguments": {"foo": ["bar", "blah"]}})
12751067Szelenkov@nginx.com
12761596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'rules'
12771596Szelenkov@nginx.com        assert self.get(url='/?foo=bar')['status'] == 200, 'rules 2'
12781596Szelenkov@nginx.com        assert self.get(url='/?foo=blah')['status'] == 200, 'rules 3'
12791596Szelenkov@nginx.com        assert (
12801596Szelenkov@nginx.com            self.get(url='/?foo=blah&foo=bar&foo=blah')['status'] == 200
12811596Szelenkov@nginx.com        ), 'rules 4'
12821596Szelenkov@nginx.com        assert (
12831596Szelenkov@nginx.com            self.get(url='/?foo=blah&foo=bar&foo=')['status'] == 404
12841596Szelenkov@nginx.com        ), 'rules 5'
12851067Szelenkov@nginx.com
12861067Szelenkov@nginx.com    def test_routes_match_arguments_array(self):
12871308Szelenkov@nginx.com        self.route_match(
12881308Szelenkov@nginx.com            {
12891308Szelenkov@nginx.com                "arguments": [
12901308Szelenkov@nginx.com                    {"var1": "val1*"},
12911308Szelenkov@nginx.com                    {"var2": "val2"},
12921308Szelenkov@nginx.com                    {"var3": ["foo", "bar"]},
12931308Szelenkov@nginx.com                    {"var1": "bar", "var4": "foo"},
12941308Szelenkov@nginx.com                ]
12951308Szelenkov@nginx.com            }
12961067Szelenkov@nginx.com        )
12971067Szelenkov@nginx.com
12981596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'arr'
12991596Szelenkov@nginx.com        assert self.get(url='/?var1=val123')['status'] == 200, 'arr 2'
13001596Szelenkov@nginx.com        assert self.get(url='/?var2=val2')['status'] == 200, 'arr 3'
13011596Szelenkov@nginx.com        assert self.get(url='/?var3=bar')['status'] == 200, 'arr 4'
13021596Szelenkov@nginx.com        assert self.get(url='/?var1=bar')['status'] == 404, 'arr 5'
13031596Szelenkov@nginx.com        assert self.get(url='/?var1=bar&var4=foo')['status'] == 200, 'arr 6'
13041067Szelenkov@nginx.com
13051596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
13061596Szelenkov@nginx.com            'routes/0/match/arguments/1'
13071596Szelenkov@nginx.com        ), 'match arguments array configure 2'
13081067Szelenkov@nginx.com
13091596Szelenkov@nginx.com        assert self.get(url='/?var2=val2')['status'] == 404, 'arr 7'
13101596Szelenkov@nginx.com        assert self.get(url='/?var3=foo')['status'] == 200, 'arr 8'
13111067Szelenkov@nginx.com
13121846Szelenkov@nginx.com    def test_routes_match_arguments_invalid(self):
13131475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": ["var"]})
13141475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": [{"var1": {}}]})
13151475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"": "bar"}})
13161475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"foo": "%"}})
13171475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"foo": "%1G"}})
13181475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"%": "bar"}})
13191475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"foo": "%0"}})
13201475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"foo": "%%1F"}})
13211475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"%%1F": ""}})
13221475Saxel.duch@nginx.com        self.route_match_invalid({"arguments": {"%7%F": ""}})
13231475Saxel.duch@nginx.com
13241068Szelenkov@nginx.com    def test_routes_match_cookies(self):
13251308Szelenkov@nginx.com        self.route_match({"cookies": {"foO": "bar"}})
13261068Szelenkov@nginx.com
13271596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'cookie'
13281308Szelenkov@nginx.com        self.cookie('foO=bar', 200)
13291308Szelenkov@nginx.com        self.cookie('foO=bar;1', 200)
13301308Szelenkov@nginx.com        self.cookie(['foO=bar', 'blah=blah'], 200)
13311308Szelenkov@nginx.com        self.cookie('foO=bar; blah=blah', 200)
13321308Szelenkov@nginx.com        self.cookie('Foo=bar', 404)
13331308Szelenkov@nginx.com        self.cookie('foO=Bar', 404)
13341308Szelenkov@nginx.com        self.cookie('foO=bar1', 404)
13351308Szelenkov@nginx.com        self.cookie('1foO=bar;', 404)
13361068Szelenkov@nginx.com
13371068Szelenkov@nginx.com    def test_routes_match_cookies_empty(self):
13381308Szelenkov@nginx.com        self.route_match({"cookies": {}})
13391596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'cookies empty'
13401068Szelenkov@nginx.com
13411308Szelenkov@nginx.com        self.route_match({"cookies": []})
13421596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'cookies empty 2'
13431068Szelenkov@nginx.com
13441068Szelenkov@nginx.com    def test_routes_match_cookies_invalid(self):
13451308Szelenkov@nginx.com        self.route_match_invalid({"cookies": ["var"]})
13461308Szelenkov@nginx.com        self.route_match_invalid({"cookies": [{"foo": {}}]})
13471068Szelenkov@nginx.com
13481068Szelenkov@nginx.com    def test_routes_match_cookies_multiple(self):
13491308Szelenkov@nginx.com        self.route_match({"cookies": {"foo": "bar", "blah": "blah"}})
13501308Szelenkov@nginx.com
13511596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'multiple'
13521308Szelenkov@nginx.com        self.cookie('foo=bar; blah=blah', 200)
13531308Szelenkov@nginx.com        self.cookie(['foo=bar', 'blah=blah'], 200)
13541308Szelenkov@nginx.com        self.cookie(['foo=bar; blah', 'blah'], 404)
13551308Szelenkov@nginx.com        self.cookie(['foo=bar; blah=test', 'blah=blah'], 404)
13561068Szelenkov@nginx.com
13571308Szelenkov@nginx.com    def test_routes_match_cookies_multiple_values(self):
13581308Szelenkov@nginx.com        self.route_match({"cookies": {"blah": "blah"}})
13591308Szelenkov@nginx.com
13601308Szelenkov@nginx.com        self.cookie(['blah=blah', 'blah=blah', 'blah=blah'], 200)
13611308Szelenkov@nginx.com        self.cookie(['blah=blah', 'blah=test', 'blah=blah'], 404)
13621308Szelenkov@nginx.com        self.cookie(['blah=blah; blah=', 'blah=blah'], 404)
13631308Szelenkov@nginx.com
13641308Szelenkov@nginx.com    def test_routes_match_cookies_multiple_rules(self):
13651308Szelenkov@nginx.com        self.route_match({"cookies": {"blah": ["test", "blah"]}})
13661068Szelenkov@nginx.com
13671596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'multiple rules'
13681308Szelenkov@nginx.com        self.cookie('blah=test', 200)
13691308Szelenkov@nginx.com        self.cookie('blah=blah', 200)
13701308Szelenkov@nginx.com        self.cookie(['blah=blah', 'blah=test', 'blah=blah'], 200)
13711308Szelenkov@nginx.com        self.cookie(['blah=blah; blah=test', 'blah=blah'], 200)
13721596Szelenkov@nginx.com        self.cookie(['blah=blah', 'blah'], 200)  # invalid cookie
13731068Szelenkov@nginx.com
13741308Szelenkov@nginx.com    def test_routes_match_cookies_array(self):
13751308Szelenkov@nginx.com        self.route_match(
13761308Szelenkov@nginx.com            {
13771308Szelenkov@nginx.com                "cookies": [
13781308Szelenkov@nginx.com                    {"var1": "val1*"},
13791308Szelenkov@nginx.com                    {"var2": "val2"},
13801308Szelenkov@nginx.com                    {"var3": ["foo", "bar"]},
13811308Szelenkov@nginx.com                    {"var1": "bar", "var4": "foo"},
13821308Szelenkov@nginx.com                ]
13831308Szelenkov@nginx.com            }
13841068Szelenkov@nginx.com        )
13851068Szelenkov@nginx.com
13861596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'cookies array'
13871308Szelenkov@nginx.com        self.cookie('var1=val123', 200)
13881308Szelenkov@nginx.com        self.cookie('var2=val2', 200)
13891308Szelenkov@nginx.com        self.cookie(' var2=val2 ', 200)
13901308Szelenkov@nginx.com        self.cookie('var3=bar', 200)
13911308Szelenkov@nginx.com        self.cookie('var3=bar;', 200)
13921308Szelenkov@nginx.com        self.cookie('var1=bar', 404)
13931308Szelenkov@nginx.com        self.cookie('var1=bar; var4=foo;', 200)
13941308Szelenkov@nginx.com        self.cookie(['var1=bar', 'var4=foo'], 200)
13951068Szelenkov@nginx.com
13961596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
13971596Szelenkov@nginx.com            'routes/0/match/cookies/1'
13981596Szelenkov@nginx.com        ), 'match cookies array configure 2'
13991068Szelenkov@nginx.com
14001308Szelenkov@nginx.com        self.cookie('var2=val2', 404)
14011308Szelenkov@nginx.com        self.cookie('var3=foo', 200)
14021068Szelenkov@nginx.com
14031110Saxel.duch@nginx.com    def test_routes_match_scheme(self):
14041308Szelenkov@nginx.com        self.route_match({"scheme": "http"})
14051308Szelenkov@nginx.com        self.route_match({"scheme": "https"})
14061308Szelenkov@nginx.com        self.route_match({"scheme": "HtTp"})
14071308Szelenkov@nginx.com        self.route_match({"scheme": "HtTpS"})
14081110Saxel.duch@nginx.com
14091110Saxel.duch@nginx.com    def test_routes_match_scheme_invalid(self):
14101308Szelenkov@nginx.com        self.route_match_invalid({"scheme": ["http"]})
14111308Szelenkov@nginx.com        self.route_match_invalid({"scheme": "ftp"})
14121308Szelenkov@nginx.com        self.route_match_invalid({"scheme": "ws"})
14131308Szelenkov@nginx.com        self.route_match_invalid({"scheme": "*"})
14141308Szelenkov@nginx.com        self.route_match_invalid({"scheme": ""})
14151308Szelenkov@nginx.com
14161325Saxel.duch@nginx.com    def test_routes_source_port(self):
14171325Saxel.duch@nginx.com        def sock_port():
14181325Saxel.duch@nginx.com            _, sock = self.http(b'', start=True, raw=True, no_recv=True)
14191325Saxel.duch@nginx.com            port = sock.getsockname()[1]
14201325Saxel.duch@nginx.com            return (sock, port)
14211325Saxel.duch@nginx.com
14221325Saxel.duch@nginx.com        sock, port = sock_port()
14231325Saxel.duch@nginx.com        sock2, port2 = sock_port()
14241325Saxel.duch@nginx.com
14251325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.1:" + str(port)})
14261596Szelenkov@nginx.com        assert self.get(sock=sock)['status'] == 200, 'exact'
14271596Szelenkov@nginx.com        assert self.get(sock=sock2)['status'] == 404, 'exact 2'
14281325Saxel.duch@nginx.com
14291325Saxel.duch@nginx.com        sock, port = sock_port()
14301325Saxel.duch@nginx.com        sock2, port2 = sock_port()
14311325Saxel.duch@nginx.com
14321325Saxel.duch@nginx.com        self.route_match({"source": "!127.0.0.1:" + str(port)})
14331596Szelenkov@nginx.com        assert self.get(sock=sock)['status'] == 404, 'negative'
14341596Szelenkov@nginx.com        assert self.get(sock=sock2)['status'] == 200, 'negative 2'
14351325Saxel.duch@nginx.com
14361325Saxel.duch@nginx.com        sock, port = sock_port()
14371325Saxel.duch@nginx.com        sock2, port2 = sock_port()
14381325Saxel.duch@nginx.com
14391391Szelenkov@nginx.com        self.route_match({"source": ["*:" + str(port), "!127.0.0.1"]})
14401596Szelenkov@nginx.com        assert self.get(sock=sock)['status'] == 404, 'negative 3'
14411596Szelenkov@nginx.com        assert self.get(sock=sock2)['status'] == 404, 'negative 4'
14421391Szelenkov@nginx.com
14431391Szelenkov@nginx.com        sock, port = sock_port()
14441391Szelenkov@nginx.com        sock2, port2 = sock_port()
14451391Szelenkov@nginx.com
14461325Saxel.duch@nginx.com        self.route_match(
14471325Saxel.duch@nginx.com            {"source": "127.0.0.1:" + str(port) + "-" + str(port)}
14481325Saxel.duch@nginx.com        )
14491596Szelenkov@nginx.com        assert self.get(sock=sock)['status'] == 200, 'range single'
14501596Szelenkov@nginx.com        assert self.get(sock=sock2)['status'] == 404, 'range single 2'
14511325Saxel.duch@nginx.com
14521325Saxel.duch@nginx.com        socks = [
14531325Saxel.duch@nginx.com            sock_port(),
14541325Saxel.duch@nginx.com            sock_port(),
14551325Saxel.duch@nginx.com            sock_port(),
14561325Saxel.duch@nginx.com            sock_port(),
14571325Saxel.duch@nginx.com            sock_port(),
14581325Saxel.duch@nginx.com        ]
14591325Saxel.duch@nginx.com        socks.sort(key=lambda sock: sock[1])
14601325Saxel.duch@nginx.com
14611325Saxel.duch@nginx.com        self.route_match(
14621325Saxel.duch@nginx.com            {
14631325Saxel.duch@nginx.com                "source": "127.0.0.1:"
14641325Saxel.duch@nginx.com                + str(socks[1][1])  # second port number
14651325Saxel.duch@nginx.com                + "-"
14661325Saxel.duch@nginx.com                + str(socks[3][1])  # fourth port number
14671325Saxel.duch@nginx.com            }
14681325Saxel.duch@nginx.com        )
14691596Szelenkov@nginx.com        assert self.get(sock=socks[0][0])['status'] == 404, 'range'
14701596Szelenkov@nginx.com        assert self.get(sock=socks[1][0])['status'] == 200, 'range 2'
14711596Szelenkov@nginx.com        assert self.get(sock=socks[2][0])['status'] == 200, 'range 3'
14721596Szelenkov@nginx.com        assert self.get(sock=socks[3][0])['status'] == 200, 'range 4'
14731596Szelenkov@nginx.com        assert self.get(sock=socks[4][0])['status'] == 404, 'range 5'
14741325Saxel.duch@nginx.com
14751325Saxel.duch@nginx.com        socks = [
14761325Saxel.duch@nginx.com            sock_port(),
14771325Saxel.duch@nginx.com            sock_port(),
14781325Saxel.duch@nginx.com            sock_port(),
14791325Saxel.duch@nginx.com        ]
14801325Saxel.duch@nginx.com        socks.sort(key=lambda sock: sock[1])
14811325Saxel.duch@nginx.com
14821325Saxel.duch@nginx.com        self.route_match(
14831325Saxel.duch@nginx.com            {
14841325Saxel.duch@nginx.com                "source": [
14851325Saxel.duch@nginx.com                    "127.0.0.1:" + str(socks[0][1]),
14861325Saxel.duch@nginx.com                    "127.0.0.1:" + str(socks[2][1]),
14871325Saxel.duch@nginx.com                ]
14881325Saxel.duch@nginx.com            }
14891325Saxel.duch@nginx.com        )
14901596Szelenkov@nginx.com        assert self.get(sock=socks[0][0])['status'] == 200, 'array'
14911596Szelenkov@nginx.com        assert self.get(sock=socks[1][0])['status'] == 404, 'array 2'
14921596Szelenkov@nginx.com        assert self.get(sock=socks[2][0])['status'] == 200, 'array 3'
14931325Saxel.duch@nginx.com
14941325Saxel.duch@nginx.com    def test_routes_source_addr(self):
14951596Szelenkov@nginx.com        assert 'success' in self.conf(
14961596Szelenkov@nginx.com            {"*:7080": {"pass": "routes"}, "[::1]:7081": {"pass": "routes"},},
14971596Szelenkov@nginx.com            'listeners',
14981596Szelenkov@nginx.com        ), 'source listeners configure'
14991325Saxel.duch@nginx.com
15001325Saxel.duch@nginx.com        def get_ipv6():
15011325Saxel.duch@nginx.com            return self.get(sock_type='ipv6', port=7081)
15021325Saxel.duch@nginx.com
15031325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.1"})
15041596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'exact'
15051596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'exact ipv6'
15061325Saxel.duch@nginx.com
15071325Saxel.duch@nginx.com        self.route_match({"source": ["127.0.0.1"]})
15081596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'exact 2'
15091596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'exact 2 ipv6'
15101325Saxel.duch@nginx.com
15111325Saxel.duch@nginx.com        self.route_match({"source": "!127.0.0.1"})
15121596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'exact neg'
15131596Szelenkov@nginx.com        assert get_ipv6()['status'] == 200, 'exact neg ipv6'
15141325Saxel.duch@nginx.com
15151325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.2"})
15161596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'exact 3'
15171596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'exact 3 ipv6'
15181325Saxel.duch@nginx.com
15191325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.1-127.0.0.1"})
15201596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'range single'
15211596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range single ipv6'
15221325Saxel.duch@nginx.com
15231325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.2-127.0.0.2"})
15241596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'range single 2'
15251596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range single 2 ipv6'
15261325Saxel.duch@nginx.com
15271325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.2-127.0.0.3"})
15281596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'range'
15291596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range ipv6'
15301325Saxel.duch@nginx.com
15311325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.1-127.0.0.2"})
15321596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'range 2'
15331596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range 2 ipv6'
15341325Saxel.duch@nginx.com
15351325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.0-127.0.0.2"})
15361596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'range 3'
15371596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range 3 ipv6'
15381325Saxel.duch@nginx.com
15391325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.0-127.0.0.1"})
15401596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'range 4'
15411596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range 4 ipv6'
15421325Saxel.duch@nginx.com
15431325Saxel.duch@nginx.com        self.route_match({"source": "126.0.0.0-127.0.0.0"})
15441596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'range 5'
15451596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range 5 ipv6'
15461325Saxel.duch@nginx.com
15471325Saxel.duch@nginx.com        self.route_match({"source": "126.126.126.126-127.0.0.2"})
15481596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'range 6'
15491596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, 'range 6 ipv6'
15501325Saxel.duch@nginx.com
15511325Saxel.duch@nginx.com    def test_routes_source_ipv6(self):
15521596Szelenkov@nginx.com        assert 'success' in self.conf(
15531596Szelenkov@nginx.com            {
15541596Szelenkov@nginx.com                "[::1]:7080": {"pass": "routes"},
15551596Szelenkov@nginx.com                "127.0.0.1:7081": {"pass": "routes"},
15561596Szelenkov@nginx.com            },
15571596Szelenkov@nginx.com            'listeners',
15581596Szelenkov@nginx.com        ), 'source listeners configure'
15591325Saxel.duch@nginx.com
15601325Saxel.duch@nginx.com        self.route_match({"source": "::1"})
15611596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, 'exact'
15621596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'exact ipv4'
15631325Saxel.duch@nginx.com
15641325Saxel.duch@nginx.com        self.route_match({"source": ["::1"]})
15651596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, 'exact 2'
15661596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'exact 2 ipv4'
15671325Saxel.duch@nginx.com
15681325Saxel.duch@nginx.com        self.route_match({"source": "!::1"})
15691596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 404, 'exact neg'
15701596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 200, 'exact neg ipv4'
15711325Saxel.duch@nginx.com
15721325Saxel.duch@nginx.com        self.route_match({"source": "::2"})
15731596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 404, 'exact 3'
15741596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'exact 3 ipv4'
15751325Saxel.duch@nginx.com
15761325Saxel.duch@nginx.com        self.route_match({"source": "::1-::1"})
15771596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, 'range'
15781596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'range ipv4'
15791325Saxel.duch@nginx.com
15801325Saxel.duch@nginx.com        self.route_match({"source": "::2-::2"})
15811596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 404, 'range 2'
15821596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'range 2 ipv4'
15831325Saxel.duch@nginx.com
15841325Saxel.duch@nginx.com        self.route_match({"source": "::2-::3"})
15851596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 404, 'range 3'
15861596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'range 3 ipv4'
15871325Saxel.duch@nginx.com
15881325Saxel.duch@nginx.com        self.route_match({"source": "::1-::2"})
15891596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, 'range 4'
15901596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'range 4 ipv4'
15911325Saxel.duch@nginx.com
15921325Saxel.duch@nginx.com        self.route_match({"source": "::0-::2"})
15931596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, 'range 5'
15941596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'range 5 ipv4'
15951325Saxel.duch@nginx.com
15961325Saxel.duch@nginx.com        self.route_match({"source": "::0-::1"})
15971596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, 'range 6'
15981596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'range 6 ipv4'
15991325Saxel.duch@nginx.com
16001325Saxel.duch@nginx.com    def test_routes_source_cidr(self):
16011596Szelenkov@nginx.com        assert 'success' in self.conf(
16021596Szelenkov@nginx.com            {"*:7080": {"pass": "routes"}, "[::1]:7081": {"pass": "routes"},},
16031596Szelenkov@nginx.com            'listeners',
16041596Szelenkov@nginx.com        ), 'source listeners configure'
16051325Saxel.duch@nginx.com
16061325Saxel.duch@nginx.com        def get_ipv6():
16071325Saxel.duch@nginx.com            return self.get(sock_type='ipv6', port=7081)
16081325Saxel.duch@nginx.com
16091325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.1/32"})
16101596Szelenkov@nginx.com        assert self.get()['status'] == 200, '32'
16111596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, '32 ipv6'
16121325Saxel.duch@nginx.com
16131325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.0/32"})
16141596Szelenkov@nginx.com        assert self.get()['status'] == 404, '32 2'
16151596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, '32 2 ipv6'
16161325Saxel.duch@nginx.com
16171325Saxel.duch@nginx.com        self.route_match({"source": "127.0.0.0/31"})
16181596Szelenkov@nginx.com        assert self.get()['status'] == 200, '31'
16191596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, '31 ipv6'
16201325Saxel.duch@nginx.com
16211325Saxel.duch@nginx.com        self.route_match({"source": "0.0.0.0/1"})
16221596Szelenkov@nginx.com        assert self.get()['status'] == 200, '1'
16231596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, '1 ipv6'
16241325Saxel.duch@nginx.com
16251325Saxel.duch@nginx.com        self.route_match({"source": "0.0.0.0/0"})
16261596Szelenkov@nginx.com        assert self.get()['status'] == 200, '0'
16271596Szelenkov@nginx.com        assert get_ipv6()['status'] == 404, '0 ipv6'
16281325Saxel.duch@nginx.com
16291325Saxel.duch@nginx.com    def test_routes_source_cidr_ipv6(self):
16301596Szelenkov@nginx.com        assert 'success' in self.conf(
16311596Szelenkov@nginx.com            {
16321596Szelenkov@nginx.com                "[::1]:7080": {"pass": "routes"},
16331596Szelenkov@nginx.com                "127.0.0.1:7081": {"pass": "routes"},
16341596Szelenkov@nginx.com            },
16351596Szelenkov@nginx.com            'listeners',
16361596Szelenkov@nginx.com        ), 'source listeners configure'
16371325Saxel.duch@nginx.com
16381325Saxel.duch@nginx.com        self.route_match({"source": "::1/128"})
16391596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, '128'
16401596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, '128 ipv4'
16411325Saxel.duch@nginx.com
16421325Saxel.duch@nginx.com        self.route_match({"source": "::0/128"})
16431596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 404, '128 2'
16441596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, '128 ipv4'
16451325Saxel.duch@nginx.com
16461325Saxel.duch@nginx.com        self.route_match({"source": "::0/127"})
16471596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, '127'
16481596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, '127 ipv4'
16491325Saxel.duch@nginx.com
16501325Saxel.duch@nginx.com        self.route_match({"source": "::0/32"})
16511596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, '32'
16521596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, '32 ipv4'
16531325Saxel.duch@nginx.com
16541325Saxel.duch@nginx.com        self.route_match({"source": "::0/1"})
16551596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, '1'
16561596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, '1 ipv4'
16571325Saxel.duch@nginx.com
16581325Saxel.duch@nginx.com        self.route_match({"source": "::/0"})
16591596Szelenkov@nginx.com        assert self.get(sock_type='ipv6')['status'] == 200, '0'
16601596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, '0 ipv4'
16611325Saxel.duch@nginx.com
16621654Szelenkov@nginx.com    def test_routes_source_unix(self, temp_dir):
16631654Szelenkov@nginx.com        addr = temp_dir + '/sock'
16641325Saxel.duch@nginx.com
16651596Szelenkov@nginx.com        assert 'success' in self.conf(
16661596Szelenkov@nginx.com            {"unix:" + addr: {"pass": "routes"}}, 'listeners'
16671596Szelenkov@nginx.com        ), 'source listeners configure'
16681325Saxel.duch@nginx.com
16691325Saxel.duch@nginx.com        self.route_match({"source": "!0.0.0.0/0"})
16701596Szelenkov@nginx.com        assert (
16711596Szelenkov@nginx.com            self.get(sock_type='unix', addr=addr)['status'] == 200
16721596Szelenkov@nginx.com        ), 'unix ipv4'
16731325Saxel.duch@nginx.com
16741325Saxel.duch@nginx.com        self.route_match({"source": "!::/0"})
16751596Szelenkov@nginx.com        assert (
16761596Szelenkov@nginx.com            self.get(sock_type='unix', addr=addr)['status'] == 200
16771596Szelenkov@nginx.com        ), 'unix ipv6'
16781325Saxel.duch@nginx.com
16791325Saxel.duch@nginx.com    def test_routes_match_source(self):
16801325Saxel.duch@nginx.com        self.route_match({"source": "::"})
16811325Saxel.duch@nginx.com        self.route_match(
16821325Saxel.duch@nginx.com            {
16831325Saxel.duch@nginx.com                "source": [
16841325Saxel.duch@nginx.com                    "127.0.0.1",
16851325Saxel.duch@nginx.com                    "192.168.0.10:8080",
16861325Saxel.duch@nginx.com                    "192.168.0.11:8080-8090",
16871325Saxel.duch@nginx.com                ]
16881325Saxel.duch@nginx.com            }
16891325Saxel.duch@nginx.com        )
16901325Saxel.duch@nginx.com        self.route_match(
16911325Saxel.duch@nginx.com            {
16921325Saxel.duch@nginx.com                "source": [
16931325Saxel.duch@nginx.com                    "10.0.0.0/8",
16941325Saxel.duch@nginx.com                    "10.0.0.0/7:1000",
16951325Saxel.duch@nginx.com                    "10.0.0.0/32:8080-8090",
16961325Saxel.duch@nginx.com                ]
16971325Saxel.duch@nginx.com            }
16981325Saxel.duch@nginx.com        )
16991325Saxel.duch@nginx.com        self.route_match(
17001325Saxel.duch@nginx.com            {
17011325Saxel.duch@nginx.com                "source": [
17021325Saxel.duch@nginx.com                    "10.0.0.0-10.0.0.1",
17031325Saxel.duch@nginx.com                    "10.0.0.0-11.0.0.0:1000",
17041325Saxel.duch@nginx.com                    "127.0.0.0-127.0.0.255:8080-8090",
17051325Saxel.duch@nginx.com                ]
17061325Saxel.duch@nginx.com            }
17071325Saxel.duch@nginx.com        )
17081325Saxel.duch@nginx.com        self.route_match(
17091325Saxel.duch@nginx.com            {"source": ["2001::", "[2002::]:8000", "[2003::]:8080-8090"]}
17101325Saxel.duch@nginx.com        )
17111325Saxel.duch@nginx.com        self.route_match(
17121325Saxel.duch@nginx.com            {
17131325Saxel.duch@nginx.com                "source": [
17141325Saxel.duch@nginx.com                    "2001::-200f:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
17151325Saxel.duch@nginx.com                    "[fe08::-feff::]:8000",
17161325Saxel.duch@nginx.com                    "[fff0::-fff0::10]:8080-8090",
17171325Saxel.duch@nginx.com                ]
17181325Saxel.duch@nginx.com            }
17191325Saxel.duch@nginx.com        )
17201325Saxel.duch@nginx.com        self.route_match(
17211325Saxel.duch@nginx.com            {
17221325Saxel.duch@nginx.com                "source": [
17231325Saxel.duch@nginx.com                    "2001::/16",
17241325Saxel.duch@nginx.com                    "[0ff::/64]:8000",
17251325Saxel.duch@nginx.com                    "[fff0:abcd:ffff:ffff:ffff::/128]:8080-8090",
17261325Saxel.duch@nginx.com                ]
17271325Saxel.duch@nginx.com            }
17281325Saxel.duch@nginx.com        )
17291325Saxel.duch@nginx.com        self.route_match({"source": "*:0-65535"})
17301596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'source any'
17311325Saxel.duch@nginx.com
17321325Saxel.duch@nginx.com    def test_routes_match_source_invalid(self):
17331325Saxel.duch@nginx.com        self.route_match_invalid({"source": "127"})
17341325Saxel.duch@nginx.com        self.route_match_invalid({"source": "256.0.0.1"})
17351325Saxel.duch@nginx.com        self.route_match_invalid({"source": "127.0.0."})
17361391Szelenkov@nginx.com        self.route_match_invalid({"source": " 127.0.0.1"})
17371325Saxel.duch@nginx.com        self.route_match_invalid({"source": "127.0.0.1:"})
17381325Saxel.duch@nginx.com        self.route_match_invalid({"source": "127.0.0.1/"})
17391325Saxel.duch@nginx.com        self.route_match_invalid({"source": "11.0.0.0/33"})
17401325Saxel.duch@nginx.com        self.route_match_invalid({"source": "11.0.0.0/65536"})
17411325Saxel.duch@nginx.com        self.route_match_invalid({"source": "11.0.0.0-10.0.0.0"})
17421325Saxel.duch@nginx.com        self.route_match_invalid({"source": "11.0.0.0:3000-2000"})
17431325Saxel.duch@nginx.com        self.route_match_invalid({"source": ["11.0.0.0:3000-2000"]})
17441325Saxel.duch@nginx.com        self.route_match_invalid({"source": "[2001::]:3000-2000"})
17451325Saxel.duch@nginx.com        self.route_match_invalid({"source": "2001::-2000::"})
17461325Saxel.duch@nginx.com        self.route_match_invalid({"source": "2001::/129"})
17471325Saxel.duch@nginx.com        self.route_match_invalid({"source": "::FFFFF"})
17481325Saxel.duch@nginx.com        self.route_match_invalid({"source": "[::1]:"})
17491370Szelenkov@nginx.com        self.route_match_invalid({"source": "[:::]:7080"})
17501325Saxel.duch@nginx.com        self.route_match_invalid({"source": "*:"})
17511325Saxel.duch@nginx.com        self.route_match_invalid({"source": "*:1-a"})
17521325Saxel.duch@nginx.com        self.route_match_invalid({"source": "*:65536"})
17531325Saxel.duch@nginx.com
17541327Saxel.duch@nginx.com    def test_routes_match_destination(self):
17551596Szelenkov@nginx.com        assert 'success' in self.conf(
17561596Szelenkov@nginx.com            {"*:7080": {"pass": "routes"}, "*:7081": {"pass": "routes"}},
17571596Szelenkov@nginx.com            'listeners',
17581596Szelenkov@nginx.com        ), 'listeners configure'
17591327Saxel.duch@nginx.com
17601327Saxel.duch@nginx.com        self.route_match({"destination": "*:7080"})
17611596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'dest'
17621596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'dest 2'
17631327Saxel.duch@nginx.com
17641327Saxel.duch@nginx.com        self.route_match({"destination": ["127.0.0.1:7080"]})
17651596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'dest 3'
17661596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'dest 4'
17671327Saxel.duch@nginx.com
17681327Saxel.duch@nginx.com        self.route_match({"destination": "!*:7080"})
17691596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg'
17701596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 200, 'dest neg 2'
17711327Saxel.duch@nginx.com
17721391Szelenkov@nginx.com        self.route_match({"destination": ['!*:7080', '!*:7081']})
17731596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 3'
17741596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'dest neg 4'
17751391Szelenkov@nginx.com
17761391Szelenkov@nginx.com        self.route_match({"destination": ['!*:7081', '!*:7082']})
17771596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'dest neg 5'
17781391Szelenkov@nginx.com
17791391Szelenkov@nginx.com        self.route_match({"destination": ['*:7080', '!*:7080']})
17801596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 6'
17811391Szelenkov@nginx.com
17821391Szelenkov@nginx.com        self.route_match(
17831391Szelenkov@nginx.com            {"destination": ['127.0.0.1:7080', '*:7081', '!*:7080']}
17841391Szelenkov@nginx.com        )
17851596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 7'
17861596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 200, 'dest neg 8'
17871391Szelenkov@nginx.com
17881391Szelenkov@nginx.com        self.route_match({"destination": ['!*:7081', '!*:7082', '*:7083']})
17891596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 9'
17901391Szelenkov@nginx.com
17911391Szelenkov@nginx.com        self.route_match(
17921391Szelenkov@nginx.com            {"destination": ['*:7081', '!127.0.0.1:7080', '*:7080']}
17931391Szelenkov@nginx.com        )
17941596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 10'
17951596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 200, 'dest neg 11'
17961391Szelenkov@nginx.com
17971596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
17981596Szelenkov@nginx.com            'routes/0/match/destination/0'
17991596Szelenkov@nginx.com        ), 'remove destination rule'
18001596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 12'
18011596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'dest neg 13'
18021391Szelenkov@nginx.com
18031596Szelenkov@nginx.com        assert 'success' in self.conf_delete(
18041596Szelenkov@nginx.com            'routes/0/match/destination/0'
18051596Szelenkov@nginx.com        ), 'remove destination rule 2'
18061596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'dest neg 14'
18071596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'dest neg 15'
18081391Szelenkov@nginx.com
18091596Szelenkov@nginx.com        assert 'success' in self.conf_post(
18101596Szelenkov@nginx.com            "\"!127.0.0.1\"", 'routes/0/match/destination'
18111596Szelenkov@nginx.com        ), 'add destination rule'
18121596Szelenkov@nginx.com        assert self.get()['status'] == 404, 'dest neg 16'
18131596Szelenkov@nginx.com        assert self.get(port=7081)['status'] == 404, 'dest neg 17'
18141391Szelenkov@nginx.com
18151327Saxel.duch@nginx.com    def test_routes_match_destination_proxy(self):
18161596Szelenkov@nginx.com        assert 'success' in self.conf(
18171596Szelenkov@nginx.com            {
18181596Szelenkov@nginx.com                "listeners": {
18191596Szelenkov@nginx.com                    "*:7080": {"pass": "routes/first"},
18201596Szelenkov@nginx.com                    "*:7081": {"pass": "routes/second"},
18211596Szelenkov@nginx.com                },
18221596Szelenkov@nginx.com                "routes": {
18231596Szelenkov@nginx.com                    "first": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
18241596Szelenkov@nginx.com                    "second": [
18251596Szelenkov@nginx.com                        {
18261596Szelenkov@nginx.com                            "match": {"destination": ["127.0.0.1:7081"]},
18271596Szelenkov@nginx.com                            "action": {"return": 200},
18281596Szelenkov@nginx.com                        }
18291596Szelenkov@nginx.com                    ],
18301596Szelenkov@nginx.com                },
18311596Szelenkov@nginx.com                "applications": {},
18321596Szelenkov@nginx.com            }
18331596Szelenkov@nginx.com        ), 'proxy configure'
18341327Saxel.duch@nginx.com
18351596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'proxy'
1836