Skip to content

Commit

Permalink
Merge pull request #169 from Matoking/quote_fix
Browse files Browse the repository at this point in the history
Use requests to quote URLs
  • Loading branch information
jamielennox authored Apr 30, 2021
2 parents cb8084a + 77f1a1f commit 850671f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fixes:
- As part of 1.9.0 we started quoting unsafe URL characters. This was done incorrectly that meant we requoted existing quoted strings. Fixes #170.
3 changes: 2 additions & 1 deletion requests_mock/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import weakref

from requests.adapters import BaseAdapter
from requests.utils import requote_uri
import six
from six.moves.urllib import parse as urlparse

Expand Down Expand Up @@ -102,7 +103,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 = urlparse.quote(url_parts.path or '/')
self._path = requote_uri(url_parts.path or '/')
self._query = url_parts.query

if not case_sensitive:
Expand Down
11 changes: 8 additions & 3 deletions tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ 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')
self.assertNoMatchBoth('http://www.test.com/abc',
Expand All @@ -161,6 +158,14 @@ def test_url_matching(self):
self.assertNoMatchBoth('http://test.com/abc/',
'http://www.test.com:5000/abc')

def test_quotation(self):
self.assertMatchBoth('http://www.test.com/a string%url',
'http://www.test.com/a string%url')
self.assertMatchBoth('http://www.test.com/ABC 123',
'http://www.test.com/ABC%20123')
self.assertMatchBoth('http://www.test.com/user@example.com',
'http://www.test.com/user@example.com')

def test_subset_match(self):
self.assertMatch('/path', 'http://www.test.com/path')
self.assertMatch('/path', 'http://www.test.com/path')
Expand Down

0 comments on commit 850671f

Please sign in to comment.