def _cs_request(self, url, method, **kwargs): # Check that certain things are called correctly if method in ['GET', 'DELETE']: assert 'body' not in kwargs elif method == 'PUT': assert 'body' in kwargs if url is not None: # Call the method args = parse.parse_qsl(parse.urlparse(url)[4]) kwargs.update(args) munged_url = url.rsplit('?', 1)[0] munged_url = munged_url.strip('/').replace('/', '_') munged_url = munged_url.replace('.', '_') munged_url = munged_url.replace('-', '_') munged_url = munged_url.replace(' ', '_') callback = "%s_%s" % (method.lower(), munged_url) if not hasattr(self, callback): raise AssertionError('Called unknown API method: %s %s, ' 'expected fakes method name: %s' % (method, url, callback)) # Note the call self.callstack.append((method, url, kwargs.get('body'))) status, headers, body = getattr(self, callback)(**kwargs) r = utils.TestResponse({ "status_code": status, "text": body, "headers": headers, }) return r, body
def _test_from_response(self, body): data = { 'status_code': 503, 'headers': { 'Content-Type': 'application/json', 'x-compute-request-id': ('req-65d6443c-5910-4eb4-b48a-e69849c26836'), }, 'text': json.dumps(body) } response = test_utils.TestResponse(data) fake_url = 'http://localhost:8779/v1.0/fake/instances' error = exceptions.from_response(response, 'GET', fake_url) self.assertIsInstance(error, exceptions.ServiceUnavailable)