def test_process_template_resnpose(self): from django.template.response import TemplateResponse from urlmiddleware import URLMiddleware request = self.factory.get('/foo/bar/') template_response = TemplateResponse(request, 'base.html') new = TemplateResponse(request, 'base.html') class MyTemplateResnponseViewMiddleware(object): def process_template_response(self, request, response): return new def mock_match(self, path, middleware_method=None): return [ MyTemplateResnponseViewMiddleware(), ] with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals( m.process_template_response(request, template_response), new)
def test_process_response(self): from django.http import HttpResponse from urlmiddleware import URLMiddleware class MyTemplateResnponseViewMiddleware(object): def process_response(self, request, response): from django.http import HttpResponse return HttpResponse("New Response") def mock_match(self, path, middleware_method=None): return [ MyTemplateResnponseViewMiddleware(), ] request = self.factory.get('/foo/bar/') response = HttpResponse("Response") with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() expected_content = "New Response" self.assertEquals( m.process_response(request, response).content, expected_content)
def test_no_middleware_url(self): from urlmiddleware import URLMiddleware m = URLMiddleware() middleware = m.get_matched_middleware("/no_middleware/") self.assertEquals(middleware, [])
def test_getting_matched(self): from urlmiddleware import URLMiddleware from test_urlmiddleware.middleware import NoOpMiddleWare m = URLMiddleware() middleware = m.get_matched_middleware("/") self.assertEquals(middleware[0].__class__, NoOpMiddleWare)
def test_match_cache(self): from urlmiddleware.middleware import _match_cache, URLMiddleware from test_urlmiddleware.middleware import NoOpMiddleWare keys = _match_cache.keys()[:] + [('/',), ] m = URLMiddleware() m.get_matched_middleware('/') self.assertEqual(_match_cache.keys(), keys) self.assertEqual(_match_cache[('/',)], [NoOpMiddleWare, ])
def test_match_cache(self): from urlmiddleware.middleware import _match_cache, URLMiddleware from test_urlmiddleware.middleware import NoOpMiddleWare keys = _match_cache.keys()[:] + [ ('/', ), ] m = URLMiddleware() m.get_matched_middleware('/') self.assertEqual(_match_cache.keys(), keys) self.assertEqual(_match_cache[('/', )], [ NoOpMiddleWare, ])
def test_process_request_no_op(self): from urlmiddleware import URLMiddleware class MyProcessRequestMiddleware(object): def process_request(self, request, *args, **kwargs): pass def mock_match(self, path, middleware_method=None): return [MyProcessRequestMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals(m.process_request(request), None)
def test_process_view_no_op(self): from urlmiddleware import URLMiddleware class MyProcessViewMiddleware(object): def process_view(self, request, view, view_args, view_kwargs): pass def mock_match(self, path, middleware_method=None): return [MyProcessViewMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals(m.process_view(request, lambda x: None, [], {}), None)
def test_process_request_no_op(self): from urlmiddleware import URLMiddleware class MyProcessRequestMiddleware(object): def process_request(self, request, *args, **kwargs): pass def mock_match(self, path, middleware_method=None): return [ MyProcessRequestMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals(m.process_request(request), None)
def test_process_exception_no_op(self): from urlmiddleware import URLMiddleware class MyTemplateResnponseViewMiddleware(object): def process_exception(self, request, exception): pass def mock_match(self, path, middleware_method=None): return [MyTemplateResnponseViewMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() e = Exception("Uh oh.") self.assertEquals(m.process_exception(request, e), None)
def test_process_view(self): from urlmiddleware import URLMiddleware class MyProcessViewMiddleware(object): def process_view(self, request, view, view_args, view_kwargs): from django.http import HttpResponse return HttpResponse("New Process View Response") def mock_match(self, path, middleware_method=None): return [MyProcessViewMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() expected_content = "New Process View Response" self.assertEquals(m.process_view(request, lambda x: None, [], {}).content, expected_content)
def test_process_exception_no_op(self): from urlmiddleware import URLMiddleware class MyTemplateResnponseViewMiddleware(object): def process_exception(self, request, exception): pass def mock_match(self, path, middleware_method=None): return [ MyTemplateResnponseViewMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() e = Exception("Uh oh.") self.assertEquals(m.process_exception(request, e), None)
def test_process_view_no_op(self): from urlmiddleware import URLMiddleware class MyProcessViewMiddleware(object): def process_view(self, request, view, view_args, view_kwargs): pass def mock_match(self, path, middleware_method=None): return [ MyProcessViewMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals(m.process_view(request, lambda x: None, [], {}), None)
def test_process_response_no_op(self): from django.http import HttpResponse from urlmiddleware import URLMiddleware class MyTemplateResnponseViewMiddleware(object): def process_response(self, request, response): return response def mock_match(self, path, middleware_method=None): return [MyTemplateResnponseViewMiddleware(), ] request = self.factory.get('/foo/bar/') response = HttpResponse("Response") with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals(m.process_response(request, response), response)
def test_process_exception(self): from urlmiddleware import URLMiddleware class MyTemplateResnponseViewMiddleware(object): def process_exception(self, request, exception): from django.http import HttpResponse return HttpResponse("New Response") def mock_match(self, path, middleware_method=None): return [MyTemplateResnponseViewMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() e = Exception("Uh oh.") expected_content = "New Response" self.assertEquals(m.process_exception(request, e).content, expected_content)
def test_process_template_resnpose(self): from django.template.response import TemplateResponse from urlmiddleware import URLMiddleware request = self.factory.get('/foo/bar/') template_response = TemplateResponse(request, 'base.html') new = TemplateResponse(request, 'base.html') class MyTemplateResnponseViewMiddleware(object): def process_template_response(self, request, response): return new def mock_match(self, path, middleware_method=None): return [MyTemplateResnponseViewMiddleware(), ] with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() self.assertEquals(m.process_template_response(request, template_response), new)
def test_process_request(self): from urlmiddleware import URLMiddleware class MyProcessRequestMiddleware(object): def process_request(self, request, *args, **kwargs): from django.http import HttpResponse return HttpResponse("New Process Request Response") def mock_match(self, path, middleware_method=None): return [ MyProcessRequestMiddleware(), ] request = self.factory.get('/foo/bar/') with patch.object(URLMiddleware, 'get_matched_middleware', mock_match): m = URLMiddleware() expected_content = "New Process Request Response" self.assertEquals( m.process_request(request).content, expected_content)
def test_middleware(self): from urlmiddleware import URLMiddleware URLMiddleware()