PageRenderTime 95ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/pyramid/tests/test_config/test_routes.py

https://github.com/archanmishra/pyramid
Python | 223 lines | 196 code | 27 blank | 0 comment | 13 complexity | 686be359c96907914d50e8ebad6ca473 MD5 | raw file
  1. import unittest
  2. from pyramid.tests.test_config import dummyfactory
  3. from pyramid.tests.test_config import DummyContext
  4. class RoutesConfiguratorMixinTests(unittest.TestCase):
  5. def _makeOne(self, *arg, **kw):
  6. from pyramid.config import Configurator
  7. config = Configurator(*arg, **kw)
  8. return config
  9. def _assertRoute(self, config, name, path, num_predicates=0):
  10. from pyramid.interfaces import IRoutesMapper
  11. mapper = config.registry.getUtility(IRoutesMapper)
  12. routes = mapper.get_routes()
  13. route = routes[0]
  14. self.assertEqual(len(routes), 1)
  15. self.assertEqual(route.name, name)
  16. self.assertEqual(route.path, path)
  17. self.assertEqual(len(routes[0].predicates), num_predicates)
  18. return route
  19. def _makeRequest(self, config):
  20. request = DummyRequest()
  21. request.registry = config.registry
  22. return request
  23. def test_get_routes_mapper_not_yet_registered(self):
  24. config = self._makeOne()
  25. mapper = config.get_routes_mapper()
  26. self.assertEqual(mapper.routelist, [])
  27. def test_get_routes_mapper_already_registered(self):
  28. from pyramid.interfaces import IRoutesMapper
  29. config = self._makeOne()
  30. mapper = object()
  31. config.registry.registerUtility(mapper, IRoutesMapper)
  32. result = config.get_routes_mapper()
  33. self.assertEqual(result, mapper)
  34. def test_add_route_defaults(self):
  35. config = self._makeOne(autocommit=True)
  36. config.add_route('name', 'path')
  37. self._assertRoute(config, 'name', 'path')
  38. def test_add_route_with_route_prefix(self):
  39. config = self._makeOne(autocommit=True)
  40. config.route_prefix = 'root'
  41. config.add_route('name', 'path')
  42. self._assertRoute(config, 'name', 'root/path')
  43. def test_add_route_discriminator(self):
  44. config = self._makeOne()
  45. config.add_route('name', 'path')
  46. self.assertEqual(config.action_state.actions[-1][0], ('route', 'name'))
  47. def test_add_route_with_factory(self):
  48. config = self._makeOne(autocommit=True)
  49. factory = object()
  50. config.add_route('name', 'path', factory=factory)
  51. route = self._assertRoute(config, 'name', 'path')
  52. self.assertEqual(route.factory, factory)
  53. def test_add_route_with_static(self):
  54. config = self._makeOne(autocommit=True)
  55. config.add_route('name', 'path/{foo}', static=True)
  56. mapper = config.get_routes_mapper()
  57. self.assertEqual(len(mapper.get_routes()), 0)
  58. self.assertEqual(mapper.generate('name', {"foo":"a"}), '/path/a')
  59. def test_add_route_with_factory_dottedname(self):
  60. config = self._makeOne(autocommit=True)
  61. config.add_route(
  62. 'name', 'path',
  63. factory='pyramid.tests.test_config.dummyfactory')
  64. route = self._assertRoute(config, 'name', 'path')
  65. self.assertEqual(route.factory, dummyfactory)
  66. def test_add_route_with_xhr(self):
  67. config = self._makeOne(autocommit=True)
  68. config.add_route('name', 'path', xhr=True)
  69. route = self._assertRoute(config, 'name', 'path', 1)
  70. predicate = route.predicates[0]
  71. request = self._makeRequest(config)
  72. request.is_xhr = True
  73. self.assertEqual(predicate(None, request), True)
  74. request = self._makeRequest(config)
  75. request.is_xhr = False
  76. self.assertEqual(predicate(None, request), False)
  77. def test_add_route_with_request_method(self):
  78. config = self._makeOne(autocommit=True)
  79. config.add_route('name', 'path', request_method='GET')
  80. route = self._assertRoute(config, 'name', 'path', 1)
  81. predicate = route.predicates[0]
  82. request = self._makeRequest(config)
  83. request.method = 'GET'
  84. self.assertEqual(predicate(None, request), True)
  85. request = self._makeRequest(config)
  86. request.method = 'POST'
  87. self.assertEqual(predicate(None, request), False)
  88. def test_add_route_with_path_info(self):
  89. config = self._makeOne(autocommit=True)
  90. config.add_route('name', 'path', path_info='/foo')
  91. route = self._assertRoute(config, 'name', 'path', 1)
  92. predicate = route.predicates[0]
  93. request = self._makeRequest(config)
  94. request.path_info = '/foo'
  95. self.assertEqual(predicate(None, request), True)
  96. request = self._makeRequest(config)
  97. request.path_info = '/'
  98. self.assertEqual(predicate(None, request), False)
  99. def test_add_route_with_request_param(self):
  100. config = self._makeOne(autocommit=True)
  101. config.add_route('name', 'path', request_param='abc')
  102. route = self._assertRoute(config, 'name', 'path', 1)
  103. predicate = route.predicates[0]
  104. request = self._makeRequest(config)
  105. request.params = {'abc':'123'}
  106. self.assertEqual(predicate(None, request), True)
  107. request = self._makeRequest(config)
  108. request.params = {}
  109. self.assertEqual(predicate(None, request), False)
  110. def test_add_route_with_custom_predicates(self):
  111. config = self._makeOne(autocommit=True)
  112. def pred1(context, request): pass
  113. def pred2(context, request): pass
  114. config.add_route('name', 'path', custom_predicates=(pred1, pred2))
  115. route = self._assertRoute(config, 'name', 'path', 2)
  116. self.assertEqual(route.predicates, [pred1, pred2])
  117. def test_add_route_with_header(self):
  118. config = self._makeOne(autocommit=True)
  119. config.add_route('name', 'path', header='Host')
  120. route = self._assertRoute(config, 'name', 'path', 1)
  121. predicate = route.predicates[0]
  122. request = self._makeRequest(config)
  123. request.headers = {'Host':'example.com'}
  124. self.assertEqual(predicate(None, request), True)
  125. request = self._makeRequest(config)
  126. request.headers = {}
  127. self.assertEqual(predicate(None, request), False)
  128. def test_add_route_with_accept(self):
  129. config = self._makeOne(autocommit=True)
  130. config.add_route('name', 'path', accept='text/xml')
  131. route = self._assertRoute(config, 'name', 'path', 1)
  132. predicate = route.predicates[0]
  133. request = self._makeRequest(config)
  134. request.accept = ['text/xml']
  135. self.assertEqual(predicate(None, request), True)
  136. request = self._makeRequest(config)
  137. request.accept = ['text/html']
  138. self.assertEqual(predicate(None, request), False)
  139. def test_add_route_no_pattern_with_path(self):
  140. config = self._makeOne(autocommit=True)
  141. config.add_route('name', path='path')
  142. self._assertRoute(config, 'name', 'path')
  143. def test_add_route_no_path_no_pattern(self):
  144. from pyramid.exceptions import ConfigurationError
  145. config = self._makeOne()
  146. self.assertRaises(ConfigurationError, config.add_route, 'name')
  147. def test_add_route_with_pregenerator(self):
  148. config = self._makeOne(autocommit=True)
  149. config.add_route('name', 'pattern', pregenerator='123')
  150. route = self._assertRoute(config, 'name', 'pattern')
  151. self.assertEqual(route.pregenerator, '123')
  152. def test_add_route_no_view_with_view_attr(self):
  153. config = self._makeOne(autocommit=True)
  154. from pyramid.exceptions import ConfigurationError
  155. try:
  156. config.add_route('name', '/pattern', view_attr='abc')
  157. except ConfigurationError:
  158. pass
  159. else: # pragma: no cover
  160. raise AssertionError
  161. def test_add_route_no_view_with_view_context(self):
  162. config = self._makeOne(autocommit=True)
  163. from pyramid.exceptions import ConfigurationError
  164. try:
  165. config.add_route('name', '/pattern', view_context=DummyContext)
  166. except ConfigurationError:
  167. pass
  168. else: # pragma: no cover
  169. raise AssertionError
  170. def test_add_route_no_view_with_view_permission(self):
  171. config = self._makeOne(autocommit=True)
  172. from pyramid.exceptions import ConfigurationError
  173. try:
  174. config.add_route('name', '/pattern', view_permission='edit')
  175. except ConfigurationError:
  176. pass
  177. else: # pragma: no cover
  178. raise AssertionError
  179. def test_add_route_no_view_with_view_renderer(self):
  180. config = self._makeOne(autocommit=True)
  181. from pyramid.exceptions import ConfigurationError
  182. try:
  183. config.add_route('name', '/pattern', view_renderer='json')
  184. except ConfigurationError:
  185. pass
  186. else: # pragma: no cover
  187. raise AssertionError
  188. class DummyRequest:
  189. subpath = ()
  190. matchdict = None
  191. def __init__(self, environ=None):
  192. if environ is None:
  193. environ = {}
  194. self.environ = environ
  195. self.params = {}
  196. self.cookies = {}