Skip to content

Commit c5f96e4

Browse files
committed
Raise an error when both can_handle_fun and match_querystring are used.
1 parent 535bc57 commit c5f96e4

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

mocket/mocks/mockhttp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,16 @@ def __init__(
237237
responses: Response(s) to return
238238
match_querystring: Whether to match query strings
239239
can_handle_fun: Custom matching function
240+
241+
Raises:
242+
ValueError: If both can_handle_fun and match_querystring are specified
240243
"""
244+
if can_handle_fun and not match_querystring:
245+
raise ValueError(
246+
"cannot specify both 'can_handle_fun' and 'match_querystring=False': "
247+
"when using a custom matching function, 'match_querystring' is ignored"
248+
)
249+
241250
self._can_handle_fun = can_handle_fun if can_handle_fun else self._can_handle
242251

243252
uri = urlsplit(uri)

tests/test_http.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,39 @@ def test_can_handle(self):
482482
response = requests.get("http://testme.org/foobar?b=2")
483483
self.assertEqual(response.status_code, 200)
484484
self.assertEqual(response.json(), {"message": "Missed!"})
485+
486+
def test_can_handle_fun_with_match_querystring_false_raises(self):
487+
"""Test that using both can_handle_fun and match_querystring=False raises ValueError."""
488+
with self.assertRaises(ValueError) as context:
489+
Entry(
490+
"http://testme.org/path",
491+
Entry.GET,
492+
responses=["test"],
493+
can_handle_fun=lambda path, qs: True,
494+
match_querystring=False,
495+
)
496+
self.assertIn(
497+
"cannot specify both 'can_handle_fun' and 'match_querystring=False'",
498+
str(context.exception),
499+
)
500+
501+
def test_can_handle_fun_with_match_querystring_true_works(self):
502+
"""Test that using can_handle_fun with match_querystring=True works fine."""
503+
entry = Entry(
504+
"http://testme.org/path",
505+
Entry.GET,
506+
responses=["test"],
507+
can_handle_fun=lambda path, qs: True,
508+
match_querystring=True,
509+
)
510+
self.assertIsNotNone(entry)
511+
512+
def test_can_handle_fun_alone_works(self):
513+
"""Test that using can_handle_fun alone (without specifying match_querystring) works."""
514+
entry = Entry(
515+
"http://testme.org/path",
516+
Entry.GET,
517+
responses=["test"],
518+
can_handle_fun=lambda path, qs: True,
519+
)
520+
self.assertIsNotNone(entry)

0 commit comments

Comments
 (0)