Merge pull request #98 from ametaireau/match-querystring

Add a way to match the querystrings.
This commit is contained in:
Gabriel Falcão
2013-09-23 10:58:57 -07:00
3 changed files with 34 additions and 4 deletions

View File

@@ -261,6 +261,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

View File

@@ -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]

View File

@@ -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<endpoint>\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"