def test_real_http_changes(self, real_send): url = 'http://www.google.com/' test_text = 'real http data' test_bytes = test_text.encode('utf-8') req = requests.Request(method='GET', url=url) real_send.return_value = response.create_response(req.prepare(), status_code=200, content=test_bytes) with requests_mock.Mocker() as m: # real_http defaults to false so should raise NoMockAddress self.assertRaises(exceptions.NoMockAddress, requests.get, url) self.assertEqual(1, m.call_count) self.assertEqual(0, real_send.call_count) # change the value of real_http mid test m.real_http = True # fetch the url again and it should go through to the real url that # we've mocked out at a lower level. resp = requests.get(url) self.assertEqual(1, real_send.call_count) self.assertEqual(url, real_send.call_args[0][0].url) self.assertEqual(test_text, resp.text) self.assertEqual(test_bytes, resp.content)
def test_real_http_and_session(self, real_send): url = 'http://www.google.com/' test_text = 'real http data' test_bytes = test_text.encode('utf-8') req = requests.Request(method='GET', url=url) real_send.return_value = response.create_response(req.prepare(), status_code=200, content=test_bytes) session = requests.Session() with requests_mock.Mocker(session=session, real_http=True): resp = session.get(url) self.assertEqual(test_text, resp.text) self.assertEqual(test_bytes, resp.content)
def test_real_http(self, real_send, mocker): url = 'http://www.google.com/' test_text = 'real http data' test_bytes = test_text.encode('utf-8') # using create_response is a bit bootstrappy here but so long as it's # coming from HTTPAdapter.send it's ok req = requests.Request(method='GET', url=url) real_send.return_value = response.create_response(req.prepare(), status_code=200, content=test_bytes) resp = requests.get(url) self.assertEqual(1, real_send.call_count) self.assertEqual(url, real_send.call_args[0][0].url) self.assertEqual(test_text, resp.text) self.assertEqual(test_bytes, resp.content)
def test_real_http(self, real_send, mocker): url = 'http://www.google.com/' test_text = 'real http data' test_bytes = test_text.encode('utf-8') # using create_response is a bit bootstrappy here but so long as it's # coming from HTTPAdapter.send it's ok req = requests.Request(method='GET', url=url) real_send.return_value = response.create_response(req.prepare(), status_code=200, content=test_bytes) resp = requests.get(url) self.assertEqual(1, real_send.call_count) self.assertEqual(url, real_send.call_args[0][0].url) self.assertEqual(test_text, resp.text) self.assertEqual(test_bytes, resp.content)
def create_response(self, **kwargs): return response.create_response(self.request, **kwargs)