Skip to content

Commit

Permalink
Add a test for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed Feb 11, 2025
1 parent 238f356 commit 0184a40
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 0184a40

Please sign in to comment.