diff --git a/AUTHORS.rst b/AUTHORS.rst index ad596bf8..3eeb1cff 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -165,3 +165,4 @@ Patches and Suggestions - Robin Linderborg (`@vienno `_) - Brian Samek (`@bsamek `_) - Dmitry Dygalo (`@Stranger6667 `_) +- Tomáš Heger (`@geckon `_) diff --git a/requests/models.py b/requests/models.py index e1c3ccd5..d98b9620 100644 --- a/requests/models.py +++ b/requests/models.py @@ -335,6 +335,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): else: url = unicode(url) if is_py2 else str(url) + # Ignore any leading and trailing whitespace characters. + url = url.strip() + # Don't do any URL preparation for non-HTTP schemes like `mailto`, # `data` etc to work around exceptions from `url_parse`, which # handles RFC 3986 only. diff --git a/test_requests.py b/test_requests.py index cc8223c5..45afd517 100755 --- a/test_requests.py +++ b/test_requests.py @@ -1317,6 +1317,17 @@ class TestCaseInsensitiveDict: cid['changed'] = True assert cid != cid_copy + def test_url_surrounding_whitespace(self, httpbin): + """Test case with URLs surrounded by whitespace characters.""" + get_url = httpbin('get') + # All surrounding whitespaces are supposed to be ignored: + assert requests.get(get_url + ' ').status_code == 200 + assert requests.get(' ' + get_url).status_code == 200 + assert requests.get(get_url + ' \t ').status_code == 200 + assert requests.get(' \t' + get_url).status_code == 200 + assert requests.get(get_url + '\n').status_code == 200 + # The whitespaces can't be in the middle of the URL though: + assert requests.get(get_url + ' abc').status_code == 404 class TestUtils: