Mercurial > piecrust2
comparison tests/test_routing.py @ 568:6b6c5442c790
bug: Correctly handle root URLs with special characters.
The `site/root` setting is now pre-escaped to get a correct URL, and routing
excludes it from escaping.
Add unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 13 Oct 2015 22:50:38 -0700 |
parents | f33712c4cfab |
children | f6f9a284a5f3 |
comparison
equal
deleted
inserted
replaced
567:a65f04ddbea2 | 568:6b6c5442c790 |
---|---|
1 import urllib.parse | |
1 import pytest | 2 import pytest |
2 from piecrust.routing import Route | 3 from piecrust.routing import Route |
3 from .mockutil import get_mock_app | 4 from .mockutil import get_mock_app |
4 | 5 |
5 | 6 |
32 ('/', '/%foo%/%bar%', set(['foo', 'bar'])), | 33 ('/', '/%foo%/%bar%', set(['foo', 'bar'])), |
33 ('/', '/%foo%/%path:bar%', set(['foo', 'bar'])), | 34 ('/', '/%foo%/%path:bar%', set(['foo', 'bar'])), |
34 ('/something', '/%foo%', set(['foo'])), | 35 ('/something', '/%foo%', set(['foo'])), |
35 ('/something', '/%path:foo%', set(['foo'])), | 36 ('/something', '/%path:foo%', set(['foo'])), |
36 ('/something', '/%foo%/%bar%', set(['foo', 'bar'])), | 37 ('/something', '/%foo%/%bar%', set(['foo', 'bar'])), |
37 ('/something', '/%foo%/%path:bar%', set(['foo', 'bar'])) | 38 ('/something', '/%foo%/%path:bar%', set(['foo', 'bar'])), |
39 ('/~johndoe', '/%foo%', set(['foo'])), | |
40 ('/~johndoe', '/%path:foo%', set(['foo'])), | |
41 ('/~johndoe', '/%foo%/%bar%', set(['foo', 'bar'])), | |
42 ('/~johndoe', '/%foo%/%path:bar%', set(['foo', 'bar'])) | |
38 ]) | 43 ]) |
39 def test_required_metadata(site_root, route_pattern, | 44 def test_required_metadata(site_root, route_pattern, |
40 expected_required_metadata): | 45 expected_required_metadata): |
41 app = get_mock_app() | 46 app = get_mock_app() |
42 app.config.set('site/root', site_root.rstrip('/') + '/') | 47 app.config.set('site/root', site_root.rstrip('/') + '/') |
89 'prefix/', | 94 'prefix/', |
90 {'foo': ''}), | 95 {'foo': ''}), |
91 ('/blah', {'url': '/prefix/%path:foo%'}, | 96 ('/blah', {'url': '/prefix/%path:foo%'}, |
92 'prefix', | 97 'prefix', |
93 {'foo': ''}), | 98 {'foo': ''}), |
99 | |
100 ('/~johndoe', {'url': '/%foo%'}, | |
101 'something', | |
102 {'foo': 'something'}), | |
103 ('/~johndoe', {'url': '/%foo%'}, | |
104 'something/other', | |
105 None), | |
106 ('/~johndoe', {'url': '/%path:foo%'}, | |
107 'something/other', | |
108 {'foo': 'something/other'}), | |
109 ('/~johndoe', {'url': '/%path:foo%'}, | |
110 '', | |
111 {'foo': ''}), | |
112 ('/~johndoe', {'url': '/prefix/%path:foo%'}, | |
113 'prefix/something/other', | |
114 {'foo': 'something/other'}), | |
115 ('/~johndoe', {'url': '/prefix/%path:foo%'}, | |
116 'prefix/', | |
117 {'foo': ''}), | |
118 ('/~johndoe', {'url': '/prefix/%path:foo%'}, | |
119 'prefix', | |
120 {'foo': ''}), | |
94 ]) | 121 ]) |
95 def test_match_uri(site_root, config, uri, expected_match): | 122 def test_match_uri(site_root, config, uri, expected_match): |
96 site_root = site_root.rstrip('/') + '/' | 123 site_root = site_root.rstrip('/') + '/' |
97 app = get_mock_app() | 124 app = get_mock_app() |
98 app.config.set('site/root', site_root) | 125 app.config.set('site/root', urllib.parse.quote(site_root)) |
99 config.setdefault('source', 'blah') | 126 config.setdefault('source', 'blah') |
100 route = Route(app, config) | 127 route = Route(app, config) |
101 assert route.uri_pattern == config['url'].lstrip('/') | 128 assert route.uri_pattern == config['url'].lstrip('/') |
102 m = route.matchUri(site_root + uri) | 129 m = route.matchUri(urllib.parse.quote(site_root) + uri) |
103 assert m == expected_match | 130 assert m == expected_match |
104 | 131 |
105 | 132 |
106 @pytest.mark.parametrize( | 133 @pytest.mark.parametrize( |
107 'site_root', | 134 'site_root', |
108 [ | 135 [ |
109 ('/'), ('/whatever') | 136 ('/'), ('/whatever'), ('/~johndoe') |
110 ]) | 137 ]) |
111 def test_match_uri_requires_absolute_uri(site_root): | 138 def test_match_uri_requires_absolute_uri(site_root): |
112 with pytest.raises(Exception): | 139 with pytest.raises(Exception): |
113 app = get_mock_app() | 140 app = get_mock_app() |
114 app.config.set('site/root', site_root.rstrip('/') + '/') | 141 app.config.set('site/root', site_root.rstrip('/') + '/') |
146 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'), | 173 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'), |
147 ('foo.bar.ext', 1, False, 'foo.bar.ext'), | 174 ('foo.bar.ext', 1, False, 'foo.bar.ext'), |
148 ('foo.bar.ext', 2, False, 'foo.bar/2.ext') | 175 ('foo.bar.ext', 2, False, 'foo.bar/2.ext') |
149 ]) | 176 ]) |
150 def test_get_uri(slug, page_num, pretty, expected): | 177 def test_get_uri(slug, page_num, pretty, expected): |
151 app = get_mock_app() | 178 for root in ['/', '/blah/', '/~johndoe/']: |
152 app.config.set('site/root', '/blah/') | 179 app = get_mock_app() |
153 app.config.set('site/pretty_urls', pretty) | 180 app.config.set('site/root', urllib.parse.quote(root)) |
154 app.config.set('site/trailing_slash', False) | 181 app.config.set('site/pretty_urls', pretty) |
155 app.config.set('__cache/pagination_suffix_format', '/%(num)d') | 182 app.config.set('site/trailing_slash', False) |
183 app.config.set('__cache/pagination_suffix_format', '/%(num)d') | |
156 | 184 |
157 config = {'url': '/%path:slug%', 'source': 'blah'} | 185 config = {'url': '/%path:slug%', 'source': 'blah'} |
158 route = Route(app, config) | 186 route = Route(app, config) |
159 uri = route.getUri({'slug': slug}, sub_num=page_num) | 187 uri = route.getUri({'slug': slug}, sub_num=page_num) |
160 assert uri == ('/blah/' + expected) | 188 assert uri == (urllib.parse.quote(root) + expected) |
161 | 189 |