Skip to content

Commit 982c8ac

Browse files
authored
Merge pull request #283 from twm/282-params
Permit & and # in params
2 parents fe0fa76 + 6c7d95b commit 982c8ac

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

changelog.d/282.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In treq 20.3.0 the *params* argument didn't accept parameter names or values that contain the characters ``&`` or ``#``.
2+
Now these characters are properly escaped.

src/treq/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from twisted.python.components import proxyForInterface
1111
from twisted.python.compat import _PY3, unicode
1212
from twisted.python.filepath import FilePath
13-
from hyperlink import URL
13+
from hyperlink import parse as _parse_url
1414

1515
from twisted.web.http_headers import Headers
1616
from twisted.web.iweb import IBodyProducer, IResponse
@@ -155,9 +155,9 @@ def request(self, method, url, **kwargs):
155155
method = method.encode('ascii').upper()
156156

157157
if isinstance(url, unicode):
158-
parsed_url = URL.from_text(url)
158+
parsed_url = _parse_url(url)
159159
else:
160-
parsed_url = URL.from_text(url.decode('ascii'))
160+
parsed_url = _parse_url(url.decode('ascii'))
161161

162162
# Join parameters provided in the URL
163163
# and the ones passed as argument.

src/treq/test/test_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@ def test_request_tuple_query_param_coercion(self):
139139
None,
140140
)
141141

142+
def test_request_query_param_seps(self):
143+
"""
144+
When the characters ``&`` and ``#`` are passed to *params* as param
145+
names or values they are percent-escaped in the URL.
146+
147+
This reproduces https://github.com/twisted/treq/issues/282
148+
"""
149+
self.client.request('GET', 'http://example.com/', params=(
150+
('ampersand', '&'),
151+
('&', 'ampersand'),
152+
('octothorpe', '#'),
153+
('#', 'octothorpe'),
154+
))
155+
156+
self.agent.request.assert_called_once_with(
157+
b'GET',
158+
(
159+
b'http://example.com/'
160+
b'?ampersand=%26'
161+
b'&%26=ampersand'
162+
b'&octothorpe=%23'
163+
b'&%23=octothorpe'
164+
),
165+
Headers({b'accept-encoding': [b'gzip']}),
166+
None,
167+
)
168+
142169
def test_request_merge_query_params(self):
143170
self.client.request('GET', 'http://example.com/?baz=bax',
144171
params={'foo': ['bar', 'baz']})

0 commit comments

Comments
 (0)