def test_http_404_json_request(self): # Record a 404 mock_conn = http.httplib.HTTPConnection('example.com', 8004, '', timeout=600.0) mock_conn.request('GET', '/', headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-dragonclient' }) mock_conn.getresponse().AndReturn( fakes.FakeHTTPResponse(404, 'OK', {'content-type': 'application/json'}, '{}')) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') self.assertRaises(exc.HTTPNotFound, client.json_request, 'GET', '') self.m.VerifyAll()
def test_http_json_request_redirect(self): # Record the 302 mock_conn = http.httplib.HTTPConnection('example.com', 8004, '', timeout=600.0) mock_conn.request('GET', '/', headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-dragonclient' }) mock_conn.getresponse().AndReturn( fakes.FakeHTTPResponse(302, 'Found', {'location': 'http://example.com:8004'}, '')) # Record the following 200 mock_conn = http.httplib.HTTPConnection('example.com', 8004, '', timeout=600.0) mock_conn.request('GET', '/', headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-dragonclient' }) mock_conn.getresponse().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(resp.status, 200) self.assertEqual(body, {}) self.m.VerifyAll()
def test_http_json_request_prohibited_redirect(self): # Record the 302 mock_conn = http.httplib.HTTPConnection('example.com', 8004, '', timeout=600.0) mock_conn.request('GET', '/', headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-dragonclient' }) mock_conn.getresponse().AndReturn( fakes.FakeHTTPResponse( 302, 'Found', {'location': 'http://prohibited.example' '.com:8004'}, '')) # Replay, create client, assert self.m.ReplayAll() client = http.HTTPClient('http://example.com:8004') self.assertRaises(exc.InvalidEndpoint, client.json_request, 'GET', '') self.m.VerifyAll()
def test_http_json_request_non_json_resp_cont_type(self): # Record a 200 mock_conn = http.httplib.HTTPConnection('example.com', 8004, '', timeout=600.0) mock_conn.request('GET', '/', body='"test-body"', headers={ 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-dragonclient' }) mock_conn.getresponse().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(resp.status, 200) self.assertEqual(body, None) self.m.VerifyAll()