I am creating my own fixture to simulate a service endpoint needed in my unit tests. In order to intercept the HTTP requests, I use requests_mock as follows:
@pytest.fixture
def sparql_endpoint(requests_mock):
yield lambda uri, initial_data: Endpoint(requests_mock, uri, initial_data)
and in Endpoint.__init__
I do the following:
m.post(url=uri, raw=self.handle_post)
m.get(url=uri, raw=self.handle_get)
In my actual testcase I inject the endpoint and initialize it:
def test_basic_select(my_endpoint):
repo_uri = 'https://my.rdfdb.com/repo/sparql'
rdf_files = ['tests/upper_ontology.ttl',
'tests/domain_ontology.ttl',
'tests/instance_data.ttl']
endpoint = sparql_endpoint(repo_uri, rdf_files)
Which does, in fact, initialize the mocked endpoint and I see Mocker.start()
get invoked if I set a breakpoint there. However, later in the testcase I get the following:
..\..\AppData\Local\Programs\Python\Python37\lib\urllib\request.py:222: in urlopen
return opener.open(url, data, timeout)
..\..\AppData\Local\Programs\Python\Python37\lib\urllib\request.py:525: in open
response = self._open(req, data)
..\..\AppData\Local\Programs\Python\Python37\lib\urllib\request.py:543: in _open
'_open', req)
..\..\AppData\Local\Programs\Python\Python37\lib\urllib\request.py:503: in _call_chain
result = func(*args)
..\..\AppData\Local\Programs\Python\Python37\lib\urllib\request.py:1360: in https_open
context=self._context, check_hostname=self._check_hostname)
E urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
..\..\AppData\Local\Programs\Python\Python37\lib\urllib\request.py:1319: URLError
Because it cannot resolve the fake URL I gave it. So, did I somehow mess up the handler registration so that the Matcher is not kicking the request there? Why is urlopen still trying to resolve the host?