MCPcopy Index your code
hub / github.com/django/django / test_middleware

Method test_middleware

tests/cache/tests.py:2713–2741  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2711 self.assertEqual(middleware.cache, self.other_cache)
2712
2713 def test_middleware(self):
2714 middleware = CacheMiddleware(hello_world_view)
2715 prefix_middleware = CacheMiddleware(hello_world_view, key_prefix="prefix1")
2716 timeout_middleware = CacheMiddleware(hello_world_view, cache_timeout=1)
2717
2718 request = self.factory.get("/view/")
2719
2720 # Put the request through the request middleware
2721 result = middleware.process_request(request)
2722 self.assertIsNone(result)
2723
2724 response = hello_world_view(request, "1")
2725
2726 # Now put the response through the response middleware
2727 response = middleware.process_response(request, response)
2728
2729 # Repeating the request should result in a cache hit
2730 result = middleware.process_request(request)
2731 self.assertIsNotNone(result)
2732 self.assertEqual(result.content, b"Hello World 1")
2733
2734 # The same request through a different middleware won't hit
2735 result = prefix_middleware.process_request(request)
2736 self.assertIsNone(result)
2737
2738 # The same request with a timeout _will_ hit
2739 result = timeout_middleware.process_request(request)
2740 self.assertIsNotNone(result)
2741 self.assertEqual(result.content, b"Hello World 1")
2742
2743 def test_view_decorator(self):
2744 # decorate the same view with different cache decorators

Callers

nothing calls this directly

Calls 5

CacheMiddlewareClass · 0.90
hello_world_viewFunction · 0.85
getMethod · 0.45
process_requestMethod · 0.45
process_responseMethod · 0.45

Tested by

no test coverage detected