Skip to content

Commit

Permalink
Merge pull request #338 from jsmolar/rate_limit_auth
Browse files Browse the repository at this point in the history
Add RLP targetting tests
  • Loading branch information
pehala authored Mar 4, 2024
2 parents 591c2a1 + 2255799 commit 5c71f7e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 17 deletions.
Empty file.
20 changes: 20 additions & 0 deletions testsuite/tests/kuadrant/limitador/route/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Conftest for RLP targeting route tests """

import pytest

from testsuite.gateway import PathMatch, RouteMatch, MatchType


@pytest.fixture(scope="module")
def route(route, backend):
"""Add two new rules to the route"""
route.remove_all_rules()
route.add_rule(
backend,
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
)
route.add_rule(
backend,
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)),
)
return route
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests that one RLP limit targeting two rules limits them together"""

import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType
from testsuite.policy.rate_limit_policy import RouteSelector, Limit


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)),
)
rate_limit.add_limit("test", [Limit(5, 10)], route_selectors=[selector])
return rate_limit


def test_limit_targeting_two_rules(client):
"""Tests that one RLP limit targeting two rules limits them together"""
responses = client.get_many("/get", 3)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"

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

assert client.get("/get").status_code == 429
assert client.get("/anything").status_code == 429
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test that multiple limits targeting same rule are correctly applied"""

import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType
from testsuite.policy.rate_limit_policy import RouteSelector, Limit


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
)
rate_limit.add_limit("test1", [Limit(8, 10)], route_selectors=[selector])
rate_limit.add_limit("test2", [Limit(3, 5)], route_selectors=[selector])
return rate_limit


def test_two_rules_targeting_one_limit(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
assert client.get("/get").status_code == 429
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,18 @@
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def route(route, backend):
"""Add two new rules to the route"""
route.remove_all_rules()
route.add_rule(
backend,
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
)
route.add_rule(
backend,
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)),
)
return route


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)))
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
RouteMatch(path=PathMatch(value="/anything/test", type=MatchType.PATH_PREFIX)),
)
rate_limit.add_limit("multiple", [Limit(5, 10)], route_selectors=[selector])
return rate_limit


def test_rule(client):
def test_rule_match(client):
"""Tests that RLP correctly applies to the given HTTPRoute rule"""
responses = client.get_many("/get", 5)
responses.assert_all(status_code=200)
Expand Down

0 comments on commit 5c71f7e

Please sign in to comment.