Skip to content

Commit

Permalink
Added support for purl.
Browse files Browse the repository at this point in the history
  • Loading branch information
noamkush authored and jamielennox committed Apr 23, 2019
1 parent 5ad9bed commit 7314358
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions requests_mock/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
from requests_mock.request import _RequestObjectProxy
from requests_mock.response import _MatcherResponse

try:
import purl
purl_types = (purl.URL,)
except ImportError:
purl = None
purl_types = ()

ANY = object()


Expand Down Expand Up @@ -95,6 +102,16 @@ def __init__(self, method, url, responses, complete_qs, request_headers,
self._path = self._path.lower()
self._query = self._query.lower()

elif isinstance(url, purl_types):
self._scheme = url.scheme()
self._netloc = url.netloc()
self._path = url.path()
self._query = url.query()

if not case_sensitive:
self._path = self._path.lower()
self._query = self._query.lower()

else:
self._scheme = None
self._netloc = None
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fixtures
mock
purl
pytest
sphinx
testrepository>=0.0.18
Expand Down
7 changes: 7 additions & 0 deletions tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import json
import re

import purl
import requests
import six
from six.moves.urllib import parse as urlparse
Expand Down Expand Up @@ -327,6 +328,12 @@ def test_with_regexp(self):
resp = self.session.get(u)
self.assertEqual('resp', resp.text)

def test_with_purl(self):
self.adapter.register_uri('GET', purl.URL('mock://www.tester.com/a'), text='resp')

resp = self.session.get('mock://www.tester.com/a')
self.assertEqual('resp', resp.text)

def test_requests_in_history_on_no_match(self):
self.assertRaises(requests_mock.NoMockAddress,
self.session.get,
Expand Down

0 comments on commit 7314358

Please sign in to comment.