Skip to content

Commit

Permalink
Added tests to guard against RLP applying to HTTPRouteRule with diffe…
Browse files Browse the repository at this point in the history
…rent method
  • Loading branch information
martinhesko committed Feb 28, 2024
1 parent 2540380 commit f106fe2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 40 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests that RLP for a subset of HTTPRouteRule limits all the paths in the HTTPRouteRule"""
"""Tests that RLP for a HTTPRouteRule doesn't affect the HTTPRoute with same path but different method"""

import pytest

Expand All @@ -13,6 +13,9 @@ def route(route, backend):
route.add_rule(
backend,
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX), method=HTTPMethod.GET),
)
route.add_rule(
backend,
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX), method=HTTPMethod.POST),
)
return route
Expand All @@ -28,11 +31,11 @@ def rate_limit(rate_limit):
return rate_limit


def test_route_rule_subset(client):
"""Tests that RLP for a subset of HTTPRouteRule applies to the entire HTTPRouteRule"""
def test_route_subset_method(client):
"""Tests that RLP for a HTTPRouteRule doesn't apply to separate HTTPRouteRule with different method"""
responses = client.get_many("/anything", 5)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
assert client.get("/anything").status_code == 429
assert client.post("/anything").status_code == 429
assert client.post("/anything").status_code == 200
36 changes: 0 additions & 36 deletions testsuite/tests/kuadrant/limitador/test_route_rule_child.py

This file was deleted.

46 changes: 46 additions & 0 deletions testsuite/tests/kuadrant/limitador/test_route_rule_invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Tests that RLP for a HTTPRouteRule doesn't take effect if there isn't a exact matching HTTPRouteRule"""

import pytest

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


@pytest.fixture(scope="module", params=["/anything/get", "/anything", "/get"])
def endpoint(request):
"""Endpoints to apply a RLP to"""
return request.param


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


@pytest.fixture(scope="module")
def rate_limit(rate_limit, endpoint):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value=endpoint, type=MatchType.EXACT), method=HTTPMethod.GET)
)
rate_limit.add_limit("basic", [Limit(5, 10)], route_selectors=[selector])
return rate_limit


def test_route_rule_invalid(client, endpoint):
"""Tests that RLP for a HTTPRouteRule doesn't apply if there isn't an exact match"""
responses = client.get_many(endpoint, 5)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
assert client.get(endpoint).status_code == 200

0 comments on commit f106fe2

Please sign in to comment.