annotate tests/test_routing.py @ 661:2f780b191541

internal: Fix a bug with registering taxonomy terms that are not strings. Some objects, like the blog data provider's taxnonomy entries, can render as strings, but are objects themselves. When registering them as "used terms", we need to use their string representation.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 01 Mar 2016 22:26:09 -0800
parents 6b6c5442c790
children f6f9a284a5f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
1 import urllib.parse
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import pytest
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from piecrust.routing import Route
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
4 from .mockutil import get_mock_app
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 @pytest.mark.parametrize(
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 'config, metadata, expected',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 [
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 {'foo': 'bar'}, True),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 {'zoo': 'zar', 'foo': 'bar'}, True),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 ({'url': '/%foo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 {'zoo': 'zar'}, False),
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 ({'url': '/%foo%/%zoo%'},
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 {'zoo': 'zar'}, False)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 ])
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 def test_matches_metadata(config, metadata, expected):
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
20 app = get_mock_app()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
21 app.config.set('site/root', '/')
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 config.setdefault('source', 'blah')
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 route = Route(app, config)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 m = route.matchesMetadata(metadata)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 assert m == expected
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 @pytest.mark.parametrize(
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
29 'site_root, route_pattern, expected_required_metadata',
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 [
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
31 ('/', '/%foo%', set(['foo'])),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
32 ('/', '/%path:foo%', set(['foo'])),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
33 ('/', '/%foo%/%bar%', set(['foo', 'bar'])),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
34 ('/', '/%foo%/%path:bar%', set(['foo', 'bar'])),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
35 ('/something', '/%foo%', set(['foo'])),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
36 ('/something', '/%path:foo%', set(['foo'])),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
37 ('/something', '/%foo%/%bar%', set(['foo', 'bar'])),
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
38 ('/something', '/%foo%/%path:bar%', set(['foo', 'bar'])),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
39 ('/~johndoe', '/%foo%', set(['foo'])),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
40 ('/~johndoe', '/%path:foo%', set(['foo'])),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
41 ('/~johndoe', '/%foo%/%bar%', set(['foo', 'bar'])),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
42 ('/~johndoe', '/%foo%/%path:bar%', set(['foo', 'bar']))
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
43 ])
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
44 def test_required_metadata(site_root, route_pattern,
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
45 expected_required_metadata):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
46 app = get_mock_app()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
47 app.config.set('site/root', site_root.rstrip('/') + '/')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
48 config = {'url': route_pattern, 'source': 'blah'}
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
49 route = Route(app, config)
369
4b1019bb2533 serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
50 assert route.required_route_metadata == expected_required_metadata
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
51
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
52
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
53 @pytest.mark.parametrize(
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
54 'site_root, config, uri, expected_match',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
55 [
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
56 ('/', {'url': '/%foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 'something',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 {'foo': 'something'}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
59 ('/', {'url': '/%foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 'something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 None),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
62 ('/', {'url': '/%path:foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 'something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 {'foo': 'something/other'}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
65 ('/', {'url': '/%path:foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 '',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 {'foo': ''}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
68 ('/', {'url': '/prefix/%path:foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 'prefix/something/other',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 {'foo': 'something/other'}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
71 ('/', {'url': '/prefix/%path:foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 'prefix/',
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 {'foo': ''}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
74 ('/', {'url': '/prefix/%path:foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
75 'prefix',
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
76 {'foo': ''}),
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
77
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
78 ('/blah', {'url': '/%foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
79 'something',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
80 {'foo': 'something'}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
81 ('/blah', {'url': '/%foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
82 'something/other',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
83 None),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
84 ('/blah', {'url': '/%path:foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
85 'something/other',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
86 {'foo': 'something/other'}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
87 ('/blah', {'url': '/%path:foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
88 '',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
89 {'foo': ''}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
90 ('/blah', {'url': '/prefix/%path:foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
91 'prefix/something/other',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
92 {'foo': 'something/other'}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
93 ('/blah', {'url': '/prefix/%path:foo%'},
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
94 'prefix/',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
95 {'foo': ''}),
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
96 ('/blah', {'url': '/prefix/%path:foo%'},
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 'prefix',
380
f33712c4cfab routing: Fix bugs with matching URLs with correct route but missing metadata.
Ludovic Chabant <ludovic@chabant.com>
parents: 369
diff changeset
98 {'foo': ''}),
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
99
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
100 ('/~johndoe', {'url': '/%foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
101 'something',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
102 {'foo': 'something'}),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
103 ('/~johndoe', {'url': '/%foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
104 'something/other',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
105 None),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
106 ('/~johndoe', {'url': '/%path:foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
107 'something/other',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
108 {'foo': 'something/other'}),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
109 ('/~johndoe', {'url': '/%path:foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
110 '',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
111 {'foo': ''}),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
112 ('/~johndoe', {'url': '/prefix/%path:foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
113 'prefix/something/other',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
114 {'foo': 'something/other'}),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
115 ('/~johndoe', {'url': '/prefix/%path:foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
116 'prefix/',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
117 {'foo': ''}),
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
118 ('/~johndoe', {'url': '/prefix/%path:foo%'},
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
119 'prefix',
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
120 {'foo': ''}),
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 ])
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
122 def test_match_uri(site_root, config, uri, expected_match):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
123 site_root = site_root.rstrip('/') + '/'
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
124 app = get_mock_app()
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
125 app.config.set('site/root', urllib.parse.quote(site_root))
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 config.setdefault('source', 'blah')
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 route = Route(app, config)
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 assert route.uri_pattern == config['url'].lstrip('/')
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
129 m = route.matchUri(urllib.parse.quote(site_root) + uri)
177
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 assert m == expected_match
4b0c87e7df73 tests: Add unit tests for routing classes.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
132
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
133 @pytest.mark.parametrize(
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
134 'site_root',
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
135 [
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
136 ('/'), ('/whatever'), ('/~johndoe')
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
137 ])
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
138 def test_match_uri_requires_absolute_uri(site_root):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
139 with pytest.raises(Exception):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
140 app = get_mock_app()
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
141 app.config.set('site/root', site_root.rstrip('/') + '/')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
142 config = {'url': '/%path:slug%', 'source': 'blah'}
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
143 route = Route(app, config)
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
144 route.matchUri('notabsuri')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
145
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
146
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 262
diff changeset
147 @pytest.mark.parametrize(
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
148 'slug, page_num, pretty, expected',
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
149 [
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
150 # Pretty URLs
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
151 ('', 1, True, ''),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
152 ('', 2, True, '2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
153 ('foo', 1, True, 'foo'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
154 ('foo', 2, True, 'foo/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
155 ('foo/bar', 1, True, 'foo/bar'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
156 ('foo/bar', 2, True, 'foo/bar/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
157 ('foo.ext', 1, True, 'foo.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
158 ('foo.ext', 2, True, 'foo.ext/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
159 ('foo/bar.ext', 1, True, 'foo/bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
160 ('foo/bar.ext', 2, True, 'foo/bar.ext/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
161 ('foo.bar.ext', 1, True, 'foo.bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
162 ('foo.bar.ext', 2, True, 'foo.bar.ext/2'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
163 # Ugly URLs
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
164 ('', 1, False, ''),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
165 ('', 2, False, '2.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
166 ('foo', 1, False, 'foo.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
167 ('foo', 2, False, 'foo/2.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
168 ('foo/bar', 1, False, 'foo/bar.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
169 ('foo/bar', 2, False, 'foo/bar/2.html'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
170 ('foo.ext', 1, False, 'foo.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
171 ('foo.ext', 2, False, 'foo/2.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
172 ('foo/bar.ext', 1, False, 'foo/bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
173 ('foo/bar.ext', 2, False, 'foo/bar/2.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
174 ('foo.bar.ext', 1, False, 'foo.bar.ext'),
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
175 ('foo.bar.ext', 2, False, 'foo.bar/2.ext')
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
176 ])
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
177 def test_get_uri(slug, page_num, pretty, expected):
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
178 for root in ['/', '/blah/', '/~johndoe/']:
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
179 app = get_mock_app()
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
180 app.config.set('site/root', urllib.parse.quote(root))
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
181 app.config.set('site/pretty_urls', pretty)
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
182 app.config.set('site/trailing_slash', False)
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
183 app.config.set('__cache/pagination_suffix_format', '/%(num)d')
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
184
568
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
185 config = {'url': '/%path:slug%', 'source': 'blah'}
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
186 route = Route(app, config)
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
187 uri = route.getUri({'slug': slug}, sub_num=page_num)
6b6c5442c790 bug: Correctly handle root URLs with special characters.
Ludovic Chabant <ludovic@chabant.com>
parents: 380
diff changeset
188 assert uri == (urllib.parse.quote(root) + expected)
262
61145dcd56e0 routing: Better generate URLs according to the site configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 177
diff changeset
189