def test_response_error_handling(self): client = Client(api_key, api_secret) # check if appropriate error is raised depending on status code # AND if error data is in response, it is used error_body = {'success':0, 'error': 'some error message'} mock_response = {'body':json.dumps(error_body), 'status':200} hp.register_uri(hp.GET,re.compile('.*test$'),**mock_response) with self.assertRaises(errors.APIResponseError): client._handle_response(client._api_uri.get('test'))
def test_server_error_handling(self): client = Client(api_key, api_secret) for ecode,eclass in six.iteritems(errors._status_code_to_class): mock_response = {'body':json.dumps('{}'), 'status':ecode} hp.register_uri(hp.GET,re.compile('.*'+str(ecode)+'$'),**mock_response) with self.assertRaises(eclass): client._handle_response(client._api_uri.get(str(ecode))) # check if status code is unrecognized, generic APIServerError is raised mock_response = {'status':418} hp.register_uri(hp.GET,re.compile('.*test$'),**mock_response) with self.assertRaises(errors.APIServerError): client._handle_response(client._api_uri.get('test'))
def test_200_response_handling(self): # check if 200 response returns the json data client = Client(api_key, api_secret) mock_response = {'body':json.dumps(mock_collection), 'status':200} hp.register_uri(hp.GET,re.compile('.*test$'),**mock_response) response = client._api_uri.get('test') self.assertEqual(client._handle_response(response),mock_collection) # check if 200 response with success==1 also returns the json data mock_response = {'body':json.dumps({'success':1, 'return':mock_items}), 'status':200} hp.register_uri(hp.GET,re.compile('.*test$'),**mock_response) response = client._api_uri.get('test') self.assertEqual(client._handle_response(response),mock_items)