def test_200_response_handling(self): # check if 200 response returns a json decoded response client = Client(api_key, api_secret) mock_response = {'body': json.dumps(mock_items), 'status': 200} hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response) response = client._get('test') self.assertEqual(client._handle_response(response), mock_items)
def test_error_response_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 = { 'error': { 'id': 0, 'message': 'fake error message', 'description': 'fake error description' } } for ecode, eclass in six.iteritems(errors._status_code_to_class): mock_response = {'body': json.dumps(error_body), 'status': ecode} hp.register_uri(hp.GET, re.compile('.*' + str(ecode) + '$'), **mock_response) with self.assertRaises(eclass): client._handle_response(client._get(str(ecode))) # check if appropriate error raised even with no error message in body or if content-type is not text/json for ecode, eclass in six.iteritems(errors._status_code_to_class): mock_response = {'status': ecode, 'content_type': 'text/plain'} hp.register_uri(hp.GET, re.compile('.*' + str(ecode) + '$'), **mock_response) with self.assertRaises(eclass): client._handle_response(client._get(str(ecode))) # check if status code is unrecognized, generic APIError is raised mock_response = {'status': 418} hp.register_uri(hp.GET, re.compile('.*test$'), **mock_response) with self.assertRaises(errors.APIError): client._handle_response(client._get('test'))