#🔒 fastapi-limiter

7 messages · Page 1 of 1 (latest)

hazy pumice
#

I am trying to implement rate limiter for my FastAPI project, but the dependencies make it hard for me to mock the limiter so my tests that I have already written do not fail. I simply want to mock the limiter for tests so the tests do not care about a limiter being there. As you can see in the code, I used fastapi-limiter 0.2.0. So my question is if there is someone that has dealt with similar issue and has an answer.

Code:
https://pastebin.com/4eu2JyhM

thorn fractalBOT
#

@hazy pumice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ruby sky
#

I haven't ever tried to mock a ratelimiter with pytest/fastapi, so take this with a grain of salt: I would suggest writing a test that actually starts and calls the service to test the rate limiter that way.

hazy pumice
#

I am not sure if we understand each other, I will eventually add test for my limiter, but now I want my older tests to work (they send more requests than I want and the limiter catches it) so I need to mock the limiter, I guess. I am not sure either. I tried multiple approaches, but because of the order of the packages that are being loaded the fastapi has reference to the limiter instance before I even mock the method that returns the limiter to the fastapi.

ruby sky
#

oh, I see. Yeah I'll have to think about that

thorn fractalBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.