Add endpoints to cost/hit tests

This commit is contained in:
Joris Hartog
2022-08-29 22:03:04 +02:00
parent 875c8758c1
commit 5bb0edbd01
2 changed files with 50 additions and 0 deletions

View File

@@ -300,11 +300,19 @@ class TestDecorators(TestSlowapi):
async def t1(request: Request):
return PlainTextResponse("test")
@app.get("/t2")
@limiter.limit("50/minute", cost=15)
async def t2(request: Request):
return PlainTextResponse("test")
client = TestClient(app)
for i in range(0, 10):
response = client.get("/t1")
assert response.status_code == 200 if i < 5 else 429
response = client.get("/t2")
assert response.status_code == 200 if i < 3 else 429
def test_callable_cost(self):
app, limiter = self.build_fastapi_app(key_func=get_ipaddr)
@@ -313,7 +321,17 @@ class TestDecorators(TestSlowapi):
async def t1(request: Request):
return PlainTextResponse("test")
@app.get("/t2")
@limiter.limit(
"50/minute", cost=lambda request: int(request.headers["foo"]) * 1.5
)
async def t2(request: Request):
return PlainTextResponse("test")
client = TestClient(app)
for i in range(0, 10):
response = client.get("/t1", headers={"foo": "10"})
assert response.status_code == 200 if i < 5 else 429
response = client.get("/t2", headers={"foo": "5"})
assert response.status_code == 200 if i < 6 else 429

View File

@@ -269,12 +269,27 @@ class TestDecorators(TestSlowapi):
app.add_route("/t1", t1)
@limiter.limit("50/minute", cost=15)
async def t2(request: Request):
return PlainTextResponse("test")
app.add_route("/t2", t2)
client = TestClient(app)
for i in range(0, 10):
response = client.get("/t1")
assert response.status_code == 200 if i < 5 else 429
if i < 5:
assert response.text == "test"
else:
assert "error" in response.json()
response = client.get("/t2")
assert response.status_code == 200 if i < 3 else 429
if i < 3:
assert response.text == "test"
else:
assert "error" in response.json()
def test_callable_cost(self):
app, limiter = self.build_starlette_app(key_func=get_ipaddr)
@@ -285,9 +300,26 @@ class TestDecorators(TestSlowapi):
app.add_route("/t1", t1)
@limiter.limit(
"50/minute", cost=lambda request: int(request.headers["foo"]) * 1.5
)
async def t2(request: Request):
return PlainTextResponse("test")
app.add_route("/t2", t2)
client = TestClient(app)
for i in range(0, 10):
response = client.get("/t1", headers={"foo": "10"})
assert response.status_code == 200 if i < 5 else 429
if i < 5:
assert response.text == "test"
else:
assert "error" in response.json()
response = client.get("/t2", headers={"foo": "5"})
assert response.status_code == 200 if i < 6 else 429
if i < 6:
assert response.text == "test"
else:
assert "error" in response.json()