Merge "Replace TestResponse with requests_mock"
This commit is contained in:
@@ -467,8 +467,7 @@ class GenericAuthPluginTests(utils.TestCase):
|
|||||||
text = uuid.uuid4().hex
|
text = uuid.uuid4().hex
|
||||||
|
|
||||||
with mock.patch.object(self.session.session, 'request') as mocked:
|
with mock.patch.object(self.session.session, 'request') as mocked:
|
||||||
mocked.return_value = utils.TestResponse({'status_code': 200,
|
mocked.return_value = utils.test_response(text=text)
|
||||||
'text': text})
|
|
||||||
resp = self.session.get('prefix',
|
resp = self.session.get('prefix',
|
||||||
endpoint_filter=self.ENDPOINT_FILTER)
|
endpoint_filter=self.ENDPOINT_FILTER)
|
||||||
|
|
||||||
|
@@ -17,10 +17,7 @@ from keystoneclient import httpclient
|
|||||||
from keystoneclient.tests.unit import utils
|
from keystoneclient.tests.unit import utils
|
||||||
|
|
||||||
|
|
||||||
FAKE_RESPONSE = utils.TestResponse({
|
FAKE_RESPONSE = utils.test_response(json={'hi': 'there'})
|
||||||
"status_code": 200,
|
|
||||||
"text": '{"hi": "there"}',
|
|
||||||
})
|
|
||||||
|
|
||||||
REQUEST_URL = 'https://127.0.0.1:5000/hi'
|
REQUEST_URL = 'https://127.0.0.1:5000/hi'
|
||||||
RESPONSE_BODY = '{"hi": "there"}'
|
RESPONSE_BODY = '{"hi": "there"}'
|
||||||
|
@@ -113,7 +113,7 @@ class SessionTests(utils.TestCase):
|
|||||||
session = client_session.Session(cert='cert.pem', timeout=5,
|
session = client_session.Session(cert='cert.pem', timeout=5,
|
||||||
verify='certs')
|
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)
|
RESP = mock.Mock(return_value=FAKE_RESP)
|
||||||
|
|
||||||
with mock.patch.object(session.session, 'request', RESP) as mocked:
|
with mock.patch.object(session.session, 'request', RESP) as mocked:
|
||||||
@@ -622,7 +622,7 @@ class SessionAuthTests(utils.TestCase):
|
|||||||
|
|
||||||
requests_auth = object()
|
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)
|
RESP = mock.Mock(return_value=FAKE_RESP)
|
||||||
|
|
||||||
with mock.patch.object(sess.session, 'request', RESP) as mocked:
|
with mock.patch.object(sess.session, 'request', RESP) as mocked:
|
||||||
|
@@ -17,6 +17,7 @@ import uuid
|
|||||||
import fixtures
|
import fixtures
|
||||||
from oslo_serialization import jsonutils
|
from oslo_serialization import jsonutils
|
||||||
import requests
|
import requests
|
||||||
|
import requests_mock
|
||||||
from requests_mock.contrib import fixture
|
from requests_mock.contrib import fixture
|
||||||
import six
|
import six
|
||||||
from six.moves.urllib import parse as urlparse
|
from six.moves.urllib import parse as urlparse
|
||||||
@@ -127,32 +128,9 @@ if tuple(sys.version_info)[0:2] < (2, 7):
|
|||||||
TestCase.assertDictEqual = assertDictEqual
|
TestCase.assertDictEqual = assertDictEqual
|
||||||
|
|
||||||
|
|
||||||
class TestResponse(requests.Response):
|
def test_response(**kwargs):
|
||||||
"""Class used to wrap requests.Response.
|
r = requests.Request(method='GET', url='http://localhost:5000').prepare()
|
||||||
|
return requests_mock.create_response(r, **kwargs)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class DisableModuleFixture(fixtures.Fixture):
|
class DisableModuleFixture(fixtures.Fixture):
|
||||||
|
@@ -13,8 +13,6 @@
|
|||||||
from keystoneclient.tests.unit import client_fixtures
|
from keystoneclient.tests.unit import client_fixtures
|
||||||
from keystoneclient.tests.unit import utils
|
from keystoneclient.tests.unit import utils
|
||||||
|
|
||||||
TestResponse = utils.TestResponse
|
|
||||||
|
|
||||||
|
|
||||||
class UnauthenticatedTestCase(utils.TestCase):
|
class UnauthenticatedTestCase(utils.TestCase):
|
||||||
"""Class used as base for unauthenticated calls."""
|
"""Class used as base for unauthenticated calls."""
|
||||||
|
@@ -17,13 +17,14 @@ from oslo_utils import timeutils
|
|||||||
|
|
||||||
from keystoneclient import access
|
from keystoneclient import access
|
||||||
from keystoneclient import fixture
|
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 client_fixtures
|
||||||
from keystoneclient.tests.unit.v3 import utils
|
from keystoneclient.tests.unit.v3 import utils
|
||||||
|
|
||||||
|
|
||||||
TOKEN_RESPONSE = utils.TestResponse({
|
TOKEN_RESPONSE = test_utils.test_response(
|
||||||
"headers": client_fixtures.AUTH_RESPONSE_HEADERS
|
headers=client_fixtures.AUTH_RESPONSE_HEADERS
|
||||||
})
|
)
|
||||||
UNSCOPED_TOKEN = client_fixtures.unscoped_token()
|
UNSCOPED_TOKEN = client_fixtures.unscoped_token()
|
||||||
DOMAIN_SCOPED_TOKEN = client_fixtures.domain_scoped_token()
|
DOMAIN_SCOPED_TOKEN = client_fixtures.domain_scoped_token()
|
||||||
PROJECT_SCOPED_TOKEN = client_fixtures.project_scoped_token()
|
PROJECT_SCOPED_TOKEN = client_fixtures.project_scoped_token()
|
||||||
|
@@ -13,6 +13,7 @@
|
|||||||
from keystoneclient import access
|
from keystoneclient import access
|
||||||
from keystoneclient import exceptions
|
from keystoneclient import exceptions
|
||||||
from keystoneclient import fixture
|
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 client_fixtures
|
||||||
from keystoneclient.tests.unit.v3 import utils
|
from keystoneclient.tests.unit.v3 import utils
|
||||||
|
|
||||||
@@ -21,9 +22,9 @@ class ServiceCatalogTest(utils.TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(ServiceCatalogTest, self).setUp()
|
super(ServiceCatalogTest, self).setUp()
|
||||||
self.AUTH_RESPONSE_BODY = client_fixtures.auth_response_body()
|
self.AUTH_RESPONSE_BODY = client_fixtures.auth_response_body()
|
||||||
self.RESPONSE = utils.TestResponse({
|
self.RESPONSE = test_utils.test_response(
|
||||||
"headers": client_fixtures.AUTH_RESPONSE_HEADERS
|
headers=client_fixtures.AUTH_RESPONSE_HEADERS
|
||||||
})
|
)
|
||||||
|
|
||||||
self.north_endpoints = {'public':
|
self.north_endpoints = {'public':
|
||||||
'http://glance.north.host/glanceapi/public',
|
'http://glance.north.host/glanceapi/public',
|
||||||
|
@@ -19,9 +19,6 @@ from keystoneclient.tests.unit import client_fixtures
|
|||||||
from keystoneclient.tests.unit import utils
|
from keystoneclient.tests.unit import utils
|
||||||
|
|
||||||
|
|
||||||
TestResponse = utils.TestResponse
|
|
||||||
|
|
||||||
|
|
||||||
def parameterize(ref):
|
def parameterize(ref):
|
||||||
"""Rewrites attributes to match the kwarg naming convention in client.
|
"""Rewrites attributes to match the kwarg naming convention in client.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user