Enhanced HttpError to now include the original request URI if possible.
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index e89fe9f..ecb59f9 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -35,6 +35,7 @@
from apiclient.discovery import build, key2param
from apiclient.http import HttpMock
from apiclient.errors import HttpError
+from apiclient.errors import InvalidJsonError
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
@@ -56,8 +57,8 @@
try:
buzz = build('buzz', 'v1', self.http)
self.fail("should have raised an exception over malformed JSON.")
- except HttpError, e:
- self.assertEqual(e.content, "{\n")
+ except InvalidJsonError:
+ pass
class Discovery(unittest.TestCase):
diff --git a/tests/test_errors.py b/tests/test_errors.py
index 44b2a14..ab0be28 100644
--- a/tests/test_errors.py
+++ b/tests/test_errors.py
@@ -27,42 +27,7 @@
from apiclient.errors import HttpError
-def fake_response(data, headers):
- return httplib2.Response(headers), data
-
-
-class Error(unittest.TestCase):
- """Test handling of error bodies."""
-
- def test_json_body(self):
- """Test a nicely formed, expected error response."""
- resp, content = fake_response(json_error_content,
- {'status':'400', 'content-type': 'application/json'})
- error = HttpError(resp, content)
- self.assertEqual(str(error), '<HttpError 400 "country is required">')
-
- def test_bad_json_body(self):
- """Test handling of bodies with invalid json."""
- resp, content = fake_response('{',
- {'status':'400', 'content-type': 'application/json'})
- error = HttpError(resp, content)
- self.assertEqual(str(error), '<HttpError 400 "{">')
-
- def test_missing_message_json_body(self):
- """Test handling of bodies with missing expected 'message' element."""
- resp, content = fake_response('{}',
- {'status':'400', 'content-type': 'application/json'})
- error = HttpError(resp, content)
- self.assertEqual(str(error), '<HttpError 400 "{}">')
-
- def test_non_json(self):
- """Test handling of non-JSON bodies"""
- resp, content = fake_response('NOT OK', {'status':'400'})
- error = HttpError(resp, content)
- self.assertEqual(str(error), '<HttpError 400 "Ok">')
-
-
-json_error_content = """
+JSON_ERROR_CONTENT = """
{
"error": {
"errors": [
@@ -80,3 +45,43 @@
}
"""
+def fake_response(data, headers):
+ return httplib2.Response(headers), data
+
+
+class Error(unittest.TestCase):
+ """Test handling of error bodies."""
+
+ def test_json_body(self):
+ """Test a nicely formed, expected error response."""
+ resp, content = fake_response(JSON_ERROR_CONTENT,
+ {'status':'400', 'content-type': 'application/json'})
+ error = HttpError(resp, content)
+ self.assertEqual(str(error), '<HttpError 400 "country is required">')
+
+ def test_bad_json_body(self):
+ """Test handling of bodies with invalid json."""
+ resp, content = fake_response('{',
+ {'status':'400', 'content-type': 'application/json'})
+ error = HttpError(resp, content)
+ self.assertEqual(str(error), '<HttpError 400 "{">')
+
+ def test_with_uri(self):
+ """Test handling of passing in the request uri."""
+ resp, content = fake_response('{',
+ {'status':'400', 'content-type': 'application/json'})
+ error = HttpError(resp, content, 'http://example.org')
+ self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "{">')
+
+ def test_missing_message_json_body(self):
+ """Test handling of bodies with missing expected 'message' element."""
+ resp, content = fake_response('{}',
+ {'status':'400', 'content-type': 'application/json'})
+ error = HttpError(resp, content)
+ self.assertEqual(str(error), '<HttpError 400 "{}">')
+
+ def test_non_json(self):
+ """Test handling of non-JSON bodies"""
+ resp, content = fake_response('NOT OK', {'status':'400'})
+ error = HttpError(resp, content)
+ self.assertEqual(str(error), '<HttpError 400 "Ok">')