Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rate limiting test #268

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions piccolo_api/openapi/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ class OAuthRedirectEndpoint(HTTPEndpoint):
def get(self, request: Request):
return get_swagger_ui_oauth2_redirect_html()

router.add_route("/", endpoint=DocsEndpoint)
router.add_route("/oauth2-redirect/", endpoint=OAuthRedirectEndpoint)
router.add_route("/", endpoint=DocsEndpoint) # type: ignore
router.add_route(
"/oauth2-redirect/",
endpoint=OAuthRedirectEndpoint, # type: ignore
)

return router
42 changes: 27 additions & 15 deletions tests/rate_limiting/test_rate_middleware.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
from time import sleep
from unittest import TestCase

from httpx import ASGITransport, AsyncClient
from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from starlette.routing import Route, Router
from starlette.testclient import TestClient

from piccolo_api.rate_limiting.middleware import (
InMemoryLimitProvider,
Expand All @@ -27,23 +28,34 @@ def test_limit(self):
InMemoryLimitProvider(limit=5, timespan=1, block_duration=1),
)

client = TestClient(app)
# We have to use `httpx.AsyncClient` directly, because `TestClient`
# was broken in this PR:
# https://github.com/encode/starlette/pull/2377
# `TestClient` no longer sends the client IP and port.
# If a fix is released, we can go back to using `TestClient` directly.
client = AsyncClient(
transport=ASGITransport(app=app),
base_url="http://testserver",
)

async def run_test():
successful = 0
for _ in range(20):
response = await client.get("/")
if response.status_code == 429:
break
else:
successful += 1

successful = 0
for i in range(20):
response = client.get("/")
if response.status_code == 429:
break
else:
successful += 1
self.assertEqual(successful, 5)

self.assertEqual(successful, 5)
# After the 'block_duration' has expired, requests should be
# allowed again.
sleep(1.1)
response = await client.get("/")
self.assertEqual(response.status_code, 200)

# After the 'block_duration' has expired, requests should be allowed
# again.
sleep(1.1)
response = client.get("/")
self.assertEqual(response.status_code, 200)
asyncio.run(run_test())

def test_memory_usage(self):
"""
Expand Down
Loading