Replace TestResponse with requests_mock
The TestResponse object doesn't do the right thing with regards to content vs text. Just reuse the one from requests_mock rather that try and fix it. Change-Id: Ia8bcae126babb0e616329928c57f875a50a957d6
This commit is contained in:
		@@ -467,8 +467,7 @@ class GenericAuthPluginTests(utils.TestCase):
 | 
			
		||||
        text = uuid.uuid4().hex
 | 
			
		||||
 | 
			
		||||
        with mock.patch.object(self.session.session, 'request') as mocked:
 | 
			
		||||
            mocked.return_value = utils.TestResponse({'status_code': 200,
 | 
			
		||||
                                                      'text': text})
 | 
			
		||||
            mocked.return_value = utils.test_response(text=text)
 | 
			
		||||
            resp = self.session.get('prefix',
 | 
			
		||||
                                    endpoint_filter=self.ENDPOINT_FILTER)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,10 +17,7 @@ from keystoneclient import httpclient
 | 
			
		||||
from keystoneclient.tests.unit import utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FAKE_RESPONSE = utils.TestResponse({
 | 
			
		||||
    "status_code": 200,
 | 
			
		||||
    "text": '{"hi": "there"}',
 | 
			
		||||
})
 | 
			
		||||
FAKE_RESPONSE = utils.test_response(json={'hi': 'there'})
 | 
			
		||||
 | 
			
		||||
REQUEST_URL = 'https://127.0.0.1:5000/hi'
 | 
			
		||||
RESPONSE_BODY = '{"hi": "there"}'
 | 
			
		||||
 
 | 
			
		||||
@@ -113,7 +113,7 @@ class SessionTests(utils.TestCase):
 | 
			
		||||
        session = client_session.Session(cert='cert.pem', timeout=5,
 | 
			
		||||
                                         verify='certs')
 | 
			
		||||
 | 
			
		||||
        FAKE_RESP = utils.TestResponse({'status_code': 200, 'text': 'resp'})
 | 
			
		||||
        FAKE_RESP = utils.test_response(text='resp')
 | 
			
		||||
        RESP = mock.Mock(return_value=FAKE_RESP)
 | 
			
		||||
 | 
			
		||||
        with mock.patch.object(session.session, 'request', RESP) as mocked:
 | 
			
		||||
@@ -622,7 +622,7 @@ class SessionAuthTests(utils.TestCase):
 | 
			
		||||
 | 
			
		||||
        requests_auth = object()
 | 
			
		||||
 | 
			
		||||
        FAKE_RESP = utils.TestResponse({'status_code': 200, 'text': 'resp'})
 | 
			
		||||
        FAKE_RESP = utils.test_response(text='resp')
 | 
			
		||||
        RESP = mock.Mock(return_value=FAKE_RESP)
 | 
			
		||||
 | 
			
		||||
        with mock.patch.object(sess.session, 'request', RESP) as mocked:
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ import uuid
 | 
			
		||||
import fixtures
 | 
			
		||||
from oslo_serialization import jsonutils
 | 
			
		||||
import requests
 | 
			
		||||
import requests_mock
 | 
			
		||||
from requests_mock.contrib import fixture
 | 
			
		||||
import six
 | 
			
		||||
from six.moves.urllib import parse as urlparse
 | 
			
		||||
@@ -127,32 +128,9 @@ if tuple(sys.version_info)[0:2] < (2, 7):
 | 
			
		||||
    TestCase.assertDictEqual = assertDictEqual
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestResponse(requests.Response):
 | 
			
		||||
    """Class used to wrap requests.Response.
 | 
			
		||||
 | 
			
		||||
    It also provides convenience to initialize with a dict.
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    def __init__(self, data):
 | 
			
		||||
        self._text = None
 | 
			
		||||
        super(TestResponse, self).__init__()
 | 
			
		||||
        if isinstance(data, dict):
 | 
			
		||||
            self.status_code = data.get('status_code', 200)
 | 
			
		||||
            headers = data.get('headers')
 | 
			
		||||
            if headers:
 | 
			
		||||
                self.headers.update(headers)
 | 
			
		||||
            # Fake the text attribute to streamline Response creation
 | 
			
		||||
            # _content is defined by requests.Response
 | 
			
		||||
            self._content = data.get('text')
 | 
			
		||||
        else:
 | 
			
		||||
            self.status_code = data
 | 
			
		||||
 | 
			
		||||
    def __eq__(self, other):
 | 
			
		||||
        return self.__dict__ == other.__dict__
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def text(self):
 | 
			
		||||
        return self.content
 | 
			
		||||
def test_response(**kwargs):
 | 
			
		||||
    r = requests.Request(method='GET', url='http://localhost:5000').prepare()
 | 
			
		||||
    return requests_mock.create_response(r, **kwargs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DisableModuleFixture(fixtures.Fixture):
 | 
			
		||||
 
 | 
			
		||||
@@ -13,8 +13,6 @@
 | 
			
		||||
from keystoneclient.tests.unit import client_fixtures
 | 
			
		||||
from keystoneclient.tests.unit import utils
 | 
			
		||||
 | 
			
		||||
TestResponse = utils.TestResponse
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class UnauthenticatedTestCase(utils.TestCase):
 | 
			
		||||
    """Class used as base for unauthenticated calls."""
 | 
			
		||||
 
 | 
			
		||||
@@ -17,13 +17,14 @@ from oslo_utils import timeutils
 | 
			
		||||
 | 
			
		||||
from keystoneclient import access
 | 
			
		||||
from keystoneclient import fixture
 | 
			
		||||
from keystoneclient.tests.unit import utils as test_utils
 | 
			
		||||
from keystoneclient.tests.unit.v3 import client_fixtures
 | 
			
		||||
from keystoneclient.tests.unit.v3 import utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
TOKEN_RESPONSE = utils.TestResponse({
 | 
			
		||||
    "headers": client_fixtures.AUTH_RESPONSE_HEADERS
 | 
			
		||||
})
 | 
			
		||||
TOKEN_RESPONSE = test_utils.test_response(
 | 
			
		||||
    headers=client_fixtures.AUTH_RESPONSE_HEADERS
 | 
			
		||||
)
 | 
			
		||||
UNSCOPED_TOKEN = client_fixtures.unscoped_token()
 | 
			
		||||
DOMAIN_SCOPED_TOKEN = client_fixtures.domain_scoped_token()
 | 
			
		||||
PROJECT_SCOPED_TOKEN = client_fixtures.project_scoped_token()
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@
 | 
			
		||||
from keystoneclient import access
 | 
			
		||||
from keystoneclient import exceptions
 | 
			
		||||
from keystoneclient import fixture
 | 
			
		||||
from keystoneclient.tests.unit import utils as test_utils
 | 
			
		||||
from keystoneclient.tests.unit.v3 import client_fixtures
 | 
			
		||||
from keystoneclient.tests.unit.v3 import utils
 | 
			
		||||
 | 
			
		||||
@@ -21,9 +22,9 @@ class ServiceCatalogTest(utils.TestCase):
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        super(ServiceCatalogTest, self).setUp()
 | 
			
		||||
        self.AUTH_RESPONSE_BODY = client_fixtures.auth_response_body()
 | 
			
		||||
        self.RESPONSE = utils.TestResponse({
 | 
			
		||||
            "headers": client_fixtures.AUTH_RESPONSE_HEADERS
 | 
			
		||||
        })
 | 
			
		||||
        self.RESPONSE = test_utils.test_response(
 | 
			
		||||
            headers=client_fixtures.AUTH_RESPONSE_HEADERS
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        self.north_endpoints = {'public':
 | 
			
		||||
                                'http://glance.north.host/glanceapi/public',
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,6 @@ from keystoneclient.tests.unit import client_fixtures
 | 
			
		||||
from keystoneclient.tests.unit import utils
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
TestResponse = utils.TestResponse
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def parameterize(ref):
 | 
			
		||||
    """Rewrites attributes to match the kwarg naming convention in client.
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user