1b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru# -*- coding: utf-8 -*-
2b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruimport random
31b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert
4b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruimport webapp2
5b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querufrom webapp2 import BaseRoute, RedirectHandler, Request, Route, Router
6b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
7b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruimport test_base
8b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
9b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
10b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass TestRoute(test_base.BaseTestCase):
11b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_no_variable(self):
12b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/hello', None)
13b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route, args, kwargs = route.match(Request.blank('/hello'))
14b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(kwargs, {})
15b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        url = route.build(Request.blank('/'), (), {})
16b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(url, '/hello')
17b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
18b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/hello/world/', None)
19b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route, args, kwargs = route.match(Request.blank('/hello/world/'))
20b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(kwargs, {})
21b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        url = route.build(Request.blank('/'), (), {})
22b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(url, '/hello/world/')
23b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
24b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_repetition_operator(self):
25b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<:\d>', None)
26b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/1')), (route, ('1',), {}))
27b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/2')), (route, ('2',), {}))
28b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        route = Route(r'/<:\d{2,3}>', None)
30b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/11')), (route, ('11',), {}))
31f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        self.assertEqual(route.match(Request.blank('/111')), (route, ('111',), {}))
32b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/1111')), None)
33b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
34f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    def test_unnamed_variable(self):
35b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<:\d{4}>', None)
36b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/2010')), (route, ('2010',), {}))
37f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        self.assertEqual(route.match(Request.blank('/aaaa')), None)
38b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
39c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(r'/<:\d{2}>.<:\d{2}>', None)
40c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/98.99')), (route, ('98', '99'), {}))
41c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/aa.aa')), None)
42c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
43c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(r'/<:\d{2}>.<:\d{2}>/<foo>', None)
44c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/98.99/test')), (route, ('98', '99'), {'foo': 'test'}))
45c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/aa.aa/test')), None)
46c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
47c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_simple_variable(self):
48c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(r'/<foo>', None)
49c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/bar')), (route, (), {'foo': 'bar'}))
50c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url = route.build(Request.blank('/'), (), dict(foo='baz'))
51c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(url, '/baz')
52c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
53c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_expr_variable(self):
54c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(r'/<year:\d{4}>', None)
55c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/bar')), None)
56c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/2010')), (route, (), {'year': '2010'}))
57c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank('/1900')), (route, (), {'year': '1900'}))
58c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url = route.build(Request.blank('/'), (), dict(year='2010'))
59c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(url, '/2010')
60c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
61c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_expr_variable2(self):
62c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(r'/<year:\d{4}>/foo/', None)
63c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url = route.build(Request.blank('/'), (), dict(year='2010'))
64c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(url, '/2010/foo/')
65c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
66c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_build_missing_argument(self):
67c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(r'/<:\d{4}>', None)
68c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertRaises(KeyError, route.build, Request.blank('/'), (), {})
6950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
7050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        route = Route(r'/<:\d{4}>/<:\d{2}>', None)
7150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertRaises(KeyError, route.build, Request.blank('/'), (2010,), {})
72b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
73b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_invalid_argument(self):
74b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<:\d{4}>', None)
75b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(ValueError, route.build, Request.blank('/'), ('20100',), {})
76b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
77b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_invalid_argument2(self):
78b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<:\d{4}>', None)
79b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(ValueError, route.build, Request.blank('/'), ('201a',), {})
80b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
81b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_missing_keyword(self):
82b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<year:\d{4}>', None)
83b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(KeyError, route.build, Request.blank('/'), (), {})
84b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
85b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_missing_keyword2(self):
86b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<year:\d{4}>/<month:\d{2}>', None)
87b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(KeyError, route.build, Request.blank('/'), (), dict(year='2010'))
88b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
89b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_invalid_keyword(self):
90b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/<year:\d{4}>', None)
91b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(ValueError, route.build, Request.blank('/'), (), dict(year='20100'))
92b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
93b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_invalid_keyword2(self):
9483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route(r'/<year:\d{4}>', None)
9583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertRaises(ValueError, route.build, Request.blank('/'), (), dict(year='201a'))
9683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
9783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_build_with_unnamed_variable(self):
9883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route(r'/<:\d{4}>/<month:\d{2}>', None)
9983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
10083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = route.build(Request.blank('/'), (2010,), dict(month=10))
10183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertEqual(url, '/2010/10')
10283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
10383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = route.build(Request.blank('/'), ('1999',), dict(month='07'))
10483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertEqual(url, '/1999/07')
10583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
10683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_build_default_keyword(self):
10783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route(r'/<year:\d{4}>/<month:\d{2}>', None,
10883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            defaults={'month': 10})
10983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = route.build(Request.blank('/'), (), dict(year='2010'))
11083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertEqual(url, '/2010/10')
11183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
11283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route(r'/<year:\d{4}>/<month:\d{2}>', None,
11383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius            defaults={'year': 1900})
11483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = route.build(Request.blank('/'), (), dict(month='07'))
11550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertEqual(url, '/1900/07')
11650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
11783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_build_extra_keyword(self):
11850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        route = Route(r'/<year:\d{4}>', None)
11950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        url = route.build(Request.blank('/'), (), dict(year='2010', foo='bar'))
12050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertEqual(url, '/2010?foo=bar')
12150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        # Arguments are sorted.
12250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        url = route.build(Request.blank('/'), (), dict(year='2010', foo='bar', baz='ding'))
12350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertEqual(url, '/2010?baz=ding&foo=bar')
12450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
12550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    def test_build_extra_positional_keyword(self):
12650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        route = Route(r'/<year:\d{4}>/<:\d{2}>', None)
12750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
12850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        url = route.build(Request.blank('/'), ('08', 'i-should-be-ignored', 'me-too'), dict(year='2010', foo='bar'))
12950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertEqual(url, '/2010/08?foo=bar')
13050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
13150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        url = route.build(Request.blank('/'), ('08', 'i-should-be-ignored', 'me-too'), dict(year='2010', foo='bar', baz='ding'))
13250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertEqual(url, '/2010/08?baz=ding&foo=bar')
13383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
13483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_build_int_keyword(self):
13583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route(r'/<year:\d{4}>', None)
13683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = route.build(Request.blank('/'), (), dict(year=2010))
13750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        self.assertEqual(url, '/2010')
138b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
13983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_build_int_variable(self):
14083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route(r'/<:\d{4}>', None)
14183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = route.build(Request.blank('/'), (2010,), {})
14283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertEqual(url, '/2010')
14383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
14483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_router_build_error(self):
14583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        router = Router(None)
14683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        router.add(Route('/<year:\d{4}>', None, name='year-page'))
14783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
14883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        url = router.build(Request.blank('/'), 'year-page', (), dict(year='2010'))
14983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertEqual(url, '/2010')
15083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
15183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        self.assertRaises(KeyError, router.build, Request.blank('/'), 'i-dont-exist', (), dict(year='2010'))
15283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
15383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius    def test_reverse_template(self):
15483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius        route = Route('/foo', None)
155b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        # Access route.regex just to set the lazy properties.
156b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        regex = route.regex
157b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.reverse_template, '/foo')
158b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
15950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        route = Route('/foo/<bar>', None)
1602e615e9896b12236afe0ff2695e8afc2ee73f961claireho        # Access route.regex just to set the lazy properties.
161b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho        regex = route.regex
162f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        self.assertEqual(route.reverse_template, '/foo/%(bar)s')
163b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
164b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route('/foo/<bar>/<baz:\d>', None)
165b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        # Access route.regex just to set the lazy properties.
166b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        regex = route.regex
167b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.reverse_template, '/foo/%(bar)s/%(baz)s')
168b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
169b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_invalid_template(self):
17050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        # To break it:
1712e615e9896b12236afe0ff2695e8afc2ee73f961claireho        # <>foo:><bar<:baz>
172b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho        route = Route('/<foo/<:bar', None)
173f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        # Access route.regex just to set the lazy properties.
174b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        regex = route.regex
175b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.reverse_template, '/<foo/<:bar')
176c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
177c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_build_full_without_request(self):
178c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        router = Router(None)
179c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        router.add(Route(r'/hello', None, name='hello'))
180c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertRaises(AttributeError, router.build, None, 'hello', (), dict(_full=True))
181c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertRaises(AttributeError, router.build, None, 'hello', (), dict(_scheme='https'))
182c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
183c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_positions(self):
184c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        template = '/<:\d+>' * 98
185c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        args = tuple(str(i) for i in range(98))
186c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url_res = '/' + '/'.join(args)
187c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
188c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        route = Route(template, None)
189c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank(url_res)), (route, args, {}))
190c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url = route.build(Request.blank('/'), args, {})
191c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(url_res, url)
192c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
193c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        args = [str(i) for i in range(1000)]
194c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        random.shuffle(args)
195c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        args = tuple(args[:98])
196c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url_res = '/' + '/'.join(args)
197b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(route.match(Request.blank(url_res)), (route, args, {}))
198c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        url = route.build(Request.blank('/'), args, {})
199c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(url_res, url)
200c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
201b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build_only_without_name(self):
202b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(ValueError, Route, r'/<foo>', None, build_only=True)
203b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
204b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_route_repr(self):
205b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(Route(r'/<foo>', None).__repr__(),
206b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            "<Route('/<foo>', None, name=None, defaults={}, build_only=False)>")
207b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(Route(r'/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True).__repr__(),
208b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            "<Route('/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True)>")
209b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
210f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        self.assertEqual(str(Route(r'/<foo>', None)),
211b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            "<Route('/<foo>', None, name=None, defaults={}, build_only=False)>")
212c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru        self.assertEqual(str(Route(r'/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True)),
21350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho            "<Route('/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True)>")
214c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru
215c69afcec261fc345fda8daf46f0ea6b4351dc777Jean-Baptiste Queru    def test_router_repr(self):
216b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router = Router(None)
217b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router.add(Route(r'/hello', None, name='hello', build_only=True))
218b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router.add(Route(r'/world', None))
219b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
220b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(router.__repr__(), "<Router([<Route('/world', None, name=None, defaults={}, build_only=False)>, <Route('/hello', None, name='hello', defaults={}, build_only=True)>])>")
22183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
222b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_base_route(self):
223b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = BaseRoute('foo', 'bar')
224b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(NotImplementedError, route.match, None)
225b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
226b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_set_matcher(self):
227b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req = Request.blank('/')
228b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        def custom_matcher(router, request):
229b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            self.assertEqual(request, req)
230b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
231b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router = Router(None)
23250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        router.set_matcher(custom_matcher)
23350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        router.match(req)
23483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius
23550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    def test_set_builder(self):
23650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        req = Request.blank('/')
23750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        def custom_builder(router, request, name, args, kwargs):
238b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            self.assertEqual(request, req)
239b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            return 'http://www.google.com'
240b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
241b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router = Router(None)
242b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router.set_builder(custom_builder)
243b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        res = router.build(req, '', (), {})
244b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(res, 'http://www.google.com')
245b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
246b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_set_adapter(self):
247b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        def custom_adapter(router, handler):
248b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            class MyAdapter(webapp2.BaseHandlerAdapter):
249b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                def __call__(self, request, response):
250b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                    response.write('hello my adapter')
251b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
252b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            return MyAdapter(handler)
253b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
254b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        def myhandler(request, *args, **kwargs):
255b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            return webapp2.Response('hello')
256b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
257b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        app = webapp2.WSGIApplication([('/', myhandler)])
258b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        app.router.set_adapter(custom_adapter)
259b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
260b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        rsp = app.get_response('/')
261b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(rsp.status_int, 200)
262f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        self.assertEqual(rsp.body, 'hello my adapter')
263b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
264b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_methods(self):
265b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/', methods=['GET', 'POST'])
266b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router = Router([route])
267b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req = Request.blank('/')
268b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req.method = 'GET'
269b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertTrue(router.match(req) is not None)
270b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req.method = 'POST'
271b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertTrue(router.match(req) is not None)
272b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req.method = 'PUT'
273b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(webapp2.exc.HTTPMethodNotAllowed, router.match, req)
274b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
275b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_schemes(self):
276b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/', schemes=['http'])
277b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req = Request.blank('http://mydomain.com/')
278b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertTrue(route.match(req) is not None)
279b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req = Request.blank('https://mydomain.com/')
280b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertTrue(route.match(req) is None)
281b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
282b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = Route(r'/', schemes=['https'])
283b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req = Request.blank('https://mydomain.com/')
284b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertTrue(route.match(req) is not None)
285b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        req = Request.blank('http://mydomain.com/')
286b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertTrue(route.match(req) is None)
287b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
288b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
289b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruclass TestSimpleRoute(test_base.BaseTestCase):
290b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_no_variable(self):
291b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router = webapp2.Router([(r'/', 'my_handler')])
292b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
293b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        matched_route, args, kwargs = router.match(webapp2.Request.blank('/'))
294b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(args, ())
295b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(kwargs, {})
296b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
297b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_simple_variables(self):
298b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        router = webapp2.Router([(r'/(\d{4})/(\d{2})', 'my_handler')])
299b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
300b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        matched_route, args, kwargs = router.match(webapp2.Request.blank('/2007/10'))
301b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(args, ('2007', '10'))
302f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        self.assertEqual(kwargs, {})
303f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
304b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_build(self):
305b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        route = webapp2.SimpleRoute('/', None)
306b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertRaises(NotImplementedError, route.build, None, None, None)
307b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
308b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    def test_route_repr(self):
309b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(webapp2.SimpleRoute(r'/<foo>', None).__repr__(), "<SimpleRoute('/<foo>', None)>")
310b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        self.assertEqual(str(webapp2.SimpleRoute(r'/<foo>', None)), "<SimpleRoute('/<foo>', None)>")
311b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
312b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
313b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruif __name__ == '__main__':
314b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    test_base.main()
315b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru