Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query string ordering #354

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion httpretty/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ def __repr__(self):
from urllib.parse import quote
from urllib.parse import quote_plus
from urllib.parse import unquote
from urllib.parse import urlencode
unquote_utf8 = unquote

def encode_obj(in_obj):
return in_obj
except ImportError: # pragma: no cover
from urlparse import urlsplit, urlunsplit, parse_qs, unquote
from urllib import quote, quote_plus
from urllib import quote, quote_plus, urlencode

def unquote_utf8(qs):
if isinstance(qs, text_type):
Expand All @@ -72,6 +76,31 @@ def unquote_utf8(qs):
else:
return s

def encode_obj(in_obj):

def encode_list(in_list):
out_list = []
for el in in_list:
out_list.append(encode_obj(el))
return out_list

def encode_dict(in_dict):
out_dict = {}
for k, v in in_dict.iteritems():
out_dict[k] = encode_obj(v)
return out_dict

if isinstance(in_obj, unicode):
return in_obj.encode('utf-8')
elif isinstance(in_obj, list):
return encode_list(in_obj)
elif isinstance(in_obj, tuple):
return tuple(encode_list(in_obj))
elif isinstance(in_obj, dict):
return encode_dict(in_obj)

return in_obj


try: # pragma: no cover
from http.server import BaseHTTPRequestHandler
Expand All @@ -93,6 +122,7 @@ def unquote_utf8(qs):
'BaseHTTPRequestHandler',
'quote',
'quote_plus',
'urlencode',
'urlunsplit',
'urlsplit',
'parse_qs',
Expand Down
11 changes: 10 additions & 1 deletion httpretty/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
BaseHTTPRequestHandler,
quote,
quote_plus,
urlencode,
encode_obj,
urlunsplit,
urlsplit,
parse_qs,
Expand Down Expand Up @@ -882,7 +884,14 @@ def __init__(self,

self.port = port or 80
self.path = path or ''
self.query = query or ''
if query:
query_items = sorted(parse_qs(query).items())
self.query = urlencode(
encode_obj(query_items),
doseq=True,
)
else:
self.query = ''
if scheme:
self.scheme = scheme
elif self.port in POTENTIAL_HTTPS_PORTS:
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,14 @@ def test_URIMatcher_respects_querystring():
info = URIInfo.from_uri('http://www.foo.com/?query=true', None)
assert matcher.matches(info)

matcher = URIMatcher('http://www.foo.com/?query=true&unquery=false', None, match_querystring=True)
info = URIInfo.from_uri('http://www.foo.com/?unquery=false&query=true', None)
assert matcher.matches(info)

matcher = URIMatcher('http://www.foo.com/?unquery=false&query=true', None, match_querystring=True)
info = URIInfo.from_uri('http://www.foo.com/?query=true&unquery=false', None)
assert matcher.matches(info)


def test_URIMatcher_equality_respects_querystring():
("URIMatcher equality check should check querystring")
Expand All @@ -642,3 +650,7 @@ def test_URIMatcher_equality_respects_querystring():
matcher_a = URIMatcher('http://www.foo.com/?query=true', None, match_querystring=True)
matcher_b = URIMatcher('http://www.foo.com/', None, match_querystring=True)
assert not matcher_a == matcher_b

matcher_a = URIMatcher('http://www.foo.com/?query=true&unquery=false', None, match_querystring=True)
matcher_b = URIMatcher('http://www.foo.com/?unquery=false&query=true', None, match_querystring=True)
assert matcher_a == matcher_b
2 changes: 1 addition & 1 deletion tests/unit/test_httpretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_uri_info_full_url():
)

expect(uri_info.full_url()).to.equal(
"http://johhny:password@google.com/?foo=bar&baz=test"
"http://johhny:password@google.com/?baz=test&foo=bar"
)

expect(uri_info.full_url(use_querystring=False)).to.equal(
Expand Down