Skip to content

Commit

Permalink
Merge pull request #343 from jsmolar/request_assert
Browse files Browse the repository at this point in the history
Refactor many request assert for limit tests
  • Loading branch information
pehala authored Feb 13, 2024
2 parents fbe3d4e + 2b51986 commit f85709c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
16 changes: 14 additions & 2 deletions testsuite/httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def __str__(self):
return f"Result[error={self.error}]"


class ResultList(list):
"""List-like object for Result"""

def assert_all(self, status_code):
"""Assert all responses that contain certain status code"""
for request in self:
assert request.status_code == status_code, (
f"Status code assertion failed for request {self.index(request)+1} out of {len(self)} requests: "
f"{request} != {status_code}"
)


class KuadrantClient(Client):
"""Httpx client which retries unstable requests"""

Expand Down Expand Up @@ -139,9 +151,9 @@ def request(
def get(self, *args, **kwargs) -> Result:
return super().get(*args, **kwargs)

def get_many(self, url, count, *, params=None, headers=None, auth=None) -> list[Result]:
def get_many(self, url, count, *, params=None, headers=None, auth=None) -> ResultList:
"""Send multiple `GET` requests."""
responses = []
responses = ResultList()
for _ in range(count):
responses.append(self.get(url, params=params, headers=headers, auth=auth))

Expand Down
4 changes: 1 addition & 3 deletions testsuite/tests/kuadrant/limitador/test_basic_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,5 @@ def rate_limit(rate_limit, limit):
def test_limit(client, limit):
"""Tests that simple limit is applied successfully"""
responses = client.get_many("/get", limit.limit)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def test_multiple_iterations(client):
"""Tests that simple limit is applied successfully and works for multiple iterations"""
for _ in range(10):
responses = client.get_many("/get", 5)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429
sleep(10)
12 changes: 9 additions & 3 deletions testsuite/tests/kuadrant/limitador/test_route_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ def rate_limit(rate_limit):
def test_rule(client):
"""Tests that RLP correctly applies to the given HTTPRoute rule"""
responses = client.get_many("/get", 5)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
responses.assert_all(status_code=200)

assert client.get("/get").status_code == 429

response = client.get("/anything")
assert response.status_code == 200


def test_rule_missmatch(client):
"""Tests that RLP is not applied for not existing route rule"""
responses = client.get_many("/anything/test", 7)
responses.assert_all(status_code=200)
10 changes: 4 additions & 6 deletions testsuite/tests/kuadrant/test_rate_limit_anonymous.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,15 @@ def auth(oidc_provider):
def test_no_limit_for_auth_user(client, auth):
"""Test that no limit is not applied for authenticated user"""
responses = client.get_many("/get", 7, auth=auth)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
responses.assert_all(status_code=200)


def test_anonymous_identity(client, auth):
"""Test that an anonymous requests are correctly limited"""
assert client.get("/get", auth=auth).status_code == 200

responses = client.get_many("/get", 5)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
responses.assert_all(status_code=200)

assert client.get("/get").status_code == 429
assert client.get("/get", auth=auth).status_code == 200
5 changes: 2 additions & 3 deletions testsuite/tests/kuadrant/test_rate_limit_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def auth2(rhsso, blame):
def test_authz_limit(client, auth, auth2):
"""Tests that rate limit is applied for two users independently"""
responses = client.get_many("/get", 5, auth=auth)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
responses.assert_all(status_code=200)

assert client.get("/get", auth=auth).status_code == 429
assert client.get("/get", auth=auth2).status_code == 200

0 comments on commit f85709c

Please sign in to comment.