comparison tests/test_routing.py @ 329:422052d2e978

internal: Try handling URLs in a consistent way. * Now URLs passed to, and returned from, routes will always be absolute URLs, i.e. URLs including the site root. * Validate the site root at config loading time to make sure it starts and ends with a slash. * Get rid of unused stuff. * Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 31 Mar 2015 23:03:28 -0700
parents 61145dcd56e0
children 4b1019bb2533
comparison
equal deleted inserted replaced
328:2a5996e0d3ec 329:422052d2e978
1 import mock
2 import pytest 1 import pytest
3 from piecrust.routing import Route 2 from piecrust.routing import Route
4 from .mockutil import get_mock_app 3 from .mockutil import get_mock_app
5 4
6 5
15 {'zoo': 'zar'}, False), 14 {'zoo': 'zar'}, False),
16 ({'url': '/%foo%/%zoo%'}, 15 ({'url': '/%foo%/%zoo%'},
17 {'zoo': 'zar'}, False) 16 {'zoo': 'zar'}, False)
18 ]) 17 ])
19 def test_matches_metadata(config, metadata, expected): 18 def test_matches_metadata(config, metadata, expected):
20 app = mock.Mock() 19 app = get_mock_app()
21 app.config = {'site/root': '/'} 20 app.config.set('site/root', '/')
22 config.setdefault('source', 'blah') 21 config.setdefault('source', 'blah')
23 route = Route(app, config) 22 route = Route(app, config)
24 m = route.matchesMetadata(metadata) 23 m = route.matchesMetadata(metadata)
25 assert m == expected 24 assert m == expected
26 25
27 26
28 @pytest.mark.parametrize( 27 @pytest.mark.parametrize(
29 'config, uri, expected_match', 28 'site_root, route_pattern, expected_required_metadata',
30 [ 29 [
31 ({'url': '/%foo%'}, 30 ('/', '/%foo%', set(['foo'])),
31 ('/', '/%path:foo%', set(['foo'])),
32 ('/', '/%foo%/%bar%', set(['foo', 'bar'])),
33 ('/', '/%foo%/%path:bar%', set(['foo', 'bar'])),
34 ('/something', '/%foo%', set(['foo'])),
35 ('/something', '/%path:foo%', set(['foo'])),
36 ('/something', '/%foo%/%bar%', set(['foo', 'bar'])),
37 ('/something', '/%foo%/%path:bar%', set(['foo', 'bar']))
38 ])
39 def test_required_metadata(site_root, route_pattern,
40 expected_required_metadata):
41 app = get_mock_app()
42 app.config.set('site/root', site_root.rstrip('/') + '/')
43 config = {'url': route_pattern, 'source': 'blah'}
44 route = Route(app, config)
45 assert route.required_source_metadata == expected_required_metadata
46
47
48 @pytest.mark.parametrize(
49 'site_root, config, uri, expected_match',
50 [
51 ('/', {'url': '/%foo%'},
32 'something', 52 'something',
33 {'foo': 'something'}), 53 {'foo': 'something'}),
34 ({'url': '/%foo%'}, 54 ('/', {'url': '/%foo%'},
35 'something/other', 55 'something/other',
36 None), 56 None),
37 ({'url': '/%path:foo%'}, 57 ('/', {'url': '/%path:foo%'},
38 'something/other', 58 'something/other',
39 {'foo': 'something/other'}), 59 {'foo': 'something/other'}),
40 ({'url': '/%path:foo%'}, 60 ('/', {'url': '/%path:foo%'},
41 '', 61 '',
42 {'foo': ''}), 62 {'foo': ''}),
43 ({'url': '/prefix/%path:foo%'}, 63 ('/', {'url': '/prefix/%path:foo%'},
44 'prefix/something/other', 64 'prefix/something/other',
45 {'foo': 'something/other'}), 65 {'foo': 'something/other'}),
46 ({'url': '/prefix/%path:foo%'}, 66 ('/', {'url': '/prefix/%path:foo%'},
47 'prefix/', 67 'prefix/',
48 {'foo': ''}), 68 {'foo': ''}),
49 ({'url': '/prefix/%path:foo%'}, 69 ('/', {'url': '/prefix/%path:foo%'},
70 'prefix',
71 {}),
72
73 ('/blah', {'url': '/%foo%'},
74 'something',
75 {'foo': 'something'}),
76 ('/blah', {'url': '/%foo%'},
77 'something/other',
78 None),
79 ('/blah', {'url': '/%path:foo%'},
80 'something/other',
81 {'foo': 'something/other'}),
82 ('/blah', {'url': '/%path:foo%'},
83 '',
84 {'foo': ''}),
85 ('/blah', {'url': '/prefix/%path:foo%'},
86 'prefix/something/other',
87 {'foo': 'something/other'}),
88 ('/blah', {'url': '/prefix/%path:foo%'},
89 'prefix/',
90 {'foo': ''}),
91 ('/blah', {'url': '/prefix/%path:foo%'},
50 'prefix', 92 'prefix',
51 {}), 93 {}),
52 ]) 94 ])
53 def test_match_uri(config, uri, expected_match): 95 def test_match_uri(site_root, config, uri, expected_match):
54 app = mock.Mock() 96 site_root = site_root.rstrip('/') + '/'
55 app.config = {'site/root': '/'} 97 app = get_mock_app()
98 app.config.set('site/root', site_root)
56 config.setdefault('source', 'blah') 99 config.setdefault('source', 'blah')
57 route = Route(app, config) 100 route = Route(app, config)
58 assert route.uri_pattern == config['url'].lstrip('/') 101 assert route.uri_pattern == config['url'].lstrip('/')
59 m = route.matchUri(uri) 102 m = route.matchUri(site_root + uri)
60 assert m == expected_match 103 assert m == expected_match
104
105
106 @pytest.mark.parametrize(
107 'site_root',
108 [
109 ('/'), ('/whatever')
110 ])
111 def test_match_uri_requires_absolute_uri(site_root):
112 with pytest.raises(Exception):
113 app = get_mock_app()
114 app.config.set('site/root', site_root.rstrip('/') + '/')
115 config = {'url': '/%path:slug%', 'source': 'blah'}
116 route = Route(app, config)
117 route.matchUri('notabsuri')
61 118
62 119
63 @pytest.mark.parametrize( 120 @pytest.mark.parametrize(
64 'slug, page_num, pretty, expected', 121 'slug, page_num, pretty, expected',
65 [ 122 [
90 ('foo.bar.ext', 1, False, 'foo.bar.ext'), 147 ('foo.bar.ext', 1, False, 'foo.bar.ext'),
91 ('foo.bar.ext', 2, False, 'foo.bar/2.ext') 148 ('foo.bar.ext', 2, False, 'foo.bar/2.ext')
92 ]) 149 ])
93 def test_get_uri(slug, page_num, pretty, expected): 150 def test_get_uri(slug, page_num, pretty, expected):
94 app = get_mock_app() 151 app = get_mock_app()
95 app.config.set('site/root', '/blah') 152 app.config.set('site/root', '/blah/')
96 app.config.set('site/pretty_urls', pretty) 153 app.config.set('site/pretty_urls', pretty)
97 app.config.set('site/trailing_slash', False) 154 app.config.set('site/trailing_slash', False)
98 app.config.set('__cache/pagination_suffix_format', '/%(num)d') 155 app.config.set('__cache/pagination_suffix_format', '/%(num)d')
99 156
100 config = {'url': '/%path:slug%', 'source': 'blah'} 157 config = {'url': '/%path:slug%', 'source': 'blah'}