def test_302_location_no_endpoint(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, ''), (fake2, jsonutils.dumps({'Mount': 'Fuji'}))] client = http.SessionClient(session=mock.ANY, auth=mock.ANY) resp = client.request('', 'GET', redirect=True) self.assertEqual(200, resp.status_code) self.assertEqual({'Mount': 'Fuji'}, utils.get_response_body(resp)) 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-heatclient', 'raise_exc': False, 'redirect': True }, call[1])
def test_http_json_request_redirect(self, mock_request): # Record the 302 mock_request.side_effect = [ fakes.FakeHTTPResponse(302, 'Found', {'location': 'http://example.com:8004'}, ''), fakes.FakeHTTPResponse(200, 'OK', {'content-type': 'application/json'}, '{}') ] client = http.HTTPClient('http://example.com:8004') resp, body = client.json_request('GET', '') self.assertEqual(200, resp.status_code) self.assertEqual({}, body) mock_request.assert_has_calls([ mock.call('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }), mock.call('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }) ])
def test_http_manual_redirect_put_uppercase(self, mock_request): mock_request.side_effect = [ fakes.FakeHTTPResponse( 302, 'Found', {'location': 'http://example.com:8004/foo/bar'}, ''), fakes.FakeHTTPResponse(200, 'OK', {'content-type': 'application/json'}, 'invalid-json') ] client = http.HTTPClient('http://EXAMPLE.com:8004/foo') resp, body = client.json_request('PUT', '') self.assertEqual(200, resp.status_code) mock_request.assert_has_calls([ mock.call('PUT', 'http://EXAMPLE.com:8004/foo', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }), mock.call('PUT', 'http://example.com:8004/foo/bar', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }) ])
def test_http_json_request_redirect(self): # Record the 302 mock_conn = http.requests.request('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }) mock_conn.AndReturn( fakes.FakeHTTPResponse(302, 'Found', {'location': 'http://example.com:8004'}, '')) # Record the following 200 mock_conn = http.requests.request('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }) mock_conn.AndReturn( fakes.FakeHTTPResponse(200, 'OK', {'content-type': 'application/json'}, '{}')) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') resp, body = client.json_request('GET', '') self.assertEqual(200, resp.status_code) self.assertEqual({}, body)
def test_http_manual_redirect_delete(self): mock_conn = http.requests.request( 'DELETE', 'http://example.com:8004/foo', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'}) mock_conn.AndReturn( fakes.FakeHTTPResponse( 302, 'Found', {'location': 'http://example.com:8004/foo/bar'}, '')) mock_conn = http.requests.request( 'DELETE', 'http://example.com:8004/foo/bar', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'}) mock_conn.AndReturn( fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/json'}, '{}')) self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004/foo') resp, body = client.json_request('DELETE', '') self.assertEqual(200, resp.status_code)
def setUp(self): super(BuildInfoManagerTest, self).setUp() self.client = mock.Mock() self.client.get.return_value = fakes.FakeHTTPResponse( 200, None, {'content-type': 'application/json'}, jsonutils.dumps('body')) self.manager = build_info.BuildInfoManager(self.client)
def test_include_pass(self): # Record a 200 fake200 = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/octet-stream'}, '') # no token or credentials mock_conn = http.requests.request('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient' }) mock_conn.AndReturn(fake200) # credentials mock_conn = http.requests.request('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient', 'X-Auth-Key': 'pass', 'X-Auth-User': '******' }) mock_conn.AndReturn(fake200) # token suppresses credentials mock_conn = http.requests.request('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient', 'X-Auth-Token': 'abcd1234', 'X-Auth-Key': 'pass', 'X-Auth-User': '******' }) mock_conn.AndReturn(fake200) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code) client.username = '******' client.password = '******' client.include_pass = True resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code) client.auth_token = 'abcd1234' resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code)
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 = client.request('', 'GET', **kwargs) self.assertEqual( { 'endpoint_override': 'http://no.where/', 'data': "{'files': test}}", 'headers': { 'Content-Type': 'application/json' }, 'user_agent': 'python-heatclient', 'raise_exc': False }, self.request.call_args[1]) self.assertEqual(200, resp.status_code)
def raw_request(self, *args, **kwargs): assert expect_args == args return fakes.FakeHTTPResponse( '200', '', {}, {})
def test_http_json_request_argument_passed_to_requests(self, mock_request): """Check that we have sent the proper arguments to requests.""" # Record a 200 mock_request.return_value = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/json'}, '{}') client = http.HTTPClient('http://example.com:8004') client.verify_cert = True client.cert_file = 'RANDOM_CERT_FILE' client.key_file = 'RANDOM_KEY_FILE' client.auth_url = 'http://AUTH_URL' resp, body = client.json_request('GET', '', data='text') self.assertEqual(200, resp.status_code) self.assertEqual({}, body) mock_request.assert_called_with('GET', 'http://example.com:8004', allow_redirects=False, cert=('RANDOM_CERT_FILE', 'RANDOM_KEY_FILE'), verify=True, data='"text"', headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Url': 'http://AUTH_URL', 'User-Agent': 'python-heatclient' })
def json_request(self, *args, **kwargs): assert expect_args == args assert expect_kwargs['data'] == kwargs['data'] return fakes.FakeHTTPResponse( '200', '', {'content-type': 'application/json'}, {}), {}
def test_session_simple_request(self): resp = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/octet-stream'}, '') self.request.return_value = (resp, '') client = http.SessionClient(session=mock.ANY, auth=mock.ANY) response = client.request(method='GET', url='') self.assertEqual(200, response.status_code) self.assertEqual('', ''.join([x for x in response.content]))
def test_404_error_response(self): fake = fakes.FakeHTTPResponse( 404, 'FAIL', {'content-type': 'application/octet-stream'}, '') self.request.return_value = (fake, '') client = http.SessionClient(session=mock.ANY, auth=mock.ANY) e = self.assertRaises(exc.HTTPNotFound, client.request, '', 'GET') # Assert that the raised exception can be converted to string self.assertIsNotNone(six.text_type(e))
def test_no_redirect_302_no_location(self): fake = fakes.FakeHTTPResponse(302, 'OK', {'location': 'http://no.where/ishere'}, '') self.request.side_effect = [(fake, '')] client = http.SessionClient(session=mock.ANY, auth=mock.ANY) self.assertEqual(fake, client.request('', 'GET'))
def test_504_error_response(self): # for 504 we don't have specific exception type fake = fakes.FakeHTTPResponse( 504, 'FAIL', {'content-type': 'application/octet-stream'}, '') self.request.return_value = (fake, '') client = http.SessionClient(session=mock.ANY, auth=mock.ANY) e = self.assertRaises(exc.HTTPException, client.request, '', 'GET') self.assertEqual(504, e.code)
def test_session_json_request(self): fake = fakes.FakeHTTPResponse(200, 'OK', {'content-type': 'application/json'}, jsonutils.dumps({'some': 'body'})) self.request.return_value = (fake, {}) client = http.SessionClient(session=mock.ANY, auth=mock.ANY) resp = client.request('', 'GET') self.assertEqual(200, resp.status_code) self.assertEqual({'some': 'body'}, resp.json())
def test_redirect_302_no_location(self): fake = fakes.FakeHTTPResponse(302, 'OK', {}, '') self.request.side_effect = [(fake, '')] client = http.SessionClient(session=mock.ANY, auth=mock.ANY) e = self.assertRaises(exc.InvalidEndpoint, client.request, '', 'GET', redirect=True) self.assertEqual("Location not returned with 302", six.text_type(e))
def test_methods(self): fake = fakes.FakeHTTPResponse(200, 'OK', {'content-type': 'application/json'}, {}) self.request.return_value = (fake, {}) client = http.SessionClient(mock.ANY) methods = [ client.get, client.put, client.post, client.patch, client.delete, client.head ] for method in methods: resp = method('') self.assertEqual(200, resp.status_code)
def test_http_manual_redirect_error_without_location(self, mock_request): mock_request.return_value = fakes.FakeHTTPResponse( 302, 'Found', {}, '') client = http.HTTPClient('http://example.com:8004/foo') self.assertRaises(exc.InvalidEndpoint, client.json_request, 'DELETE', '') mock_request.assert_called_once_with( 'DELETE', 'http://example.com:8004/foo', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'})
def test_http_404_json_request(self, mock_request): # Record a 404 mock_request.return_value = fakes.FakeHTTPResponse( 404, 'OK', {'content-type': 'application/json'}, '{}') client = http.HTTPClient('http://example.com:8004') e = self.assertRaises(exc.HTTPNotFound, client.json_request, 'GET', '') # Assert that the raised exception can be converted to string self.assertIsNotNone(str(e)) mock_request.assert_called_with( 'GET', 'http://example.com:8004', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'})
def test_http_manual_redirect_error_without_location(self): mock_conn = http.requests.request('DELETE', 'http://example.com:8004/foo', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }) mock_conn.AndReturn(fakes.FakeHTTPResponse(302, 'Found', {}, '')) self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004/foo') self.assertRaises(exc.InvalidEndpoint, client.json_request, 'DELETE', '')
def test_http_request_specify_timeout(self, mock_request): mock_request.return_value = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/json'}, '{}') client = http.HTTPClient('http://example.com:8004', timeout='123') resp, body = client.json_request('GET', '') self.assertEqual(200, resp.status_code) mock_request.assert_called_with('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' }, timeout=float(123))
def test_http_raw_request(self, mock_request): headers = {'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient'} # Record a 200 mock_request.return_value = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/octet-stream'}, '') # Replay, create client, assert client = http.HTTPClient('http://example.com:8004') resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code) self.assertEqual('', ''.join([x for x in resp.content])) mock_request.assert_called_with('GET', 'http://example.com:8004', allow_redirects=False, headers=headers)
def test_http_json_request_w_req_body(self, mock_request): # Record a 200 mock_request.return_value = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/json'}, '{}') # Replay, create client, assert client = http.HTTPClient('http://example.com:8004') resp, body = client.json_request('GET', '', body='test-body') self.assertEqual(200, resp.status_code) mock_request.assert_called_with('GET', 'http://example.com:8004', body='test-body', allow_redirects=False, headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient' })
def test_token_or_credentials(self, mock_request): # Record a 200 fake200 = fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/octet-stream'}, '') mock_request.return_value = fake200 # no token or credentials client = http.HTTPClient('http://example.com:8004') resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code) # credentials client.username = '******' client.password = '******' resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code) # token suppresses credentials client.auth_token = 'abcd1234' resp = client.raw_request('GET', '') self.assertEqual(200, resp.status_code) mock_request.assert_has_calls([ mock.call('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient' }), mock.call('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient', 'X-Auth-Key': 'pass', 'X-Auth-User': '******' }), mock.call('GET', 'http://example.com:8004', allow_redirects=False, headers={ 'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient', 'X-Auth-Token': 'abcd1234' }) ])
def test_http_404_json_request(self): # Record a 404 mock_conn = http.requests.request( 'GET', 'http://example.com:8004', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'}) mock_conn.AndReturn( fakes.FakeHTTPResponse( 404, 'OK', {'content-type': 'application/json'}, '{}')) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') e = self.assertRaises(exc.HTTPNotFound, client.json_request, 'GET', '') # Assert that the raised exception can be converted to string self.assertIsNotNone(str(e))
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 = client.request('', 'GET', **kwargs) self.assertEqual( { 'endpoint_override': 'http://no.where/', 'data': '"some_data"', 'user_agent': 'python-heatclient', 'raise_exc': False }, self.request.call_args[1]) self.assertEqual(200, resp.status_code)
def test_http_request_specify_timeout(self): mock_conn = http.requests.request( 'GET', 'http://example.com:8004', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'}, timeout=float(123)) mock_conn.AndReturn( fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'application/json'}, '{}')) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004', timeout='123') resp, body = client.json_request('GET', '') self.assertEqual(200, resp.status_code) self.assertEqual({}, body)
def test_http_json_request_non_json_resp_cont_type(self): # Record a 200 mock_conn = http.requests.request( 'GET', 'http://example.com:8004', body='test-body', allow_redirects=False, headers={'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-heatclient'}) mock_conn.AndReturn( fakes.FakeHTTPResponse( 200, 'OK', {'content-type': 'not/json'}, {})) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') resp, body = client.json_request('GET', '', body='test-body') self.assertEqual(200, resp.status_code) self.assertIsNone(body)
def test_not_include_pass(self, mock_request): # Record a 200 fake500 = fakes.FakeHTTPResponse( 500, 'ERROR', {'content-type': 'application/octet-stream'}, b'(HTTP 401)') # no token or credentials mock_request.return_value = fake500 # Replay, create client, assert client = http.HTTPClient('http://example.com:8004') e = self.assertRaises(exc.HTTPUnauthorized, client.raw_request, 'GET', '') self.assertIn('Authentication failed', str(e)) mock_request.assert_called_with( 'GET', 'http://example.com:8004', allow_redirects=False, headers={'Content-Type': 'application/octet-stream', 'User-Agent': 'python-heatclient'})