Skip to content

Commit f62d197

Browse files
committed
feat: added check_payload to Switcher payload strategy check
1 parent 8676e2b commit f62d197

5 files changed

Lines changed: 46 additions & 10 deletions

File tree

switcher_client/lib/resolver.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from typing import Optional
22

3-
from switcher_client.lib.types import Config, Domain, Entry, Group, ResultDetail, Snapshot, StrategyConfig
4-
from switcher_client.lib.snapshot import process_operation
5-
from switcher_client.lib.utils import get, get_entry
6-
from switcher_client.lib.utils import get_entry
7-
from switcher_client.errors import LocalCriteriaError
8-
from switcher_client.switcher_data import SwitcherData
3+
from ..lib.types import Config, Domain, Entry, Group, ResultDetail, Snapshot, StrategyConfig
4+
from ..lib.snapshot import process_operation
5+
from ..lib.utils import get, get_entry
6+
from ..errors import LocalCriteriaError
7+
from ..switcher_data import SwitcherData
98

109
class Resolver:
1110

switcher_client/lib/snapshot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from typing import Optional
55
from datetime import datetime
66

7-
from switcher_client.lib.types import StrategyConfig
8-
7+
from ..lib.types import StrategyConfig
98
from .utils.payload_reader import parse_json, payload_reader
109
from .utils.ipcidr import IPCIDR
1110
from .utils.timed_match import TimedMatch

switcher_client/switcher_data.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime
22
from abc import ABCMeta
3-
from typing import Optional, Self
3+
from typing import Optional, Self, Union
44

55
from .lib.snapshot import StrategiesType
66

@@ -30,6 +30,16 @@ def check_regex(self, input: str) -> Self:
3030
""" Adds REGEX_VALIDATION input for strategy validation """
3131
return self.check(StrategiesType.REGEX.value, input)
3232

33+
def check_payload(self, input: Union[str, dict]) -> Self:
34+
""" Adds PAYLOAD_VALIDATION input for strategy validation """
35+
if isinstance(input, dict):
36+
import json
37+
payload_str = json.dumps(input)
38+
else:
39+
payload_str = input
40+
41+
return self.check(StrategiesType.PAYLOAD.value, payload_str)
42+
3343
def throttle(self, period: int) -> Self:
3444
""" Sets throttle period in milliseconds """
3545
self._throttle_period = period

tests/test_switcher_local.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from switcher_client.client import Client, ContextOptions
24
from switcher_client.errors import LocalCriteriaError
35
from switcher_client.lib.utils.timed_match.timed_match import TimedMatch
@@ -30,6 +32,32 @@ def test_local_with_strategy():
3032
.check_network('10.0.0.3') \
3133
.is_on('FF2FOR2020')
3234

35+
def test_local_with_strategy_payload():
36+
""" Should use local Snapshot to evaluate the switcher with payload validation strategy """
37+
38+
# given
39+
given_context('tests/snapshots')
40+
Client.load_snapshot()
41+
42+
switcher = Client.get_switcher()
43+
payload = {
44+
'id': 12345,
45+
'user': {
46+
'login': 'test_user',
47+
'role': 'admin'
48+
}
49+
}
50+
51+
# test (using stringified JSON)
52+
assert switcher \
53+
.check_payload(json.dumps(payload)) \
54+
.is_on('FF2FOR2023')
55+
56+
# test (using dict)
57+
assert switcher \
58+
.check_payload(payload) \
59+
.is_on('FF2FOR2023')
60+
3361
def test_local_with_strategy_no_input():
3462
""" Should return disabled when no input is provided for the strategy """
3563

tests/utils/test_timed_match.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def test_should_return_true(self):
4545
assert result is True
4646

4747
def test_should_return_false_and_abort_processing(self):
48-
4948
"""Should return false and abort processing for ReDoS pattern."""
49+
5050
result = TimedMatch.try_match([NOK_RE], NOK_INPUT)
5151
assert result is False
5252

0 commit comments

Comments
 (0)