commit | author | age
|
8064bd
|
1 |
import os |
7ae0c2
|
2 |
import unittest |
c51896
|
3 |
import warnings |
7ae0c2
|
4 |
|
a00621
|
5 |
from pyramid import testing |
CM |
6 |
|
0c29cf
|
7 |
from pyramid.compat import text_, WIN |
MM |
8 |
|
925957
|
9 |
|
fb90f0
|
10 |
class TestURLMethodsMixin(unittest.TestCase): |
e62e47
|
11 |
def setUp(self): |
a00621
|
12 |
self.config = testing.setUp() |
e62e47
|
13 |
|
CM |
14 |
def tearDown(self): |
a00621
|
15 |
testing.tearDown() |
0c29cf
|
16 |
|
c51896
|
17 |
def _makeOne(self, environ=None): |
fb90f0
|
18 |
from pyramid.url import URLMethodsMixin |
0c29cf
|
19 |
|
c51896
|
20 |
if environ is None: |
CM |
21 |
environ = {} |
0c29cf
|
22 |
|
fb90f0
|
23 |
class Request(URLMethodsMixin): |
CM |
24 |
application_url = 'http://example.com:5432' |
c51896
|
25 |
script_name = '' |
0c29cf
|
26 |
|
c51896
|
27 |
def __init__(self, environ): |
CM |
28 |
self.environ = environ |
0c29cf
|
29 |
|
c51896
|
30 |
request = Request(environ) |
fb90f0
|
31 |
request.registry = self.config.registry |
CM |
32 |
return request |
7ae0c2
|
33 |
|
c51896
|
34 |
def _registerResourceURL(self, reg): |
CM |
35 |
from pyramid.interfaces import IResourceURL |
|
36 |
from zope.interface import Interface |
0c29cf
|
37 |
|
c51896
|
38 |
class DummyResourceURL(object): |
60aeef
|
39 |
physical_path = '/context/' |
CM |
40 |
virtual_path = '/context/' |
0c29cf
|
41 |
|
MM |
42 |
def __init__(self, context, request): |
|
43 |
pass |
|
44 |
|
|
45 |
reg.registerAdapter( |
|
46 |
DummyResourceURL, (Interface, Interface), IResourceURL |
|
47 |
) |
60aeef
|
48 |
return DummyResourceURL |
c51896
|
49 |
|
fb90f0
|
50 |
def test_resource_url_root_default(self): |
CM |
51 |
request = self._makeOne() |
c51896
|
52 |
self._registerResourceURL(request.registry) |
7ae0c2
|
53 |
root = DummyContext() |
fb90f0
|
54 |
result = request.resource_url(root) |
c51896
|
55 |
self.assertEqual(result, 'http://example.com:5432/context/') |
7ae0c2
|
56 |
|
fb90f0
|
57 |
def test_resource_url_extra_args(self): |
CM |
58 |
request = self._makeOne() |
c51896
|
59 |
self._registerResourceURL(request.registry) |
e62e47
|
60 |
context = DummyContext() |
fb90f0
|
61 |
result = request.resource_url(context, 'this/theotherthing', 'that') |
e62e47
|
62 |
self.assertEqual( |
0c29cf
|
63 |
result, 'http://example.com:5432/context/this%2Ftheotherthing/that' |
MM |
64 |
) |
7ae0c2
|
65 |
|
fb90f0
|
66 |
def test_resource_url_unicode_in_element_names(self): |
CM |
67 |
request = self._makeOne() |
c51896
|
68 |
self._registerResourceURL(request.registry) |
a84e17
|
69 |
uc = text_(b'La Pe\xc3\xb1a', 'utf-8') |
e62e47
|
70 |
context = DummyContext() |
fb90f0
|
71 |
result = request.resource_url(context, uc) |
0c29cf
|
72 |
self.assertEqual( |
MM |
73 |
result, 'http://example.com:5432/context/La%20Pe%C3%B1a' |
|
74 |
) |
7ae0c2
|
75 |
|
fb90f0
|
76 |
def test_resource_url_at_sign_in_element_names(self): |
CM |
77 |
request = self._makeOne() |
c51896
|
78 |
self._registerResourceURL(request.registry) |
ab19ea
|
79 |
context = DummyContext() |
fb90f0
|
80 |
result = request.resource_url(context, '@@myview') |
0c29cf
|
81 |
self.assertEqual(result, 'http://example.com:5432/context/@@myview') |
ab19ea
|
82 |
|
fb90f0
|
83 |
def test_resource_url_element_names_url_quoted(self): |
CM |
84 |
request = self._makeOne() |
c51896
|
85 |
self._registerResourceURL(request.registry) |
e62e47
|
86 |
context = DummyContext() |
fb90f0
|
87 |
result = request.resource_url(context, 'a b c') |
c51896
|
88 |
self.assertEqual(result, 'http://example.com:5432/context/a%20b%20c') |
7ae0c2
|
89 |
|
e967a9
|
90 |
def test_resource_url_with_query_str(self): |
MM |
91 |
request = self._makeOne() |
|
92 |
self._registerResourceURL(request.registry) |
|
93 |
context = DummyContext() |
|
94 |
result = request.resource_url(context, 'a', query='(openlayers)') |
0c29cf
|
95 |
self.assertEqual( |
MM |
96 |
result, 'http://example.com:5432/context/a?(openlayers)' |
|
97 |
) |
e967a9
|
98 |
|
fb90f0
|
99 |
def test_resource_url_with_query_dict(self): |
CM |
100 |
request = self._makeOne() |
c51896
|
101 |
self._registerResourceURL(request.registry) |
e62e47
|
102 |
context = DummyContext() |
a84e17
|
103 |
uc = text_(b'La Pe\xc3\xb1a', 'utf-8') |
0c29cf
|
104 |
result = request.resource_url(context, 'a', query={'a': uc}) |
MM |
105 |
self.assertEqual( |
|
106 |
result, 'http://example.com:5432/context/a?a=La+Pe%C3%B1a' |
|
107 |
) |
7ae0c2
|
108 |
|
fb90f0
|
109 |
def test_resource_url_with_query_seq(self): |
CM |
110 |
request = self._makeOne() |
c51896
|
111 |
self._registerResourceURL(request.registry) |
e62e47
|
112 |
context = DummyContext() |
a84e17
|
113 |
uc = text_(b'La Pe\xc3\xb1a', 'utf-8') |
0c29cf
|
114 |
result = request.resource_url( |
MM |
115 |
context, 'a', query=[('a', 'hi there'), ('b', uc)] |
|
116 |
) |
|
117 |
self.assertEqual( |
|
118 |
result, |
|
119 |
'http://example.com:5432/context/a?a=hi+there&b=La+Pe%C3%B1a', |
|
120 |
) |
7ae0c2
|
121 |
|
0e0c83
|
122 |
def test_resource_url_with_query_empty(self): |
CL |
123 |
request = self._makeOne() |
|
124 |
self._registerResourceURL(request.registry) |
|
125 |
context = DummyContext() |
|
126 |
result = request.resource_url(context, 'a', query=[]) |
0c29cf
|
127 |
self.assertEqual(result, 'http://example.com:5432/context/a') |
0e0c83
|
128 |
|
effe0e
|
129 |
def test_resource_url_with_query_None(self): |
MM |
130 |
request = self._makeOne() |
|
131 |
self._registerResourceURL(request.registry) |
|
132 |
context = DummyContext() |
|
133 |
result = request.resource_url(context, 'a', query=None) |
0c29cf
|
134 |
self.assertEqual(result, 'http://example.com:5432/context/a') |
effe0e
|
135 |
|
fb90f0
|
136 |
def test_resource_url_anchor_is_after_root_when_no_elements(self): |
CM |
137 |
request = self._makeOne() |
c51896
|
138 |
self._registerResourceURL(request.registry) |
e693ce
|
139 |
context = DummyContext() |
fb90f0
|
140 |
result = request.resource_url(context, anchor='a') |
0c29cf
|
141 |
self.assertEqual(result, 'http://example.com:5432/context/#a') |
e693ce
|
142 |
|
fb90f0
|
143 |
def test_resource_url_anchor_is_after_elements_when_no_qs(self): |
CM |
144 |
request = self._makeOne() |
c51896
|
145 |
self._registerResourceURL(request.registry) |
e693ce
|
146 |
context = DummyContext() |
fb90f0
|
147 |
result = request.resource_url(context, 'a', anchor='b') |
0c29cf
|
148 |
self.assertEqual(result, 'http://example.com:5432/context/a#b') |
e693ce
|
149 |
|
fb90f0
|
150 |
def test_resource_url_anchor_is_after_qs_when_qs_is_present(self): |
CM |
151 |
request = self._makeOne() |
c51896
|
152 |
self._registerResourceURL(request.registry) |
e693ce
|
153 |
context = DummyContext() |
0c29cf
|
154 |
result = request.resource_url( |
MM |
155 |
context, 'a', query={'b': 'c'}, anchor='d' |
|
156 |
) |
|
157 |
self.assertEqual(result, 'http://example.com:5432/context/a?b=c#d') |
e693ce
|
158 |
|
fb90f0
|
159 |
def test_resource_url_anchor_is_encoded_utf8_if_unicode(self): |
CM |
160 |
request = self._makeOne() |
c51896
|
161 |
self._registerResourceURL(request.registry) |
e693ce
|
162 |
context = DummyContext() |
fc30e8
|
163 |
uc = text_(b'La Pe\xc3\xb1a', 'utf-8') |
fb90f0
|
164 |
result = request.resource_url(context, anchor=uc) |
0c29cf
|
165 |
self.assertEqual( |
MM |
166 |
result, 'http://example.com:5432/context/#La%20Pe%C3%B1a' |
|
167 |
) |
e693ce
|
168 |
|
af3134
|
169 |
def test_resource_url_anchor_is_urlencoded_safe(self): |
fb90f0
|
170 |
request = self._makeOne() |
c51896
|
171 |
self._registerResourceURL(request.registry) |
e693ce
|
172 |
context = DummyContext() |
af3134
|
173 |
result = request.resource_url(context, anchor=' /#?&+') |
0c29cf
|
174 |
self.assertEqual(result, 'http://example.com:5432/context/#%20/%23?&+') |
effe0e
|
175 |
|
MM |
176 |
def test_resource_url_anchor_is_None(self): |
|
177 |
request = self._makeOne() |
|
178 |
self._registerResourceURL(request.registry) |
|
179 |
context = DummyContext() |
|
180 |
result = request.resource_url(context, anchor=None) |
|
181 |
self.assertEqual(result, 'http://example.com:5432/context/') |
e693ce
|
182 |
|
c51896
|
183 |
def test_resource_url_no_IResourceURL_registered(self): |
CM |
184 |
# falls back to ResourceURL |
21ad66
|
185 |
root = DummyContext() |
CM |
186 |
root.__name__ = '' |
|
187 |
root.__parent__ = None |
fb90f0
|
188 |
request = self._makeOne() |
21ad66
|
189 |
request.environ = {} |
fb90f0
|
190 |
result = request.resource_url(root) |
21ad66
|
191 |
self.assertEqual(result, 'http://example.com:5432/') |
CM |
192 |
|
fb90f0
|
193 |
def test_resource_url_no_registry_on_request(self): |
CM |
194 |
request = self._makeOne() |
c51896
|
195 |
self._registerResourceURL(request.registry) |
fb90f0
|
196 |
del request.registry |
45d08c
|
197 |
root = DummyContext() |
fb90f0
|
198 |
result = request.resource_url(root) |
c51896
|
199 |
self.assertEqual(result, 'http://example.com:5432/context/') |
0c29cf
|
200 |
|
c51896
|
201 |
def test_resource_url_with_app_url(self): |
CM |
202 |
request = self._makeOne() |
|
203 |
self._registerResourceURL(request.registry) |
|
204 |
root = DummyContext() |
|
205 |
result = request.resource_url(root, app_url='http://somewhere.com') |
|
206 |
self.assertEqual(result, 'http://somewhere.com/context/') |
|
207 |
|
|
208 |
def test_resource_url_with_scheme(self): |
|
209 |
environ = { |
0c29cf
|
210 |
'wsgi.url_scheme': 'http', |
MM |
211 |
'SERVER_PORT': '8080', |
|
212 |
'SERVER_NAME': 'example.com', |
|
213 |
} |
c51896
|
214 |
request = self._makeOne(environ) |
CM |
215 |
self._registerResourceURL(request.registry) |
|
216 |
root = DummyContext() |
|
217 |
result = request.resource_url(root, scheme='https') |
|
218 |
self.assertEqual(result, 'https://example.com/context/') |
|
219 |
|
|
220 |
def test_resource_url_with_host(self): |
|
221 |
environ = { |
0c29cf
|
222 |
'wsgi.url_scheme': 'http', |
MM |
223 |
'SERVER_PORT': '8080', |
|
224 |
'SERVER_NAME': 'example.com', |
|
225 |
} |
c51896
|
226 |
request = self._makeOne(environ) |
CM |
227 |
self._registerResourceURL(request.registry) |
|
228 |
root = DummyContext() |
|
229 |
result = request.resource_url(root, host='someotherhost.com') |
|
230 |
self.assertEqual(result, 'http://someotherhost.com:8080/context/') |
|
231 |
|
|
232 |
def test_resource_url_with_port(self): |
|
233 |
environ = { |
0c29cf
|
234 |
'wsgi.url_scheme': 'http', |
MM |
235 |
'SERVER_PORT': '8080', |
|
236 |
'SERVER_NAME': 'example.com', |
|
237 |
} |
c51896
|
238 |
request = self._makeOne(environ) |
CM |
239 |
self._registerResourceURL(request.registry) |
|
240 |
root = DummyContext() |
|
241 |
result = request.resource_url(root, port='8181') |
|
242 |
self.assertEqual(result, 'http://example.com:8181/context/') |
|
243 |
|
|
244 |
def test_resource_url_with_local_url(self): |
|
245 |
environ = { |
0c29cf
|
246 |
'wsgi.url_scheme': 'http', |
MM |
247 |
'SERVER_PORT': '8080', |
|
248 |
'SERVER_NAME': 'example.com', |
|
249 |
} |
c51896
|
250 |
request = self._makeOne(environ) |
CM |
251 |
self._registerResourceURL(request.registry) |
|
252 |
root = DummyContext() |
0c29cf
|
253 |
|
c51896
|
254 |
def resource_url(req, info): |
CM |
255 |
self.assertEqual(req, request) |
|
256 |
self.assertEqual(info['virtual_path'], '/context/') |
|
257 |
self.assertEqual(info['physical_path'], '/context/') |
|
258 |
self.assertEqual(info['app_url'], 'http://example.com:5432') |
|
259 |
return 'http://example.com/contextabc/' |
0c29cf
|
260 |
|
c51896
|
261 |
root.__resource_url__ = resource_url |
CM |
262 |
result = request.resource_url(root) |
|
263 |
self.assertEqual(result, 'http://example.com/contextabc/') |
60aeef
|
264 |
|
CM |
265 |
def test_resource_url_with_route_name_no_remainder_on_adapter(self): |
|
266 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
267 |
|
60aeef
|
268 |
environ = { |
0c29cf
|
269 |
'wsgi.url_scheme': 'http', |
MM |
270 |
'SERVER_PORT': '8080', |
|
271 |
'SERVER_NAME': 'example.com', |
|
272 |
} |
60aeef
|
273 |
request = self._makeOne(environ) |
CM |
274 |
adapter = self._registerResourceURL(request.registry) |
|
275 |
# no virtual_path_tuple on adapter |
|
276 |
adapter.virtual_path = '/a/b/c/' |
|
277 |
route = DummyRoute('/1/2/3') |
|
278 |
mapper = DummyRoutesMapper(route) |
|
279 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
280 |
root = DummyContext() |
|
281 |
result = request.resource_url(root, route_name='foo') |
|
282 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
c29603
|
283 |
self.assertEqual(route.kw, {'traverse': ('', 'a', 'b', 'c', '')}) |
60aeef
|
284 |
|
CM |
285 |
def test_resource_url_with_route_name_remainder_on_adapter(self): |
|
286 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
287 |
|
60aeef
|
288 |
environ = { |
0c29cf
|
289 |
'wsgi.url_scheme': 'http', |
MM |
290 |
'SERVER_PORT': '8080', |
|
291 |
'SERVER_NAME': 'example.com', |
|
292 |
} |
60aeef
|
293 |
request = self._makeOne(environ) |
CM |
294 |
adapter = self._registerResourceURL(request.registry) |
|
295 |
# virtual_path_tuple on adapter |
|
296 |
adapter.virtual_path_tuple = ('', 'a', 'b', 'c', '') |
|
297 |
route = DummyRoute('/1/2/3') |
|
298 |
mapper = DummyRoutesMapper(route) |
|
299 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
300 |
root = DummyContext() |
|
301 |
result = request.resource_url(root, route_name='foo') |
|
302 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
c29603
|
303 |
self.assertEqual(route.kw, {'traverse': ('', 'a', 'b', 'c', '')}) |
60aeef
|
304 |
|
CM |
305 |
def test_resource_url_with_route_name_and_app_url(self): |
|
306 |
from pyramid.interfaces import IRoutesMapper |
|
307 |
|
|
308 |
environ = { |
0c29cf
|
309 |
'wsgi.url_scheme': 'http', |
MM |
310 |
'SERVER_PORT': '8080', |
|
311 |
'SERVER_NAME': 'example.com', |
|
312 |
} |
60aeef
|
313 |
request = self._makeOne(environ) |
CM |
314 |
adapter = self._registerResourceURL(request.registry) |
|
315 |
# virtual_path_tuple on adapter |
|
316 |
adapter.virtual_path_tuple = ('', 'a', 'b', 'c', '') |
|
317 |
route = DummyRoute('/1/2/3') |
|
318 |
mapper = DummyRoutesMapper(route) |
|
319 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
320 |
root = DummyContext() |
|
321 |
result = request.resource_url( |
0c29cf
|
322 |
root, route_name='foo', app_url='app_url' |
MM |
323 |
) |
|
324 |
self.assertEqual(result, 'app_url/1/2/3') |
|
325 |
self.assertEqual(route.kw, {'traverse': ('', 'a', 'b', 'c', '')}) |
|
326 |
|
|
327 |
def test_resource_url_with_route_name_and_scheme_host_port_etc(self): |
|
328 |
from pyramid.interfaces import IRoutesMapper |
|
329 |
|
|
330 |
environ = { |
|
331 |
'wsgi.url_scheme': 'http', |
|
332 |
'SERVER_PORT': '8080', |
|
333 |
'SERVER_NAME': 'example.com', |
|
334 |
} |
|
335 |
request = self._makeOne(environ) |
|
336 |
adapter = self._registerResourceURL(request.registry) |
|
337 |
# virtual_path_tuple on adapter |
|
338 |
adapter.virtual_path_tuple = ('', 'a', 'b', 'c', '') |
|
339 |
route = DummyRoute('/1/2/3') |
|
340 |
mapper = DummyRoutesMapper(route) |
|
341 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
342 |
root = DummyContext() |
|
343 |
result = request.resource_url( |
|
344 |
root, |
|
345 |
route_name='foo', |
|
346 |
scheme='scheme', |
|
347 |
host='host', |
|
348 |
port='port', |
|
349 |
query={'a': '1'}, |
|
350 |
anchor='anchor', |
|
351 |
) |
|
352 |
self.assertEqual(result, 'scheme://host:port/1/2/3?a=1#anchor') |
|
353 |
self.assertEqual(route.kw, {'traverse': ('', 'a', 'b', 'c', '')}) |
|
354 |
|
|
355 |
def test_resource_url_with_route_name_and_route_kwargs(self): |
|
356 |
from pyramid.interfaces import IRoutesMapper |
|
357 |
|
|
358 |
environ = { |
|
359 |
'wsgi.url_scheme': 'http', |
|
360 |
'SERVER_PORT': '8080', |
|
361 |
'SERVER_NAME': 'example.com', |
|
362 |
} |
|
363 |
request = self._makeOne(environ) |
|
364 |
adapter = self._registerResourceURL(request.registry) |
|
365 |
# virtual_path_tuple on adapter |
|
366 |
adapter.virtual_path_tuple = ('', 'a', 'b', 'c', '') |
|
367 |
route = DummyRoute('/1/2/3') |
|
368 |
mapper = DummyRoutesMapper(route) |
|
369 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
370 |
root = DummyContext() |
|
371 |
result = request.resource_url( |
|
372 |
root, route_name='foo', route_kw={'a': '1', 'b': '2'} |
|
373 |
) |
60aeef
|
374 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
CM |
375 |
self.assertEqual( |
0c29cf
|
376 |
route.kw, {'traverse': ('', 'a', 'b', 'c', ''), 'a': '1', 'b': '2'} |
MM |
377 |
) |
60aeef
|
378 |
|
CM |
379 |
def test_resource_url_with_route_name_and_elements(self): |
|
380 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
381 |
|
60aeef
|
382 |
environ = { |
0c29cf
|
383 |
'wsgi.url_scheme': 'http', |
MM |
384 |
'SERVER_PORT': '8080', |
|
385 |
'SERVER_NAME': 'example.com', |
|
386 |
} |
60aeef
|
387 |
request = self._makeOne(environ) |
CM |
388 |
adapter = self._registerResourceURL(request.registry) |
|
389 |
# virtual_path_tuple on adapter |
|
390 |
adapter.virtual_path_tuple = ('', 'a', 'b', 'c', '') |
|
391 |
route = DummyRoute('/1/2/3') |
|
392 |
mapper = DummyRoutesMapper(route) |
|
393 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
394 |
root = DummyContext() |
|
395 |
result = request.resource_url(root, 'e1', 'e2', route_name='foo') |
0c29cf
|
396 |
self.assertEqual(result, 'http://example.com:5432/1/2/3/e1/e2') |
c29603
|
397 |
self.assertEqual(route.kw, {'traverse': ('', 'a', 'b', 'c', '')}) |
0c29cf
|
398 |
|
c29603
|
399 |
def test_resource_url_with_route_name_and_remainder_name(self): |
CM |
400 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
401 |
|
c29603
|
402 |
environ = { |
0c29cf
|
403 |
'wsgi.url_scheme': 'http', |
MM |
404 |
'SERVER_PORT': '8080', |
|
405 |
'SERVER_NAME': 'example.com', |
|
406 |
} |
c29603
|
407 |
request = self._makeOne(environ) |
CM |
408 |
adapter = self._registerResourceURL(request.registry) |
|
409 |
# virtual_path_tuple on adapter |
|
410 |
adapter.virtual_path_tuple = ('', 'a', 'b', 'c', '') |
|
411 |
route = DummyRoute('/1/2/3') |
|
412 |
mapper = DummyRoutesMapper(route) |
|
413 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
414 |
root = DummyContext() |
0c29cf
|
415 |
result = request.resource_url( |
MM |
416 |
root, route_name='foo', route_remainder_name='fred' |
|
417 |
) |
c29603
|
418 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
60aeef
|
419 |
self.assertEqual(route.kw, {'fred': ('', 'a', 'b', 'c', '')}) |
0c29cf
|
420 |
|
c51896
|
421 |
def test_resource_path(self): |
CM |
422 |
request = self._makeOne() |
|
423 |
self._registerResourceURL(request.registry) |
|
424 |
root = DummyContext() |
|
425 |
result = request.resource_path(root) |
|
426 |
self.assertEqual(result, '/context/') |
|
427 |
|
|
428 |
def test_resource_path_kwarg(self): |
|
429 |
request = self._makeOne() |
|
430 |
self._registerResourceURL(request.registry) |
|
431 |
root = DummyContext() |
|
432 |
result = request.resource_path(root, anchor='abc') |
|
433 |
self.assertEqual(result, '/context/#abc') |
0c29cf
|
434 |
|
fb90f0
|
435 |
def test_route_url_with_elements(self): |
b60bdb
|
436 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
437 |
|
fb90f0
|
438 |
request = self._makeOne() |
70f1cd
|
439 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
6fec21
|
440 |
request.registry.registerUtility(mapper, IRoutesMapper) |
fb90f0
|
441 |
result = request.route_url('flub', 'extra1', 'extra2') |
0c29cf
|
442 |
self.assertEqual(result, 'http://example.com:5432/1/2/3/extra1/extra2') |
c18e27
|
443 |
|
fb90f0
|
444 |
def test_route_url_with_elements_path_endswith_slash(self): |
c18e27
|
445 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
446 |
|
fb90f0
|
447 |
request = self._makeOne() |
c18e27
|
448 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3/')) |
CM |
449 |
request.registry.registerUtility(mapper, IRoutesMapper) |
fb90f0
|
450 |
result = request.route_url('flub', 'extra1', 'extra2') |
0c29cf
|
451 |
self.assertEqual(result, 'http://example.com:5432/1/2/3/extra1/extra2') |
05c023
|
452 |
|
fb90f0
|
453 |
def test_route_url_no_elements(self): |
b60bdb
|
454 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
455 |
|
fb90f0
|
456 |
request = self._makeOne() |
70f1cd
|
457 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
6fec21
|
458 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
459 |
result = request.route_url( |
MM |
460 |
'flub', a=1, b=2, c=3, _query={'a': 1}, _anchor=text_(b"foo") |
|
461 |
) |
|
462 |
self.assertEqual(result, 'http://example.com:5432/1/2/3?a=1#foo') |
c18e27
|
463 |
|
effe0e
|
464 |
def test_route_url_with_query_None(self): |
MM |
465 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
466 |
|
effe0e
|
467 |
request = self._makeOne() |
MM |
468 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
469 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
470 |
result = request.route_url('flub', a=1, b=2, c=3, _query=None) |
|
471 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
|
472 |
|
a7c67c
|
473 |
def test_route_url_with_anchor_binary(self): |
c18e27
|
474 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
475 |
|
fb90f0
|
476 |
request = self._makeOne() |
c18e27
|
477 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
CM |
478 |
request.registry.registerUtility(mapper, IRoutesMapper) |
a7c67c
|
479 |
result = request.route_url('flub', _anchor=b"La Pe\xc3\xb1a") |
CM |
480 |
|
0c29cf
|
481 |
self.assertEqual( |
MM |
482 |
result, 'http://example.com:5432/1/2/3#La%20Pe%C3%B1a' |
|
483 |
) |
c18e27
|
484 |
|
fb90f0
|
485 |
def test_route_url_with_anchor_unicode(self): |
c18e27
|
486 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
487 |
|
fb90f0
|
488 |
request = self._makeOne() |
c18e27
|
489 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
CM |
490 |
request.registry.registerUtility(mapper, IRoutesMapper) |
a84e17
|
491 |
anchor = text_(b'La Pe\xc3\xb1a', 'utf-8') |
fb90f0
|
492 |
result = request.route_url('flub', _anchor=anchor) |
a7c67c
|
493 |
|
0c29cf
|
494 |
self.assertEqual( |
MM |
495 |
result, 'http://example.com:5432/1/2/3#La%20Pe%C3%B1a' |
|
496 |
) |
c18e27
|
497 |
|
effe0e
|
498 |
def test_route_url_with_anchor_None(self): |
MM |
499 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
500 |
|
effe0e
|
501 |
request = self._makeOne() |
MM |
502 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
503 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
504 |
result = request.route_url('flub', _anchor=None) |
|
505 |
|
|
506 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
|
507 |
|
fb90f0
|
508 |
def test_route_url_with_query(self): |
c18e27
|
509 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
510 |
|
fb90f0
|
511 |
request = self._makeOne() |
c18e27
|
512 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
CM |
513 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
514 |
result = request.route_url('flub', _query={'q': '1'}) |
MM |
515 |
self.assertEqual(result, 'http://example.com:5432/1/2/3?q=1') |
c18e27
|
516 |
|
e967a9
|
517 |
def test_route_url_with_query_str(self): |
MM |
518 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
519 |
|
e967a9
|
520 |
request = self._makeOne() |
MM |
521 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
522 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
523 |
result = request.route_url('flub', _query='(openlayers)') |
0c29cf
|
524 |
self.assertEqual(result, 'http://example.com:5432/1/2/3?(openlayers)') |
e967a9
|
525 |
|
0e0c83
|
526 |
def test_route_url_with_empty_query(self): |
CL |
527 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
528 |
|
0e0c83
|
529 |
request = self._makeOne() |
CL |
530 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
531 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
532 |
result = request.route_url('flub', _query={}) |
0c29cf
|
533 |
self.assertEqual(result, 'http://example.com:5432/1/2/3') |
0e0c83
|
534 |
|
fb90f0
|
535 |
def test_route_url_with_app_url(self): |
c18e27
|
536 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
537 |
|
fb90f0
|
538 |
request = self._makeOne() |
c18e27
|
539 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
CM |
540 |
request.registry.registerUtility(mapper, IRoutesMapper) |
fb90f0
|
541 |
result = request.route_url('flub', _app_url='http://example2.com') |
0c29cf
|
542 |
self.assertEqual(result, 'http://example2.com/1/2/3') |
dc405b
|
543 |
|
c51896
|
544 |
def test_route_url_with_host(self): |
CM |
545 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
546 |
|
MM |
547 |
environ = {'wsgi.url_scheme': 'http', 'SERVER_PORT': '5432'} |
c51896
|
548 |
request = self._makeOne(environ) |
CM |
549 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
550 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
551 |
result = request.route_url('flub', _host='someotherhost.com') |
0c29cf
|
552 |
self.assertEqual(result, 'http://someotherhost.com:5432/1/2/3') |
c51896
|
553 |
|
CM |
554 |
def test_route_url_with_port(self): |
|
555 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
556 |
|
c51896
|
557 |
environ = { |
0c29cf
|
558 |
'wsgi.url_scheme': 'http', |
MM |
559 |
'SERVER_PORT': '5432', |
|
560 |
'SERVER_NAME': 'example.com', |
|
561 |
} |
c51896
|
562 |
request = self._makeOne(environ) |
CM |
563 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
564 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
565 |
result = request.route_url('flub', _port='8080') |
0c29cf
|
566 |
self.assertEqual(result, 'http://example.com:8080/1/2/3') |
c51896
|
567 |
|
CM |
568 |
def test_route_url_with_scheme(self): |
|
569 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
570 |
|
c51896
|
571 |
environ = { |
0c29cf
|
572 |
'wsgi.url_scheme': 'http', |
MM |
573 |
'SERVER_PORT': '5432', |
|
574 |
'SERVER_NAME': 'example.com', |
|
575 |
} |
c51896
|
576 |
request = self._makeOne(environ) |
CM |
577 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
578 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
579 |
result = request.route_url('flub', _scheme='https') |
0c29cf
|
580 |
self.assertEqual(result, 'https://example.com/1/2/3') |
MM |
581 |
|
fb90f0
|
582 |
def test_route_url_generation_error(self): |
b60bdb
|
583 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
584 |
|
fb90f0
|
585 |
request = self._makeOne() |
05c023
|
586 |
mapper = DummyRoutesMapper(raise_exc=KeyError) |
6fec21
|
587 |
request.registry.registerUtility(mapper, IRoutesMapper) |
05c023
|
588 |
mapper.raise_exc = KeyError |
fb90f0
|
589 |
self.assertRaises(KeyError, request.route_url, 'flub', request, a=1) |
750ce4
|
590 |
|
fb90f0
|
591 |
def test_route_url_generate_doesnt_receive_query_or_anchor(self): |
b60bdb
|
592 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
593 |
|
fb90f0
|
594 |
request = self._makeOne() |
70f1cd
|
595 |
route = DummyRoute(result='') |
CM |
596 |
mapper = DummyRoutesMapper(route=route) |
fb90f0
|
597 |
request.registry.registerUtility(mapper, IRoutesMapper) |
CM |
598 |
result = request.route_url('flub', _query=dict(name='some_name')) |
0c29cf
|
599 |
self.assertEqual(route.kw, {}) # shouldnt have anchor/query |
9d9ffb
|
600 |
self.assertEqual(result, 'http://example.com:5432?name=some_name') |
CM |
601 |
|
fb90f0
|
602 |
def test_route_url_with_pregenerator(self): |
b60bdb
|
603 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
604 |
|
fb90f0
|
605 |
request = self._makeOne() |
70f1cd
|
606 |
route = DummyRoute(result='/1/2/3') |
0c29cf
|
607 |
|
70f1cd
|
608 |
def pregenerator(request, elements, kw): |
0c29cf
|
609 |
return ('a',), {'_app_url': 'http://example2.com'} |
MM |
610 |
|
70f1cd
|
611 |
route.pregenerator = pregenerator |
CM |
612 |
mapper = DummyRoutesMapper(route=route) |
|
613 |
request.registry.registerUtility(mapper, IRoutesMapper) |
fb90f0
|
614 |
result = request.route_url('flub') |
0c29cf
|
615 |
self.assertEqual(result, 'http://example2.com/1/2/3/a') |
MM |
616 |
self.assertEqual(route.kw, {}) # shouldnt have anchor/query |
b29429
|
617 |
|
fb90f0
|
618 |
def test_route_url_with_anchor_app_url_elements_and_query(self): |
c18e27
|
619 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
620 |
|
fb90f0
|
621 |
request = self._makeOne() |
c18e27
|
622 |
mapper = DummyRoutesMapper(route=DummyRoute(result='/1/2/3')) |
CM |
623 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
624 |
result = request.route_url( |
MM |
625 |
'flub', |
|
626 |
'element1', |
|
627 |
_app_url='http://example2.com', |
|
628 |
_anchor='anchor', |
|
629 |
_query={'q': '1'}, |
|
630 |
) |
|
631 |
self.assertEqual( |
|
632 |
result, 'http://example2.com/1/2/3/element1?q=1#anchor' |
|
633 |
) |
db0185
|
634 |
|
fb90f0
|
635 |
def test_route_url_integration_with_real_request(self): |
311ccc
|
636 |
# to try to replicate https://github.com/Pylons/pyramid/issues/213 |
CM |
637 |
from pyramid.interfaces import IRoutesMapper |
|
638 |
from pyramid.request import Request |
0c29cf
|
639 |
|
311ccc
|
640 |
request = Request.blank('/') |
CM |
641 |
request.registry = self.config.registry |
|
642 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
643 |
request.registry.registerUtility(mapper, IRoutesMapper) |
fb90f0
|
644 |
result = request.route_url('flub', 'extra1', 'extra2') |
0c29cf
|
645 |
self.assertEqual(result, 'http://localhost/1/2/3/extra1/extra2') |
311ccc
|
646 |
|
fb90f0
|
647 |
def test_current_route_url_current_request_has_no_route(self): |
CM |
648 |
request = self._makeOne() |
|
649 |
self.assertRaises(ValueError, request.current_route_url) |
5653d1
|
650 |
|
fb90f0
|
651 |
def test_current_route_url_with_elements_query_and_anchor(self): |
5653d1
|
652 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
653 |
|
fb90f0
|
654 |
request = self._makeOne() |
5653d1
|
655 |
route = DummyRoute('/1/2/3') |
CM |
656 |
mapper = DummyRoutesMapper(route=route) |
|
657 |
request.matched_route = route |
|
658 |
request.matchdict = {} |
|
659 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
660 |
result = request.current_route_url( |
MM |
661 |
'extra1', 'extra2', _query={'a': 1}, _anchor=text_(b"foo") |
|
662 |
) |
|
663 |
self.assertEqual( |
|
664 |
result, 'http://example.com:5432/1/2/3/extra1/extra2?a=1#foo' |
|
665 |
) |
5653d1
|
666 |
|
fb90f0
|
667 |
def test_current_route_url_with_route_name(self): |
5653d1
|
668 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
669 |
|
fb90f0
|
670 |
request = self._makeOne() |
5653d1
|
671 |
route = DummyRoute('/1/2/3') |
CM |
672 |
mapper = DummyRoutesMapper(route=route) |
|
673 |
request.matched_route = route |
|
674 |
request.matchdict = {} |
|
675 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
676 |
result = request.current_route_url( |
MM |
677 |
'extra1', |
|
678 |
'extra2', |
|
679 |
_query={'a': 1}, |
|
680 |
_anchor=text_(b"foo"), |
|
681 |
_route_name='bar', |
|
682 |
) |
|
683 |
self.assertEqual( |
|
684 |
result, 'http://example.com:5432/1/2/3/extra1/extra2?a=1#foo' |
|
685 |
) |
5653d1
|
686 |
|
fe8c0f
|
687 |
def test_current_route_url_with_request_query(self): |
JV |
688 |
from pyramid.interfaces import IRoutesMapper |
|
689 |
from webob.multidict import GetDict |
0c29cf
|
690 |
|
fe8c0f
|
691 |
request = self._makeOne() |
JV |
692 |
request.GET = GetDict([('q', '123')], {}) |
|
693 |
route = DummyRoute('/1/2/3') |
|
694 |
mapper = DummyRoutesMapper(route=route) |
|
695 |
request.matched_route = route |
|
696 |
request.matchdict = {} |
|
697 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
698 |
result = request.current_route_url() |
0c29cf
|
699 |
self.assertEqual(result, 'http://example.com:5432/1/2/3?q=123') |
fe8c0f
|
700 |
|
6a4a34
|
701 |
def test_current_route_url_with_request_query_duplicate_entries(self): |
JV |
702 |
from pyramid.interfaces import IRoutesMapper |
|
703 |
from webob.multidict import GetDict |
0c29cf
|
704 |
|
6a4a34
|
705 |
request = self._makeOne() |
db0185
|
706 |
request.GET = GetDict( |
0c29cf
|
707 |
[('q', '123'), ('b', '2'), ('b', '2'), ('q', '456')], {} |
MM |
708 |
) |
6a4a34
|
709 |
route = DummyRoute('/1/2/3') |
JV |
710 |
mapper = DummyRoutesMapper(route=route) |
|
711 |
request.matched_route = route |
|
712 |
request.matchdict = {} |
|
713 |
request.registry.registerUtility(mapper, IRoutesMapper) |
|
714 |
result = request.current_route_url() |
0c29cf
|
715 |
self.assertEqual( |
MM |
716 |
result, 'http://example.com:5432/1/2/3?q=123&b=2&b=2&q=456' |
|
717 |
) |
6a4a34
|
718 |
|
fe8c0f
|
719 |
def test_current_route_url_with_query_override(self): |
JV |
720 |
from pyramid.interfaces import IRoutesMapper |
|
721 |
from webob.multidict import GetDict |
0c29cf
|
722 |
|
fe8c0f
|
723 |
request = self._makeOne() |
JV |
724 |
request.GET = GetDict([('q', '123')], {}) |
|
725 |
route = DummyRoute('/1/2/3') |
|
726 |
mapper = DummyRoutesMapper(route=route) |
|
727 |
request.matched_route = route |
|
728 |
request.matchdict = {} |
|
729 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
730 |
result = request.current_route_url(_query={'a': 1}) |
MM |
731 |
self.assertEqual(result, 'http://example.com:5432/1/2/3?a=1') |
fe8c0f
|
732 |
|
12cef0
|
733 |
def test_current_route_path(self): |
CM |
734 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
735 |
|
12cef0
|
736 |
request = self._makeOne() |
CM |
737 |
route = DummyRoute('/1/2/3') |
|
738 |
mapper = DummyRoutesMapper(route=route) |
|
739 |
request.matched_route = route |
|
740 |
request.matchdict = {} |
5c6963
|
741 |
request.script_name = '/script_name' |
12cef0
|
742 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
743 |
result = request.current_route_path( |
MM |
744 |
'extra1', 'extra2', _query={'a': 1}, _anchor=text_(b"foo") |
|
745 |
) |
5c6963
|
746 |
self.assertEqual(result, '/script_name/1/2/3/extra1/extra2?a=1#foo') |
6a4a34
|
747 |
|
fb90f0
|
748 |
def test_route_path_with_elements(self): |
2c9d14
|
749 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
750 |
|
fb90f0
|
751 |
request = self._makeOne() |
2c9d14
|
752 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
CM |
753 |
request.registry.registerUtility(mapper, IRoutesMapper) |
fb90f0
|
754 |
request.script_name = '' |
0c29cf
|
755 |
result = request.route_path( |
MM |
756 |
'flub', |
|
757 |
'extra1', |
|
758 |
'extra2', |
|
759 |
a=1, |
|
760 |
b=2, |
|
761 |
c=3, |
|
762 |
_query={'a': 1}, |
|
763 |
_anchor=text_(b"foo"), |
|
764 |
) |
2c9d14
|
765 |
self.assertEqual(result, '/1/2/3/extra1/extra2?a=1#foo') |
CM |
766 |
|
fb90f0
|
767 |
def test_route_path_with_script_name(self): |
0a0edf
|
768 |
from pyramid.interfaces import IRoutesMapper |
0c29cf
|
769 |
|
fb90f0
|
770 |
request = self._makeOne() |
0a0edf
|
771 |
request.script_name = '/foo' |
CM |
772 |
mapper = DummyRoutesMapper(route=DummyRoute('/1/2/3')) |
|
773 |
request.registry.registerUtility(mapper, IRoutesMapper) |
0c29cf
|
774 |
result = request.route_path( |
MM |
775 |
'flub', |
|
776 |
'extra1', |
|
777 |
'extra2', |
|
778 |
a=1, |
|
779 |
b=2, |
|
780 |
c=3, |
|
781 |
_query={'a': 1}, |
|
782 |
_anchor=text_(b"foo"), |
|
783 |
) |
0a0edf
|
784 |
self.assertEqual(result, '/foo/1/2/3/extra1/extra2?a=1#foo') |
0c29cf
|
785 |
|
fb90f0
|
786 |
def test_static_url_staticurlinfo_notfound(self): |
CM |
787 |
request = self._makeOne() |
|
788 |
self.assertRaises(ValueError, request.static_url, 'static/foo.css') |
750ce4
|
789 |
|
fb90f0
|
790 |
def test_static_url_abspath(self): |
b8c797
|
791 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
792 |
|
fb90f0
|
793 |
request = self._makeOne() |
b8c797
|
794 |
info = DummyStaticURLInfo('abc') |
CM |
795 |
registry = request.registry |
|
796 |
registry.registerUtility(info, IStaticURLInfo) |
|
797 |
abspath = makeabs('static', 'foo.css') |
|
798 |
result = request.static_url(abspath) |
|
799 |
self.assertEqual(result, 'abc') |
0c29cf
|
800 |
self.assertEqual( |
MM |
801 |
info.args, (makeabs('static', 'foo.css'), request, {}) |
|
802 |
) |
b8c797
|
803 |
request = self._makeOne() |
750ce4
|
804 |
|
fb90f0
|
805 |
def test_static_url_found_rel(self): |
b60bdb
|
806 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
807 |
|
fb90f0
|
808 |
request = self._makeOne() |
b29429
|
809 |
info = DummyStaticURLInfo('abc') |
CM |
810 |
request.registry.registerUtility(info, IStaticURLInfo) |
fb90f0
|
811 |
result = request.static_url('static/foo.css') |
b29429
|
812 |
self.assertEqual(result, 'abc') |
0c29cf
|
813 |
self.assertEqual(info.args, ('tests:static/foo.css', request, {})) |
750ce4
|
814 |
|
fb90f0
|
815 |
def test_static_url_abs(self): |
b60bdb
|
816 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
817 |
|
fb90f0
|
818 |
request = self._makeOne() |
b29429
|
819 |
info = DummyStaticURLInfo('abc') |
CM |
820 |
request.registry.registerUtility(info, IStaticURLInfo) |
dd3cc8
|
821 |
result = request.static_url('tests:static/foo.css') |
b29429
|
822 |
self.assertEqual(result, 'abc') |
0c29cf
|
823 |
self.assertEqual(info.args, ('tests:static/foo.css', request, {})) |
750ce4
|
824 |
|
fb90f0
|
825 |
def test_static_url_found_abs_no_registry_on_request(self): |
b60bdb
|
826 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
827 |
|
fb90f0
|
828 |
request = self._makeOne() |
CM |
829 |
registry = request.registry |
b29429
|
830 |
info = DummyStaticURLInfo('abc') |
CM |
831 |
registry.registerUtility(info, IStaticURLInfo) |
fb90f0
|
832 |
del request.registry |
dd3cc8
|
833 |
result = request.static_url('tests:static/foo.css') |
b29429
|
834 |
self.assertEqual(result, 'abc') |
0c29cf
|
835 |
self.assertEqual(info.args, ('tests:static/foo.css', request, {})) |
5c6963
|
836 |
|
b8c797
|
837 |
def test_static_url_abspath_integration_with_staticurlinfo(self): |
CM |
838 |
from pyramid.interfaces import IStaticURLInfo |
53d9d4
|
839 |
from pyramid.config.views import StaticURLInfo |
0c29cf
|
840 |
|
cda7f6
|
841 |
info = StaticURLInfo() |
b8c797
|
842 |
here = os.path.abspath(os.path.dirname(__file__)) |
cda7f6
|
843 |
info.add(self.config, 'absstatic', here) |
5c6963
|
844 |
request = self._makeOne() |
b8c797
|
845 |
registry = request.registry |
CM |
846 |
registry.registerUtility(info, IStaticURLInfo) |
|
847 |
abspath = os.path.join(here, 'test_url.py') |
|
848 |
result = request.static_url(abspath) |
0c29cf
|
849 |
self.assertEqual( |
MM |
850 |
result, 'http://example.com:5432/absstatic/test_url.py' |
|
851 |
) |
b8c797
|
852 |
|
ff41f8
|
853 |
def test_static_url_noscheme_uses_scheme_from_request(self): |
WS |
854 |
from pyramid.interfaces import IStaticURLInfo |
|
855 |
from pyramid.config.views import StaticURLInfo |
0c29cf
|
856 |
|
ff41f8
|
857 |
info = StaticURLInfo() |
WS |
858 |
here = os.path.abspath(os.path.dirname(__file__)) |
|
859 |
info.add(self.config, '//subdomain.example.com/static', here) |
|
860 |
request = self._makeOne({'wsgi.url_scheme': 'https'}) |
|
861 |
registry = request.registry |
|
862 |
registry.registerUtility(info, IStaticURLInfo) |
|
863 |
abspath = os.path.join(here, 'test_url.py') |
|
864 |
result = request.static_url(abspath) |
0c29cf
|
865 |
self.assertEqual( |
MM |
866 |
result, 'https://subdomain.example.com/static/test_url.py' |
|
867 |
) |
ff41f8
|
868 |
|
b8c797
|
869 |
def test_static_path_abspath(self): |
CM |
870 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
871 |
|
b8c797
|
872 |
request = self._makeOne() |
CM |
873 |
request.script_name = '/foo' |
|
874 |
info = DummyStaticURLInfo('abc') |
|
875 |
registry = request.registry |
|
876 |
registry.registerUtility(info, IStaticURLInfo) |
|
877 |
abspath = makeabs('static', 'foo.css') |
|
878 |
result = request.static_path(abspath) |
|
879 |
self.assertEqual(result, 'abc') |
0c29cf
|
880 |
self.assertEqual( |
MM |
881 |
info.args, |
|
882 |
(makeabs('static', 'foo.css'), request, {'_app_url': '/foo'}), |
|
883 |
) |
5c6963
|
884 |
|
CM |
885 |
def test_static_path_found_rel(self): |
|
886 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
887 |
|
5c6963
|
888 |
request = self._makeOne() |
CM |
889 |
request.script_name = '/foo' |
|
890 |
info = DummyStaticURLInfo('abc') |
|
891 |
request.registry.registerUtility(info, IStaticURLInfo) |
|
892 |
result = request.static_path('static/foo.css') |
|
893 |
self.assertEqual(result, 'abc') |
0c29cf
|
894 |
self.assertEqual( |
MM |
895 |
info.args, ('tests:static/foo.css', request, {'_app_url': '/foo'}) |
|
896 |
) |
5c6963
|
897 |
|
CM |
898 |
def test_static_path_abs(self): |
|
899 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
900 |
|
5c6963
|
901 |
request = self._makeOne() |
CM |
902 |
request.script_name = '/foo' |
|
903 |
info = DummyStaticURLInfo('abc') |
|
904 |
request.registry.registerUtility(info, IStaticURLInfo) |
dd3cc8
|
905 |
result = request.static_path('tests:static/foo.css') |
5c6963
|
906 |
self.assertEqual(result, 'abc') |
0c29cf
|
907 |
self.assertEqual( |
MM |
908 |
info.args, ('tests:static/foo.css', request, {'_app_url': '/foo'}) |
|
909 |
) |
5c6963
|
910 |
|
CM |
911 |
def test_static_path(self): |
|
912 |
from pyramid.interfaces import IStaticURLInfo |
0c29cf
|
913 |
|
5c6963
|
914 |
request = self._makeOne() |
CM |
915 |
request.script_name = '/foo' |
|
916 |
info = DummyStaticURLInfo('abc') |
|
917 |
request.registry.registerUtility(info, IStaticURLInfo) |
|
918 |
result = request.static_path('static/foo.css') |
|
919 |
self.assertEqual(result, 'abc') |
0c29cf
|
920 |
self.assertEqual( |
MM |
921 |
info.args, ('tests:static/foo.css', request, {'_app_url': '/foo'}) |
|
922 |
) |
fb90f0
|
923 |
|
c51896
|
924 |
def test_partial_application_url_with_http_host_default_port_http(self): |
0c29cf
|
925 |
environ = {'wsgi.url_scheme': 'http', 'HTTP_HOST': 'example.com:80'} |
c51896
|
926 |
request = self._makeOne(environ) |
5b1e2a
|
927 |
result = request._partial_application_url() |
c51896
|
928 |
self.assertEqual(result, 'http://example.com') |
CM |
929 |
|
|
930 |
def test_partial_application_url_with_http_host_default_port_https(self): |
0c29cf
|
931 |
environ = {'wsgi.url_scheme': 'https', 'HTTP_HOST': 'example.com:443'} |
c51896
|
932 |
request = self._makeOne(environ) |
5b1e2a
|
933 |
result = request._partial_application_url() |
c51896
|
934 |
self.assertEqual(result, 'https://example.com') |
CM |
935 |
|
|
936 |
def test_partial_application_url_with_http_host_nondefault_port_http(self): |
0c29cf
|
937 |
environ = {'wsgi.url_scheme': 'http', 'HTTP_HOST': 'example.com:8080'} |
c51896
|
938 |
request = self._makeOne(environ) |
5b1e2a
|
939 |
result = request._partial_application_url() |
c51896
|
940 |
self.assertEqual(result, 'http://example.com:8080') |
CM |
941 |
|
0c29cf
|
942 |
def test_partial_application_url_with_http_host_nondefault_port_https( |
MM |
943 |
self |
|
944 |
): |
|
945 |
environ = {'wsgi.url_scheme': 'https', 'HTTP_HOST': 'example.com:4443'} |
c51896
|
946 |
request = self._makeOne(environ) |
5b1e2a
|
947 |
result = request._partial_application_url() |
c51896
|
948 |
self.assertEqual(result, 'https://example.com:4443') |
CM |
949 |
|
|
950 |
def test_partial_application_url_with_http_host_no_colon(self): |
|
951 |
environ = { |
0c29cf
|
952 |
'wsgi.url_scheme': 'http', |
MM |
953 |
'HTTP_HOST': 'example.com', |
|
954 |
'SERVER_PORT': '80', |
|
955 |
} |
c51896
|
956 |
request = self._makeOne(environ) |
5b1e2a
|
957 |
result = request._partial_application_url() |
c51896
|
958 |
self.assertEqual(result, 'http://example.com') |
CM |
959 |
|
|
960 |
def test_partial_application_url_no_http_host(self): |
|
961 |
environ = { |
0c29cf
|
962 |
'wsgi.url_scheme': 'http', |
MM |
963 |
'SERVER_NAME': 'example.com', |
|
964 |
'SERVER_PORT': '80', |
|
965 |
} |
c51896
|
966 |
request = self._makeOne(environ) |
5b1e2a
|
967 |
result = request._partial_application_url() |
c51896
|
968 |
self.assertEqual(result, 'http://example.com') |
0c29cf
|
969 |
|
c51896
|
970 |
def test_partial_application_replace_port(self): |
CM |
971 |
environ = { |
0c29cf
|
972 |
'wsgi.url_scheme': 'http', |
MM |
973 |
'SERVER_NAME': 'example.com', |
|
974 |
'SERVER_PORT': '80', |
|
975 |
} |
c51896
|
976 |
request = self._makeOne(environ) |
5b1e2a
|
977 |
result = request._partial_application_url(port=8080) |
c51896
|
978 |
self.assertEqual(result, 'http://example.com:8080') |
CM |
979 |
|
|
980 |
def test_partial_application_replace_scheme_https_special_case(self): |
|
981 |
environ = { |
0c29cf
|
982 |
'wsgi.url_scheme': 'http', |
MM |
983 |
'SERVER_NAME': 'example.com', |
|
984 |
'SERVER_PORT': '80', |
|
985 |
} |
c51896
|
986 |
request = self._makeOne(environ) |
5b1e2a
|
987 |
result = request._partial_application_url(scheme='https') |
c51896
|
988 |
self.assertEqual(result, 'https://example.com') |
CM |
989 |
|
|
990 |
def test_partial_application_replace_scheme_https_special_case_avoid(self): |
|
991 |
environ = { |
0c29cf
|
992 |
'wsgi.url_scheme': 'http', |
MM |
993 |
'SERVER_NAME': 'example.com', |
|
994 |
'SERVER_PORT': '80', |
|
995 |
} |
c51896
|
996 |
request = self._makeOne(environ) |
5b1e2a
|
997 |
result = request._partial_application_url(scheme='https', port='8080') |
c51896
|
998 |
self.assertEqual(result, 'https://example.com:8080') |
CM |
999 |
|
|
1000 |
def test_partial_application_replace_scheme_http_special_case(self): |
|
1001 |
environ = { |
0c29cf
|
1002 |
'wsgi.url_scheme': 'https', |
MM |
1003 |
'SERVER_NAME': 'example.com', |
|
1004 |
'SERVER_PORT': '8080', |
|
1005 |
} |
c51896
|
1006 |
request = self._makeOne(environ) |
5b1e2a
|
1007 |
result = request._partial_application_url(scheme='http') |
c51896
|
1008 |
self.assertEqual(result, 'http://example.com') |
CM |
1009 |
|
|
1010 |
def test_partial_application_replace_scheme_http_special_case_avoid(self): |
|
1011 |
environ = { |
0c29cf
|
1012 |
'wsgi.url_scheme': 'https', |
MM |
1013 |
'SERVER_NAME': 'example.com', |
|
1014 |
'SERVER_PORT': '8000', |
|
1015 |
} |
c51896
|
1016 |
request = self._makeOne(environ) |
5b1e2a
|
1017 |
result = request._partial_application_url(scheme='http', port='8080') |
c51896
|
1018 |
self.assertEqual(result, 'http://example.com:8080') |
0c29cf
|
1019 |
|
c51896
|
1020 |
def test_partial_application_replace_host_no_port(self): |
CM |
1021 |
environ = { |
0c29cf
|
1022 |
'wsgi.url_scheme': 'http', |
MM |
1023 |
'SERVER_NAME': 'example.com', |
|
1024 |
'SERVER_PORT': '80', |
|
1025 |
} |
c51896
|
1026 |
request = self._makeOne(environ) |
5b1e2a
|
1027 |
result = request._partial_application_url(host='someotherhost.com') |
c51896
|
1028 |
self.assertEqual(result, 'http://someotherhost.com') |
CM |
1029 |
|
|
1030 |
def test_partial_application_replace_host_with_port(self): |
|
1031 |
environ = { |
0c29cf
|
1032 |
'wsgi.url_scheme': 'http', |
MM |
1033 |
'SERVER_NAME': 'example.com', |
|
1034 |
'SERVER_PORT': '8000', |
|
1035 |
} |
c51896
|
1036 |
request = self._makeOne(environ) |
0c29cf
|
1037 |
result = request._partial_application_url( |
MM |
1038 |
host='someotherhost.com:8080' |
|
1039 |
) |
c51896
|
1040 |
self.assertEqual(result, 'http://someotherhost.com:8080') |
CM |
1041 |
|
|
1042 |
def test_partial_application_replace_host_and_port(self): |
|
1043 |
environ = { |
0c29cf
|
1044 |
'wsgi.url_scheme': 'http', |
MM |
1045 |
'SERVER_NAME': 'example.com', |
|
1046 |
'SERVER_PORT': '80', |
|
1047 |
} |
c51896
|
1048 |
request = self._makeOne(environ) |
0c29cf
|
1049 |
result = request._partial_application_url( |
MM |
1050 |
host='someotherhost.com:8080', port='8000' |
|
1051 |
) |
c51896
|
1052 |
self.assertEqual(result, 'http://someotherhost.com:8000') |
CM |
1053 |
|
|
1054 |
def test_partial_application_replace_host_port_and_scheme(self): |
|
1055 |
environ = { |
0c29cf
|
1056 |
'wsgi.url_scheme': 'http', |
MM |
1057 |
'SERVER_NAME': 'example.com', |
|
1058 |
'SERVER_PORT': '80', |
|
1059 |
} |
c51896
|
1060 |
request = self._makeOne(environ) |
5b1e2a
|
1061 |
result = request._partial_application_url( |
0c29cf
|
1062 |
host='someotherhost.com:8080', port='8000', scheme='https' |
MM |
1063 |
) |
c51896
|
1064 |
self.assertEqual(result, 'https://someotherhost.com:8000') |
0c29cf
|
1065 |
|
c51896
|
1066 |
def test_partial_application_url_with_custom_script_name(self): |
CM |
1067 |
environ = { |
0c29cf
|
1068 |
'wsgi.url_scheme': 'http', |
MM |
1069 |
'SERVER_NAME': 'example.com', |
|
1070 |
'SERVER_PORT': '8000', |
|
1071 |
} |
c51896
|
1072 |
request = self._makeOne(environ) |
CM |
1073 |
request.script_name = '/abc' |
5b1e2a
|
1074 |
result = request._partial_application_url() |
0c29cf
|
1075 |
self.assertEqual(result, 'http://example.com:8000/abc') |
MM |
1076 |
|
|
1077 |
|
fb90f0
|
1078 |
class Test_route_url(unittest.TestCase): |
CM |
1079 |
def _callFUT(self, route_name, request, *elements, **kw): |
|
1080 |
from pyramid.url import route_url |
0c29cf
|
1081 |
|
fb90f0
|
1082 |
return route_url(route_name, request, *elements, **kw) |
CM |
1083 |
|
|
1084 |
def _makeRequest(self): |
|
1085 |
class Request(object): |
|
1086 |
def route_url(self, route_name, *elements, **kw): |
|
1087 |
self.route_name = route_name |
|
1088 |
self.elements = elements |
|
1089 |
self.kw = kw |
|
1090 |
return 'route url' |
0c29cf
|
1091 |
|
fb90f0
|
1092 |
return Request() |
CM |
1093 |
|
|
1094 |
def test_it(self): |
|
1095 |
request = self._makeRequest() |
|
1096 |
result = self._callFUT('abc', request, 'a', _app_url='') |
|
1097 |
self.assertEqual(result, 'route url') |
|
1098 |
self.assertEqual(request.route_name, 'abc') |
|
1099 |
self.assertEqual(request.elements, ('a',)) |
0c29cf
|
1100 |
self.assertEqual(request.kw, {'_app_url': ''}) |
MM |
1101 |
|
fb90f0
|
1102 |
|
CM |
1103 |
class Test_route_path(unittest.TestCase): |
|
1104 |
def _callFUT(self, route_name, request, *elements, **kw): |
|
1105 |
from pyramid.url import route_path |
0c29cf
|
1106 |
|
fb90f0
|
1107 |
return route_path(route_name, request, *elements, **kw) |
CM |
1108 |
|
|
1109 |
def _makeRequest(self): |
|
1110 |
class Request(object): |
|
1111 |
def route_path(self, route_name, *elements, **kw): |
|
1112 |
self.route_name = route_name |
|
1113 |
self.elements = elements |
|
1114 |
self.kw = kw |
|
1115 |
return 'route path' |
0c29cf
|
1116 |
|
fb90f0
|
1117 |
return Request() |
CM |
1118 |
|
|
1119 |
def test_it(self): |
|
1120 |
request = self._makeRequest() |
|
1121 |
result = self._callFUT('abc', request, 'a', _app_url='') |
|
1122 |
self.assertEqual(result, 'route path') |
|
1123 |
self.assertEqual(request.route_name, 'abc') |
|
1124 |
self.assertEqual(request.elements, ('a',)) |
0c29cf
|
1125 |
self.assertEqual(request.kw, {'_app_url': ''}) |
MM |
1126 |
|
fb90f0
|
1127 |
|
CM |
1128 |
class Test_resource_url(unittest.TestCase): |
|
1129 |
def _callFUT(self, resource, request, *elements, **kw): |
|
1130 |
from pyramid.url import resource_url |
0c29cf
|
1131 |
|
fb90f0
|
1132 |
return resource_url(resource, request, *elements, **kw) |
CM |
1133 |
|
|
1134 |
def _makeRequest(self): |
|
1135 |
class Request(object): |
|
1136 |
def resource_url(self, resource, *elements, **kw): |
|
1137 |
self.resource = resource |
|
1138 |
self.elements = elements |
|
1139 |
self.kw = kw |
|
1140 |
return 'resource url' |
0c29cf
|
1141 |
|
fb90f0
|
1142 |
return Request() |
CM |
1143 |
|
|
1144 |
def test_it(self): |
|
1145 |
request = self._makeRequest() |
|
1146 |
result = self._callFUT('abc', request, 'a', _app_url='') |
|
1147 |
self.assertEqual(result, 'resource url') |
|
1148 |
self.assertEqual(request.resource, 'abc') |
|
1149 |
self.assertEqual(request.elements, ('a',)) |
0c29cf
|
1150 |
self.assertEqual(request.kw, {'_app_url': ''}) |
MM |
1151 |
|
fb90f0
|
1152 |
|
CM |
1153 |
class Test_static_url(unittest.TestCase): |
|
1154 |
def _callFUT(self, path, request, **kw): |
|
1155 |
from pyramid.url import static_url |
0c29cf
|
1156 |
|
fb90f0
|
1157 |
return static_url(path, request, **kw) |
CM |
1158 |
|
|
1159 |
def _makeRequest(self): |
|
1160 |
class Request(object): |
|
1161 |
def static_url(self, path, **kw): |
|
1162 |
self.path = path |
|
1163 |
self.kw = kw |
|
1164 |
return 'static url' |
0c29cf
|
1165 |
|
fb90f0
|
1166 |
return Request() |
CM |
1167 |
|
b5c0cb
|
1168 |
def test_it_abs(self): |
CM |
1169 |
request = self._makeRequest() |
|
1170 |
result = self._callFUT('/foo/bar/abc', request, _app_url='') |
|
1171 |
self.assertEqual(result, 'static url') |
|
1172 |
self.assertEqual(request.path, '/foo/bar/abc') |
0c29cf
|
1173 |
self.assertEqual(request.kw, {'_app_url': ''}) |
b5c0cb
|
1174 |
|
CM |
1175 |
def test_it_absspec(self): |
|
1176 |
request = self._makeRequest() |
|
1177 |
result = self._callFUT('foo:abc', request, _anchor='anchor') |
|
1178 |
self.assertEqual(result, 'static url') |
|
1179 |
self.assertEqual(request.path, 'foo:abc') |
0c29cf
|
1180 |
self.assertEqual(request.kw, {'_anchor': 'anchor'}) |
b5c0cb
|
1181 |
|
CM |
1182 |
def test_it_rel(self): |
fb90f0
|
1183 |
request = self._makeRequest() |
CM |
1184 |
result = self._callFUT('abc', request, _app_url='') |
|
1185 |
self.assertEqual(result, 'static url') |
dd3cc8
|
1186 |
self.assertEqual(request.path, 'tests:abc') |
0c29cf
|
1187 |
self.assertEqual(request.kw, {'_app_url': ''}) |
MM |
1188 |
|
fb90f0
|
1189 |
|
5c6963
|
1190 |
class Test_static_path(unittest.TestCase): |
CM |
1191 |
def _callFUT(self, path, request, **kw): |
|
1192 |
from pyramid.url import static_path |
0c29cf
|
1193 |
|
5c6963
|
1194 |
return static_path(path, request, **kw) |
CM |
1195 |
|
|
1196 |
def _makeRequest(self): |
|
1197 |
class Request(object): |
|
1198 |
def static_path(self, path, **kw): |
|
1199 |
self.path = path |
|
1200 |
self.kw = kw |
|
1201 |
return 'static path' |
0c29cf
|
1202 |
|
5c6963
|
1203 |
return Request() |
CM |
1204 |
|
b5c0cb
|
1205 |
def test_it_abs(self): |
5c6963
|
1206 |
request = self._makeRequest() |
b5c0cb
|
1207 |
result = self._callFUT('/foo/bar/abc', request, _anchor='anchor') |
5c6963
|
1208 |
self.assertEqual(result, 'static path') |
b5c0cb
|
1209 |
self.assertEqual(request.path, '/foo/bar/abc') |
0c29cf
|
1210 |
self.assertEqual(request.kw, {'_anchor': 'anchor'}) |
5c6963
|
1211 |
|
b5c0cb
|
1212 |
def test_it_absspec(self): |
CM |
1213 |
request = self._makeRequest() |
|
1214 |
result = self._callFUT('foo:abc', request, _anchor='anchor') |
|
1215 |
self.assertEqual(result, 'static path') |
|
1216 |
self.assertEqual(request.path, 'foo:abc') |
0c29cf
|
1217 |
self.assertEqual(request.kw, {'_anchor': 'anchor'}) |
b5c0cb
|
1218 |
|
CM |
1219 |
def test_it_rel(self): |
|
1220 |
request = self._makeRequest() |
|
1221 |
result = self._callFUT('abc', request, _app_url='') |
|
1222 |
self.assertEqual(result, 'static path') |
dd3cc8
|
1223 |
self.assertEqual(request.path, 'tests:abc') |
0c29cf
|
1224 |
self.assertEqual(request.kw, {'_app_url': ''}) |
MM |
1225 |
|
b5c0cb
|
1226 |
|
fb90f0
|
1227 |
class Test_current_route_url(unittest.TestCase): |
CM |
1228 |
def _callFUT(self, request, *elements, **kw): |
|
1229 |
from pyramid.url import current_route_url |
0c29cf
|
1230 |
|
fb90f0
|
1231 |
return current_route_url(request, *elements, **kw) |
CM |
1232 |
|
|
1233 |
def _makeRequest(self): |
|
1234 |
class Request(object): |
|
1235 |
def current_route_url(self, *elements, **kw): |
|
1236 |
self.elements = elements |
|
1237 |
self.kw = kw |
|
1238 |
return 'current route url' |
0c29cf
|
1239 |
|
fb90f0
|
1240 |
return Request() |
CM |
1241 |
|
|
1242 |
def test_it(self): |
|
1243 |
request = self._makeRequest() |
|
1244 |
result = self._callFUT(request, 'abc', _app_url='') |
|
1245 |
self.assertEqual(result, 'current route url') |
|
1246 |
self.assertEqual(request.elements, ('abc',)) |
0c29cf
|
1247 |
self.assertEqual(request.kw, {'_app_url': ''}) |
MM |
1248 |
|
45d08c
|
1249 |
|
12cef0
|
1250 |
class Test_current_route_path(unittest.TestCase): |
CM |
1251 |
def _callFUT(self, request, *elements, **kw): |
|
1252 |
from pyramid.url import current_route_path |
0c29cf
|
1253 |
|
12cef0
|
1254 |
return current_route_path(request, *elements, **kw) |
CM |
1255 |
|
|
1256 |
def _makeRequest(self): |
|
1257 |
class Request(object): |
|
1258 |
def current_route_path(self, *elements, **kw): |
|
1259 |
self.elements = elements |
|
1260 |
self.kw = kw |
|
1261 |
return 'current route path' |
0c29cf
|
1262 |
|
12cef0
|
1263 |
return Request() |
CM |
1264 |
|
|
1265 |
def test_it(self): |
|
1266 |
request = self._makeRequest() |
|
1267 |
result = self._callFUT(request, 'abc', _anchor='abc') |
|
1268 |
self.assertEqual(result, 'current route path') |
|
1269 |
self.assertEqual(request.elements, ('abc',)) |
0c29cf
|
1270 |
self.assertEqual(request.kw, {'_anchor': 'abc'}) |
MM |
1271 |
|
12cef0
|
1272 |
|
33b638
|
1273 |
class Test_external_static_url_integration(unittest.TestCase): |
TL |
1274 |
def setUp(self): |
|
1275 |
self.config = testing.setUp() |
|
1276 |
|
|
1277 |
def tearDown(self): |
|
1278 |
testing.tearDown() |
|
1279 |
|
|
1280 |
def _makeRequest(self): |
84367e
|
1281 |
from pyramid.request import Request |
0c29cf
|
1282 |
|
33b638
|
1283 |
return Request.blank('/') |
TL |
1284 |
|
|
1285 |
def test_generate_external_url(self): |
|
1286 |
self.config.add_route('acme', 'https://acme.org/path/{foo}') |
|
1287 |
request = self._makeRequest() |
|
1288 |
request.registry = self.config.registry |
84367e
|
1289 |
self.assertEqual( |
0c29cf
|
1290 |
request.route_url('acme', foo='bar'), 'https://acme.org/path/bar' |
MM |
1291 |
) |
33b638
|
1292 |
|
TL |
1293 |
def test_generate_external_url_without_scheme(self): |
|
1294 |
self.config.add_route('acme', '//acme.org/path/{foo}') |
|
1295 |
request = self._makeRequest() |
|
1296 |
request.registry = self.config.registry |
84367e
|
1297 |
self.assertEqual( |
0c29cf
|
1298 |
request.route_url('acme', foo='bar'), 'http://acme.org/path/bar' |
MM |
1299 |
) |
33b638
|
1300 |
|
TL |
1301 |
def test_generate_external_url_with_explicit_scheme(self): |
|
1302 |
self.config.add_route('acme', '//acme.org/path/{foo}') |
|
1303 |
request = self._makeRequest() |
|
1304 |
request.registry = self.config.registry |
84367e
|
1305 |
self.assertEqual( |
MM |
1306 |
request.route_url('acme', foo='bar', _scheme='https'), |
0c29cf
|
1307 |
'https://acme.org/path/bar', |
MM |
1308 |
) |
33b638
|
1309 |
|
edbc1d
|
1310 |
def test_generate_external_url_with_explicit_app_url(self): |
TL |
1311 |
self.config.add_route('acme', 'http://acme.org/path/{foo}') |
|
1312 |
request = self._makeRequest() |
|
1313 |
request.registry = self.config.registry |
0c29cf
|
1314 |
self.assertRaises( |
MM |
1315 |
ValueError, |
|
1316 |
request.route_url, |
|
1317 |
'acme', |
|
1318 |
foo='bar', |
|
1319 |
_app_url='http://fakeme.com', |
|
1320 |
) |
edbc1d
|
1321 |
|
df413c
|
1322 |
def test_generate_external_url_route_path(self): |
TL |
1323 |
self.config.add_route('acme', 'https://acme.org/path/{foo}') |
|
1324 |
request = self._makeRequest() |
|
1325 |
request.registry = self.config.registry |
d07d16
|
1326 |
self.assertRaises(ValueError, request.route_path, 'acme', foo='bar') |
df413c
|
1327 |
|
84367e
|
1328 |
def test_generate_external_url_with_pregenerator(self): |
MM |
1329 |
def pregenerator(request, elements, kw): |
|
1330 |
kw['_query'] = {'q': 'foo'} |
|
1331 |
return elements, kw |
0c29cf
|
1332 |
|
MM |
1333 |
self.config.add_route( |
|
1334 |
'acme', 'https://acme.org/path/{foo}', pregenerator=pregenerator |
|
1335 |
) |
84367e
|
1336 |
request = self._makeRequest() |
MM |
1337 |
request.registry = self.config.registry |
|
1338 |
self.assertEqual( |
|
1339 |
request.route_url('acme', foo='bar'), |
0c29cf
|
1340 |
'https://acme.org/path/bar?q=foo', |
MM |
1341 |
) |
84367e
|
1342 |
|
MM |
1343 |
def test_external_url_with_route_prefix(self): |
|
1344 |
def includeme(config): |
|
1345 |
config.add_route('acme', '//acme.org/{foo}') |
0c29cf
|
1346 |
|
84367e
|
1347 |
self.config.include(includeme, route_prefix='some_prefix') |
MM |
1348 |
request = self._makeRequest() |
|
1349 |
request.registry = self.config.registry |
|
1350 |
self.assertEqual( |
0c29cf
|
1351 |
request.route_url('acme', foo='bar'), 'http://acme.org/bar' |
MM |
1352 |
) |
|
1353 |
|
df413c
|
1354 |
|
efd61e
|
1355 |
class Test_with_route_prefix(unittest.TestCase): |
HS |
1356 |
def setUp(self): |
|
1357 |
self.config = testing.setUp() |
|
1358 |
|
|
1359 |
def tearDown(self): |
|
1360 |
testing.tearDown() |
|
1361 |
|
|
1362 |
def _makeRequest(self, route): |
|
1363 |
from pyramid.request import Request |
0c29cf
|
1364 |
|
efd61e
|
1365 |
return Request.blank(route) |
HS |
1366 |
|
|
1367 |
def test_old_route_is_preserved(self): |
|
1368 |
self.config.route_prefix = 'old_prefix' |
|
1369 |
with self.config.route_prefix_context('new_addon'): |
|
1370 |
assert 'new_addon' in self.config.route_prefix |
|
1371 |
|
|
1372 |
assert 'old_prefix' == self.config.route_prefix |
|
1373 |
|
|
1374 |
def test_route_prefix_none(self): |
|
1375 |
self.config.route_prefix = 'old_prefix' |
|
1376 |
with self.config.route_prefix_context(None): |
|
1377 |
assert 'old_prefix' == self.config.route_prefix |
|
1378 |
|
|
1379 |
assert 'old_prefix' == self.config.route_prefix |
|
1380 |
|
|
1381 |
def test_route_prefix_empty(self): |
|
1382 |
self.config.route_prefix = 'old_prefix' |
|
1383 |
with self.config.route_prefix_context(''): |
|
1384 |
assert 'old_prefix' == self.config.route_prefix |
|
1385 |
|
|
1386 |
assert 'old_prefix' == self.config.route_prefix |
|
1387 |
|
|
1388 |
def test_route_has_prefix(self): |
|
1389 |
with self.config.route_prefix_context('bar'): |
|
1390 |
self.config.add_route('acme', '/foo') |
|
1391 |
request = self._makeRequest('/') |
0c29cf
|
1392 |
self.assertEqual(request.route_url('acme'), 'http://localhost/bar/foo') |
efd61e
|
1393 |
|
HS |
1394 |
def test_route_does_not_have_prefix(self): |
|
1395 |
with self.config.route_prefix_context('bar'): |
|
1396 |
pass |
|
1397 |
|
|
1398 |
self.config.add_route('acme', '/foo') |
|
1399 |
request = self._makeRequest('/') |
0c29cf
|
1400 |
self.assertEqual(request.route_url('acme'), 'http://localhost/foo') |
efd61e
|
1401 |
|
HS |
1402 |
def test_error_reset_prefix(self): |
|
1403 |
self.config.route_prefix = 'old_prefix' |
|
1404 |
|
|
1405 |
try: |
|
1406 |
with self.config.route_prefix_context('new_prefix'): |
|
1407 |
raise RuntimeError |
|
1408 |
except RuntimeError: |
|
1409 |
pass |
|
1410 |
|
|
1411 |
assert self.config.route_prefix == 'old_prefix' |
|
1412 |
|
0c29cf
|
1413 |
|
7ae0c2
|
1414 |
class DummyContext(object): |
CM |
1415 |
def __init__(self, next=None): |
|
1416 |
self.next = next |
0c29cf
|
1417 |
|
MM |
1418 |
|
5a11c0
|
1419 |
class DummyRoutesMapper: |
05c023
|
1420 |
raise_exc = None |
0c29cf
|
1421 |
|
70f1cd
|
1422 |
def __init__(self, route=None, raise_exc=False): |
CM |
1423 |
self.route = route |
750ce4
|
1424 |
|
70f1cd
|
1425 |
def get_route(self, route_name): |
CM |
1426 |
return self.route |
|
1427 |
|
0c29cf
|
1428 |
|
70f1cd
|
1429 |
class DummyRoute: |
CM |
1430 |
pregenerator = None |
5653d1
|
1431 |
name = 'route' |
0c29cf
|
1432 |
|
70f1cd
|
1433 |
def __init__(self, result='/1/2/3'): |
CM |
1434 |
self.result = result |
|
1435 |
|
|
1436 |
def generate(self, kw): |
f0b74a
|
1437 |
self.kw = kw |
05c023
|
1438 |
return self.result |
0c29cf
|
1439 |
|
MM |
1440 |
|
b29429
|
1441 |
class DummyStaticURLInfo: |
CM |
1442 |
def __init__(self, result): |
|
1443 |
self.result = result |
|
1444 |
|
|
1445 |
def generate(self, path, request, **kw): |
|
1446 |
self.args = path, request, kw |
|
1447 |
return self.result |
0c29cf
|
1448 |
|
MM |
1449 |
|
b8c797
|
1450 |
def makeabs(*elements): |
0c29cf
|
1451 |
if WIN: # pragma: no cover |
8064bd
|
1452 |
return r'c:\\' + os.path.sep.join(elements) |
CM |
1453 |
else: |
|
1454 |
return os.path.sep + os.path.sep.join(elements) |