From 0ac0bb493c366cbf390fe194be48b350f3226d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 23 Sep 2013 13:03:51 +0200 Subject: [PATCH 1/2] Add a way to match the querystrings. If you want to register one URI with multiple querystrings, you can pass the "match_querystring" attribute to register_uri. --- httpretty/core.py | 12 ++++++++---- tests/functional/test_requests.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/httpretty/core.py b/httpretty/core.py index d456fb1..2310e32 100644 --- a/httpretty/core.py +++ b/httpretty/core.py @@ -601,7 +601,8 @@ class URIMatcher(object): regex = None info = None - def __init__(self, uri, entries): + def __init__(self, uri, entries, match_querystring=False): + self._match_querystring = match_querystring if type(uri).__name__ == 'SRE_Pattern': self.regex = uri else: @@ -616,7 +617,8 @@ class URIMatcher(object): if self.info: return self.info == info else: - return self.regex.search(info.full_url(use_querystring=False)) + return self.regex.search(info.full_url( + use_querystring=self._match_querystring)) def __str__(self): wrap = 'URLMatcher({0})' @@ -683,7 +685,8 @@ class httpretty(HttpBaseClass): adding_headers=None, forcing_headers=None, status=200, - responses=None, **headers): + responses=None, match_querystring=False, + **headers): uri_is_string = isinstance(uri, basestring) @@ -705,7 +708,8 @@ class httpretty(HttpBaseClass): cls.Response(method=method, uri=uri, **headers), ] - matcher = URIMatcher(uri, entries_for_this_uri) + matcher = URIMatcher(uri, entries_for_this_uri, + match_querystring) if matcher in cls._entries: matcher.entries.extend(cls._entries[matcher]) del cls._entries[matcher] diff --git a/tests/functional/test_requests.py b/tests/functional/test_requests.py index 930ec28..dfd7c42 100644 --- a/tests/functional/test_requests.py +++ b/tests/functional/test_requests.py @@ -519,6 +519,28 @@ def test_httpretty_provides_easy_access_to_querystrings_with_regexes(): }) +@httprettified +def test_httpretty_allows_to_chose_if_querystring_should_be_matched(): + "HTTPretty should provide a way to not match regexes that have a different querystring" + + HTTPretty.register_uri( + HTTPretty.GET, + re.compile("https://example.org/(?P\w+)/$"), + body="Nudge, nudge, wink, wink. Know what I mean?", + match_querystring=True + ) + + response = requests.get('https://example.org/what/') + expect(response.text).to.equal('Nudge, nudge, wink, wink. Know what I mean?') + try: + requests.get('https://example.org/what/?flying=coconuts') + raised = False + except requests.ConnectionError: + raised = True + + assert raised is True + + @httprettified def test_httpretty_should_allow_multiple_methods_for_the_same_uri(): "HTTPretty should allow registering multiple methods for the same uri" From a161f9e660115032721499b9eb174012d10dbecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 23 Sep 2013 17:25:49 +0200 Subject: [PATCH 2/2] add documentation about the new match_querystring feature --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 3229bf7..3bd57ac 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,10 @@ def test_httpretty_should_allow_registering_regexes(): expect(httpretty.last_request().path).to.equal('/v1/deal;brand=GAP') ``` +By default, the regexp you register will match the requests without looking at +the querystring. If you want the querystring to be considered, you can set +`match_querystring=True` when calling `register_uri`. + ## expect for a response, and check the request got by the "server" to make sure it was fine. ```python