Debian Codegraph @

tests/test_errors.py

raw
1 #!/usr/bin/env python
2 #
3 # Copyright 2014 Google Inc. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """Tests for errors handling
18 """
19 from __future__ import absolute_import
20
21 __author__ = "[email protected] (Ali Afshar)"
22
23
24 import unittest
25
26 import httplib2
27
28 from googleapiclient.errors import HttpError
29
30 JSON_ERROR_CONTENT = b"""
31 {
32 "error": {
33 "errors": [
34 {
35 "domain": "global",
36 "reason": "required",
37 "message": "country is required",
38 "locationType": "parameter",
39 "location": "country"
40 }
41 ],
42 "code": 400,
43 "message": "country is required",
44 "details": "error details"
45 }
46 }
47 """
48
49 JSON_ERROR_CONTENT_NO_DETAIL = b"""
50 {
51 "error": {
52 "errors": [
53 {
54 "domain": "global",
55 "reason": "required",
56 "message": "country is required",
57 "locationType": "parameter",
58 "location": "country"
59 }
60 ],
61 "code": 400,
62 "message": "country is required"
63 }
64 }
65 """
66
67
68 def fake_response(data, headers, reason="Ok"):
69 response = httplib2.Response(headers)
70 response.reason = reason
71 return response, data
72
73
74 class Error(unittest.TestCase):
75 """Test handling of error bodies."""
76
77 def test_json_body(self):
78 """Test a nicely formed, expected error response."""
79 resp, content = fake_response(
80 JSON_ERROR_CONTENT,
81 {"status": "400", "content-type": "application/json"},
82 reason="Failed",
83 )
84 error = HttpError(resp, content, uri="http://example.org")
85 self.assertEqual(error.error_details, "error details")
86 self.assertEqual(error.status_code, 400)
87 self.assertEqual(
88 str(error),
89 '<HttpError 400 when requesting http://example.org returned "country is required". Details: "error details">',
90 )
91
92 def test_bad_json_body(self):
93 """Test handling of bodies with invalid json."""
94 resp, content = fake_response(
95 b"{", {"status": "400", "content-type": "application/json"}, reason="Failed"
96 )
97 error = HttpError(resp, content)
98 self.assertEqual(
99 str(error),
100 '<HttpError 400 when requesting None returned "Failed". Details: "{">',
101 )
102
103 def test_with_uri(self):
104 """Test handling of passing in the request uri."""
105 resp, content = fake_response(
106 b"{",
107 {"status": "400", "content-type": "application/json"},
108 reason="Failure",
109 )
110 error = HttpError(resp, content, uri="http://example.org")
111 self.assertEqual(
112 str(error),
113 '<HttpError 400 when requesting http://example.org returned "Failure". Details: "{">',
114 )
115
116 def test_missing_message_json_body(self):
117 """Test handling of bodies with missing expected 'message' element."""
118 resp, content = fake_response(
119 b"{}",
120 {"status": "400", "content-type": "application/json"},
121 reason="Failed",
122 )
123 error = HttpError(resp, content)
124 self.assertEqual(str(error), '<HttpError 400 "Failed">')
125
126 def test_non_json(self):
127 """Test handling of non-JSON bodies"""
128 resp, content = fake_response(b"Invalid request", {"status": "400"})
129 error = HttpError(resp, content)
130 self.assertEqual(
131 str(error),
132 '<HttpError 400 when requesting None returned "Ok". Details: "Invalid request">',
133 )
134
135 def test_missing_reason(self):
136 """Test an empty dict with a missing resp.reason."""
137 resp, content = fake_response(b"}NOT OK", {"status": "400"}, reason=None)
138 error = HttpError(resp, content)
139 self.assertEqual(
140 str(error),
141 '<HttpError 400 when requesting None returned "". Details: "}NOT OK">',
142 )
143
144 def test_error_detail_for_missing_message_in_error(self):
145 """Test handling of data with missing 'details' or 'detail' element."""
146 resp, content = fake_response(
147 JSON_ERROR_CONTENT_NO_DETAIL,
148 {"status": "400", "content-type": "application/json"},
149 reason="Failed",
150 )
151 error = HttpError(resp, content)
152 expected_error_details = "[{'domain': 'global', 'reason': 'required', 'message': 'country is required', 'locationType': 'parameter', 'location': 'country'}]"
153 self.assertEqual(
154 str(error),
155 '<HttpError 400 when requesting None returned "country is required". Details: "%s">'
156 % expected_error_details,
157 )
158 self.assertEqual(str(error.error_details), expected_error_details)