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

RateLimitingMiddleware tests failed #267

Closed
sinisaos opened this issue Feb 22, 2024 · 5 comments · Fixed by #268
Closed

RateLimitingMiddleware tests failed #267

sinisaos opened this issue Feb 22, 2024 · 5 comments · Fixed by #268

Comments

@sinisaos
Copy link
Member

Scope client is set to None in Starlette>=0.35.0 testclient. Due to this change, the test for RateLimitingMiddleware failed in Piccolo API and Piccolo Admin. We can fix it by changing rate limiter middleware dispatch method like this

async def dispatch(
        self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
    request_client = request.url.netloc
    if not request_client:
        # If we can't get the client, we have to reject the request.
        return Response(
            content="Client host can't be found.", status_code=400
        )

    identifier = request_client
    try:
        self.rate_limit.increment(identifier)
    except RateLimitError:
        return Response(content="Too many requests", status_code=429)
    return await call_next(request)

or based on this discussion you can change the testing method. I leave this decision to you.

@dantownsend
Copy link
Member

@sinisaos Interesting - so it just effects the tests? If it broke RateLimitingMiddleware that would be bad, as it's used by Piccolo Admin.

@dantownsend
Copy link
Member

dantownsend commented Feb 22, 2024

I think I'd rather fix the test somehow, as the problem is related to the changes to TestClient.

I sometimes use the httpx.Client directly instead of TestClient - maybe it doesn't have the same problem.

@dantownsend
Copy link
Member

@sinisaos Thanks for reporting this. I have a fix for now.

@sinisaos
Copy link
Member Author

@dantownsend I don't think all these changes to the Piccolo API and Admin were necessary because if the Starlette testclient reverts back to provide scope client (there is a PR for that), all the tests will pass again. Apologies if I wasn't clear enough in my previous comment, but the rate limit is only broken in tests, not in real use. With the changes I suggested in the comment, there was no need to change the code (except the dispatch method) in the Piccolo API and Piccolo Admin, regardless of whether Starlette revert client scope to testclient or leaves it to None as it is now. But it doesn't matter because you already make all these changes in both repositories and they work.

@dantownsend
Copy link
Member

@sinisaos Yeah, I saw the PR in the Starlette repo reverting it. The problem though is even if it gets merged today, FastAPI lags behind the latest version of Starlette, so we'd have to wait a while before our tests pass again.

I think the only solutions for an immediate fix are version pinning FastAPI to an older version (before Starlette changed how the TestClient works), or use httpx directly. I didn't want to use the httpx approach in Piccolo Admin, because loads of the tests were failing, so just version pinned FastAPI in the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants