Use create_response for fake http response in tests
The old FakeHTTPResponse we're using in the tests is really basic and lacks a lot of the information that is expected in a response from an adapter. This is a hard problem that we've already solved in the create_response function so use that to create a test response instead of a custom solution here. Change-Id: I2999c9c69b4e9ad895114fab8ae7f8ce275fa2a4 Closes-Bug: #1642697
This commit is contained in:
parent
c3ed2aa479
commit
81b72f4397
@ -14,8 +14,8 @@ import mock
|
||||
import requests
|
||||
|
||||
import requests_mock
|
||||
from requests_mock import compat
|
||||
from requests_mock import exceptions
|
||||
from requests_mock import response
|
||||
from requests_mock.tests import base
|
||||
|
||||
original_send = requests.Session.send
|
||||
@ -56,20 +56,23 @@ class MockerTests(base.TestCase):
|
||||
@requests_mock.mock(real_http=True)
|
||||
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')
|
||||
|
||||
# NOTE(jamielennox): hack for requests 1.2.3 remove after
|
||||
# requirements catches up.
|
||||
class FakeHTTPResponse(object):
|
||||
_original_response = compat._fake_http_response
|
||||
|
||||
real_send.return_value = requests.Response()
|
||||
real_send.return_value.status_code = 200
|
||||
real_send.return_value.raw = FakeHTTPResponse()
|
||||
requests.get(url)
|
||||
# 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)
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_with_test_decorator(self, m):
|
||||
self._do_test(m)
|
||||
|
Loading…
x
Reference in New Issue
Block a user