Skip to content

Commit

Permalink
Correctly url-encode list parameters (googleapis#3657)
Browse files Browse the repository at this point in the history
  • Loading branch information
isra17 authored and landrito committed Aug 21, 2017
1 parent 6659724 commit 43bae9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/google/cloud/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def build_api_url(cls, path, query_params=None,

query_params = query_params or {}
if query_params:
url += '?' + urlencode(query_params)
url += '?' + urlencode(query_params, doseq=True)

return url

Expand Down
24 changes: 16 additions & 8 deletions core/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ def test_build_api_url_no_extra_query_params(self):
self.assertEqual(conn.build_api_url('/foo'), URI)

def test_build_api_url_w_extra_query_params(self):
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import parse_qs
from six.moves.urllib.parse import urlsplit

client = object()
conn = self._make_mock_one(client)
uri = conn.build_api_url('/foo', {'bar': 'baz'})
uri = conn.build_api_url('/foo', {
'bar': 'baz',
'qux': ['quux', 'corge']
})

scheme, netloc, path, qs, _ = urlsplit(uri)
self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL)
Expand All @@ -111,8 +114,9 @@ def test_build_api_url_w_extra_query_params(self):
'foo',
])
self.assertEqual(path, PATH)
parms = dict(parse_qsl(qs))
self.assertEqual(parms['bar'], 'baz')
parms = dict(parse_qs(qs))
self.assertEqual(parms['bar'], ['baz'])
self.assertEqual(parms['qux'], ['quux', 'corge'])

def test__make_request_no_data_no_content_type_no_headers(self):
http = _Http(
Expand Down Expand Up @@ -222,7 +226,7 @@ def test_api_request_wo_json_expected(self):
b'CONTENT')

def test_api_request_w_query_params(self):
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import parse_qs
from six.moves.urllib.parse import urlsplit

http = _Http(
Expand All @@ -231,7 +235,10 @@ def test_api_request_w_query_params(self):
)
client = mock.Mock(_http=http, spec=['_http'])
conn = self._make_mock_one(client)
self.assertEqual(conn.api_request('GET', '/', {'foo': 'bar'}), {})
self.assertEqual(conn.api_request('GET', '/', {
'foo': 'bar',
'baz': ['qux', 'quux']
}), {})
self.assertEqual(http._called_with['method'], 'GET')
uri = http._called_with['uri']
scheme, netloc, path, qs, _ = urlsplit(uri)
Expand All @@ -244,8 +251,9 @@ def test_api_request_w_query_params(self):
'',
])
self.assertEqual(path, PATH)
parms = dict(parse_qsl(qs))
self.assertEqual(parms['foo'], 'bar')
parms = dict(parse_qs(qs))
self.assertEqual(parms['foo'], ['bar'])
self.assertEqual(parms['baz'], ['qux', 'quux'])
self.assertIsNone(http._called_with['body'])
expected_headers = {
'Accept-Encoding': 'gzip',
Expand Down

0 comments on commit 43bae9a

Please sign in to comment.