def test_kwargs_with_files(self, mock_dumps): fake = fakes.FakeHTTPResponse(200, 'OK', {'Content-Type': 'application/json'}, '{}') mock_dumps.return_value = "{'files': test}}" data = six.BytesIO(b'test') kwargs = { 'endpoint_override': 'http://no.where/', 'data': { 'files': data } } client = http.SessionClient(mock.ANY) self.request.return_value = (fake, {}) resp, body = client.request('', 'GET', **kwargs) self.assertEqual( { 'endpoint_override': 'http://no.where/', 'json': { 'files': data }, 'user_agent': 'python-pankoclient', 'raise_exc': False }, self.request.call_args[1]) self.assertEqual(200, resp.status_code) self.assertEqual({}, body) self.assertEqual({}, utils.get_response_body(resp))
def test_get_response_body_json_value_error(self): resp = mock.Mock() resp.content = mock.sentinel.fake_content resp.headers = {'Content-Type': 'application/json'} resp.json.side_effect = ValueError('json format error.') body = utils.get_response_body(resp) self.assertEqual(mock.sentinel.fake_content, body)
def test_302_location_not_override(self): fake1 = fakes.FakeHTTPResponse(302, 'OK', {'location': 'http://no.where/ishere'}, '') fake2 = fakes.FakeHTTPResponse(200, 'OK', {'Content-Type': 'application/json'}, jsonutils.dumps({'Mount': 'Fuji'})) self.request.side_effect = [(fake1, None), (fake2, {'Mount': 'Fuji'})] client = http.SessionClient(session=mock.ANY, auth=mock.ANY, endpoint_override='http://endpoint/') resp, body = client.request('', 'GET', redirect=True) self.assertEqual(200, resp.status_code) self.assertEqual({'Mount': 'Fuji'}, utils.get_response_body(resp)) self.assertEqual({'Mount': 'Fuji'}, body) self.assertEqual(('', 'GET'), self.request.call_args_list[0][0]) self.assertEqual(('http://no.where/ishere', 'GET'), self.request.call_args_list[1][0]) for call in self.request.call_args_list: self.assertEqual( { 'user_agent': 'python-pankoclient', 'raise_exc': False, 'redirect': True }, call[1])
def raw_request(self, method, url, **kwargs): kwargs.setdefault('headers', {}) kwargs['headers'].setdefault('Content-Type', 'application/octet-stream') resp = self._http_request(url, method, **kwargs) body = utils.get_response_body(resp) return resp, body
def json_request(self, method, url, **kwargs): kwargs.setdefault('headers', {}) kwargs['headers'].setdefault('Content-Type', 'application/json') kwargs['headers'].setdefault('Accept', 'application/json') if 'data' in kwargs: kwargs['data'] = jsonutils.dumps(kwargs['data']) resp = self._http_request(url, method, **kwargs) body = utils.get_response_body(resp) return resp, body
def test_kwargs(self): fake = fakes.FakeHTTPResponse(200, 'OK', {'Content-Type': 'application/json'}, '{}') kwargs = dict(endpoint_override='http://no.where/', data='some_data') client = http.SessionClient(mock.ANY) self.request.return_value = (fake, {}) resp, body = client.request('', 'GET', **kwargs) self.assertEqual( { 'endpoint_override': 'http://no.where/', 'json': 'some_data', 'user_agent': 'python-pankoclient', 'raise_exc': False }, self.request.call_args[1]) self.assertEqual(200, resp.status_code) self.assertEqual({}, body) self.assertEqual({}, utils.get_response_body(resp))
def test_get_response_body_unknown_type(self): resp = mock.Mock() resp.headers = {'Content-Type': 'application/unknown'} body = utils.get_response_body(resp) self.assertIsNone(body)
def test_get_response_body_raw(self): resp = mock.Mock() resp.headers = {'Content-Type': 'application/octet-stream'} resp.body.return_value = mock.sentinel.fake_body body = utils.get_response_body(resp) self.assertEqual(mock.sentinel.fake_body, body)
def test_get_response_body_json(self): resp = mock.Mock() resp.headers = {'Content-Type': 'application/json'} resp.json.return_value = mock.sentinel.fake_body body = utils.get_response_body(resp) self.assertEqual(mock.sentinel.fake_body, body)