Skip to content

Commit

Permalink
Add support for None values in data pattern (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
lundberg authored Mar 26, 2024
1 parent 15522db commit de7a983
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion respx/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,9 @@ class Data(MultiItemsMixin, Pattern):
value: MultiItems

def clean(self, value: Dict) -> MultiItems:
return MultiItems(value)
return MultiItems(
(key, "" if value is None else str(value)) for key, value in value.items()
)

def parse(self, request: httpx.Request) -> Any:
data, _ = decode_data(request)
Expand Down
17 changes: 14 additions & 3 deletions respx/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import email
from datetime import datetime
from email.message import Message
from typing import Dict, List, NamedTuple, Optional, Tuple, Type, TypeVar, Union, cast
from typing import (
Any,
Dict,
List,
NamedTuple,
Optional,
Tuple,
Type,
TypeVar,
Union,
cast,
)
from urllib.parse import parse_qsl

try:
Expand All @@ -13,13 +24,13 @@


class MultiItems(dict):
def get_list(self, key: str) -> List[str]:
def get_list(self, key: str) -> List[Any]:
try:
return [self[key]]
except KeyError: # pragma: no cover
return []

def multi_items(self) -> List[Tuple[str, str]]:
def multi_items(self) -> List[Tuple[str, Any]]:
return list(self.items())


Expand Down
12 changes: 12 additions & 0 deletions tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,18 @@ def test_content_pattern(lookup, content, expected):
None,
True,
),
(
Lookup.EQUAL,
{"none_value": None},
None,
True,
),
(
Lookup.EQUAL,
{"non_str": 123},
None,
True,
),
(
Lookup.EQUAL,
{"x": "a"},
Expand Down

0 comments on commit de7a983

Please sign in to comment.