Skip to content

Commit 7004fe6

Browse files
authored
Raise an error when both can_handle_fun and match_querystring are used (#323)
* Raise an error when both `can_handle_fun` and `match_querystring` are used.
1 parent 535bc57 commit 7004fe6

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎README.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ The callback receives:
242242
.. note::
243243

244244
When ``can_handle_fun`` is provided, it fully defines matching behavior.
245-
In this case ``match_querystring`` is not used.
245+
In this case ``match_querystring`` is ignored. Mocket will raise a ``ValueError``
246+
if you specify both ``can_handle_fun`` and ``match_querystring=False`` together,
247+
as this is likely a mistake.
246248

247249
.. code-block:: python
248250

‎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)