mirror of
https://github.com/laurentS/slowapi.git
synced 2026-03-13 09:10:20 +08:00
chg: ✅ minor change in testing
This commit is contained in:
@@ -337,16 +337,10 @@ class TestDecorators(TestSlowapi):
|
||||
assert response.status_code == 200 if i < 6 else 429
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"key_style, expected_key",
|
||||
[
|
||||
("url", "LIMITER/mock//t1/param_one/1/1/minute"),
|
||||
(
|
||||
"endpoint",
|
||||
"LIMITER/mock/tests.test_fastapi_extension.t1_func/1/1/minute",
|
||||
),
|
||||
],
|
||||
"key_style",
|
||||
["url", "endpoint"],
|
||||
)
|
||||
def test_key_style(self, build_fastapi_app, key_style, expected_key):
|
||||
def test_key_style(self, build_fastapi_app, key_style):
|
||||
app, limiter = build_fastapi_app(key_func=lambda: "mock", key_style=key_style)
|
||||
|
||||
@app.get("/t1/{my_param}")
|
||||
@@ -361,12 +355,17 @@ class TestDecorators(TestSlowapi):
|
||||
# meaning it should not raise any RateLimitExceeded error.
|
||||
if key_style == "url":
|
||||
assert second_call.status_code == 200
|
||||
# also assert that we counted only one request on the expected key
|
||||
assert limiter._storage.get(expected_key) == 1
|
||||
assert limiter._storage.get("LIMITER/mock//t1/param_one/1/1/minute") == 1
|
||||
assert limiter._storage.get("LIMITER/mock//t1/param_two/1/1/minute") == 1
|
||||
# However, with the `endpoint` key_style, it will use the function name (e.g: "t1_func")
|
||||
# meaning it will raise a RateLimitExceeded error, because no matter the parameter value
|
||||
# it will share the limitations.
|
||||
elif key_style == "endpoint":
|
||||
assert second_call.status_code == 429
|
||||
# check that we counted 2 requests, even though we had a different value for "my_param"
|
||||
assert limiter._storage.get(expected_key) == 2
|
||||
assert (
|
||||
limiter._storage.get(
|
||||
"LIMITER/mock/tests.test_fastapi_extension.t1_func/1/1/minute"
|
||||
)
|
||||
== 2
|
||||
)
|
||||
|
||||
@@ -324,16 +324,10 @@ class TestDecorators(TestSlowapi):
|
||||
assert "error" in response.json()
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"key_style, expected_key",
|
||||
[
|
||||
("url", "LIMITER/mock//t1/param_one/1/1/minute"),
|
||||
(
|
||||
"endpoint",
|
||||
"LIMITER/mock/tests.test_starlette_extension.t1_func/1/1/minute",
|
||||
),
|
||||
],
|
||||
"key_style",
|
||||
["url", "endpoint"],
|
||||
)
|
||||
def test_key_style(self, build_starlette_app, key_style, expected_key):
|
||||
def test_key_style(self, build_starlette_app, key_style):
|
||||
app, limiter = build_starlette_app(key_func=lambda: "mock", key_style=key_style)
|
||||
|
||||
@limiter.limit("1/minute")
|
||||
@@ -349,12 +343,17 @@ class TestDecorators(TestSlowapi):
|
||||
# meaning it should not raise any RateLimitExceeded error.
|
||||
if key_style == "url":
|
||||
assert second_call.status_code == 200
|
||||
# also assert that we counted only one request on the expected key
|
||||
assert limiter._storage.get(expected_key) == 1
|
||||
assert limiter._storage.get("LIMITER/mock//t1/param_one/1/1/minute") == 1
|
||||
assert limiter._storage.get("LIMITER/mock//t1/param_two/1/1/minute") == 1
|
||||
# However, with the `endpoint` key_style, it will use the function name (e.g: "t1_func")
|
||||
# meaning it will raise a RateLimitExceeded error, because no matter the parameter value
|
||||
# it will share the limitations.
|
||||
elif key_style == "endpoint":
|
||||
assert second_call.status_code == 429
|
||||
# check that we counted 2 requests, even though we had a different value for "my_param"
|
||||
assert limiter._storage.get(expected_key) == 2
|
||||
assert (
|
||||
limiter._storage.get(
|
||||
"LIMITER/mock/tests.test_starlette_extension.t1_func/1/1/minute"
|
||||
)
|
||||
== 2
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user