From 5d705bf01dac2fc5687992db8bdab833ccc00f0b Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 11 Feb 2025 16:19:53 -0700 Subject: [PATCH] Add a test for cache --- tests/test_http.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_http.py b/tests/test_http.py index 7bc7f4b254e7..cdf89241f1d1 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -78,6 +78,48 @@ async def test_get(self): self.assertEqual(result.status_code, 200) self.assertEqual(result.json(), {"message": "Hello, world!"}) + async def test_get_with_cache(self): + async with http.HttpClient(100) as client: + example_request = ( + 'GET', + self.base_url, + '/test-get-cache', + ) + url = f"{example_request[1]}{example_request[2]}" + + # Register handler that returns different data each time + counter = 0 + def handler(_handler, _request): + nonlocal counter + counter += 1 + return ( + json.dumps({"count": counter}), + 200, + {"Content-Type": "application/json", "Cache-Control": "max-age=3600"}, + ) + + self.mock_server.register_route_handler(*example_request)(handler) + + # First request should get count=1 + result = await client.get(url, cache=True) + self.assertEqual(result.status_code, 200) + self.assertEqual(result.json(), {"count": 1}) + + # Second request with cache should get same response + result = await client.get(url, cache=True) + self.assertEqual(result.status_code, 200) + self.assertEqual(result.json(), {"count": 1}) + + # Request without cache should get new response + result = await client.get(url, cache=False) + self.assertEqual(result.status_code, 200) + self.assertEqual(result.json(), {"count": 2}) + + # Re-using cache will get the old response + result = await client.get(url, cache=True) + self.assertEqual(result.status_code, 200) + self.assertEqual(result.json(), {"count": 1}) + async def test_post(self): async with http.HttpClient(100) as client: example_request = (