def test_named_url_pattern(self): "Named pattern parameter should reverse to the matching pattern" self.config.add_route('artist_detail', 'detail/artist/{pk}', view=RedirectView.as_view()) response = RedirectView.as_view(pattern_name='artist_detail')( DummyRequest(path='/foo/', method='GET'), pk=1 ) self.assertEqual(response.status_code, 301) self.assertEqual(response.headers['Location'], 'http://example.com/detail/artist/1')
def test_include_args(self): "GET arguments can be included in the redirected URL" response = RedirectView.as_view(url='/bar/')( DummyRequest(path='/foo/', method='GET') ) self.assertEqual(response.status_code, 301) self.assertEqual(response.location, '/bar/') response = RedirectView.as_view(url='/bar/', query_string=True)( DummyRequest(path='/foo/', method='GET', query_string='pork=spam') ) self.assertEqual(response.status_code, 301) self.assertEqual(response.location, '/bar/?pork=spam')
def test_include_urlencoded_args(self): "GET arguments can be URL-encoded when included in the redirected URL" response = RedirectView.as_view(url='/bar/', query_string=True)( DummyRequest(path='/foo/', method='GET', query_string='unicode=%E2%9C%93') ) self.assertEqual(response.status_code, 301) self.assertEqual(response.location, '/bar/?unicode=%E2%9C%93')
def test_temporary_redirect(self): "Permanent redirects are an option" response = RedirectView.as_view(url='/bar/', permanent=False)( DummyRequest(path='/foo/', method='GET') ) self.assertEqual(response.status_code, 302) self.assertEqual(response.location, '/bar/')
def test_permanent_redirect(self): "Default is a permanent redirect" response = RedirectView.as_view(url='/bar/')( DummyRequest(path='/foo/', method='GET') ) self.assertEqual(response.status_code, 301) self.assertEqual(response.location, '/bar/')
def test_parameter_substitution(self): "Redirection URLs can be parameterized" response = RedirectView.as_view(url='/bar/%(object_id)d/')( DummyRequest(path='/foo/42/', method='GET'), object_id=42 ) self.assertEqual(response.status_code, 301) self.assertEqual(response.location, '/bar/42/')
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ engine = engine_from_config(settings, 'sqlalchemy.') Session.configure(bind=engine) Base.metadata.bind = engine configure_views(Session) config = Configurator(settings=settings) config.add_static_view('static', 'static', cache_max_age=3600) config.add_route('home', '/', views.HomeView.as_view()) config.add_route('about', '/about', TemplateView.as_view(template_name='example_project:templates/about.pt')) config.add_route('book_list_redirect', '/book', RedirectView.as_view(url='/book/list', permanent=True)) config.add_route('book_list', '/book/list', views.BookListView.as_view()) config.add_route('book_update', '/book/update/{pk}', views.BookUpdateView.as_view()) config.add_route('book_delete', '/book/delete/{pk}', views.BookDeleteView.as_view()) config.add_route('book_create', '/book/create', views.BookCreateView.as_view()) return config.make_wsgi_app()
def test_wrong_named_url_pattern(self): "A wrong pattern name returns 410 GONE" response = RedirectView.as_view(pattern_name='wrong.pattern_name')( DummyRequest(path='/foo/', method='GET') ) self.assertEqual(response.status_code, 410)
def test_named_url_pattern_using_args(self): self.config.add_route('artist_detail', 'detail/artist/*traverse', view=RedirectView.as_view()) response = RedirectView.as_view(pattern_name='artist_detail')(DummyRequest(path='/foo/', method='GET'), traverse=(1,2)) self.assertEqual(response.status_code, 301) self.assertEqual(response.headers['Location'], 'http://example.com/detail/artist/1/2')
def test_no_url(self): "Without any configuration, returns HTTP 410 GONE" response = RedirectView.as_view()( DummyRequest(path='/foo/', method='GET') ) self.assertEqual(response.status_code, 410)