Skip to content

Commit

Permalink
Url Quote the path provided to the mocker
Browse files Browse the repository at this point in the history
Requests-mock safely quotes things like spaces and non-url safe
characters. We should do the same thing so that the matchers work.

Fix tests that were passing even though they had invalid schema and
shouldn't have been working.

Closes: #158
  • Loading branch information
jamielennox committed Apr 27, 2021
1 parent b80b2c3 commit f072845
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions releasenotes/notes/url-quote-path-a593190dee974a7a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- Fix [#148](https://github.com/jamielennox/requests-mock/issues/158). When
you have a non url-safe character in your URL it is quoted by requests
however requests-mock just treated it as a normal string.
2 changes: 1 addition & 1 deletion requests_mock/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(self, method, url, responses, complete_qs, request_headers,
url_parts = urlparse.urlparse(url)
self._scheme = url_parts.scheme.lower()
self._netloc = url_parts.netloc.lower()
self._path = url_parts.path or '/'
self._path = urlparse.quote(url_parts.path or '/')
self._query = url_parts.query

if not case_sensitive:
Expand Down
6 changes: 3 additions & 3 deletions tests/pytest/test_with_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def test_simple(requests_mock):


def test_redirect_and_nesting():
url_inner = "inner_mock://example.test/"
url_middle = "middle_mock://example.test/"
url_outer = "outer_mock://example.test/"
url_inner = "inner-mock://example.test/"
url_middle = "middle-mock://example.test/"
url_outer = "outer-mock://example.test/"
url_base = "https://www.example.com/"

text_middle = 'middle' + url_middle
Expand Down
2 changes: 2 additions & 0 deletions tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def test_url_matching(self):
'http://www.test.com/abc')
self.assertMatchBoth('http://www.test.com:5000/abc',
'http://www.test.com:5000/abc')
self.assertMatchBoth('http://www.test.com/a string%url',
'http://www.test.com/a string%url')

self.assertNoMatchBoth('https://www.test.com',
'http://www.test.com')
Expand Down

0 comments on commit f072845

Please sign in to comment.