Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 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 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 19 | from __future__ import absolute_import |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 20 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 21 | __author__ = "afshar@google.com (Ali Afshar)" |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 22 | |
| 23 | |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 24 | import unittest2 as unittest |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 25 | import httplib2 |
| 26 | |
| 27 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 28 | from googleapiclient.errors import HttpError |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 29 | |
| 30 | |
INADA Naoki | 0915761 | 2015-03-25 01:51:03 +0900 | [diff] [blame] | 31 | JSON_ERROR_CONTENT = b""" |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 32 | { |
| 33 | "error": { |
| 34 | "errors": [ |
| 35 | { |
| 36 | "domain": "global", |
| 37 | "reason": "required", |
| 38 | "message": "country is required", |
| 39 | "locationType": "parameter", |
| 40 | "location": "country" |
| 41 | } |
| 42 | ], |
| 43 | "code": 400, |
Son CHU | f6e2661 | 2017-07-17 18:02:18 +0200 | [diff] [blame] | 44 | "message": "country is required", |
| 45 | "details": "error details" |
Ali Afshar | 2dcc652 | 2010-12-16 10:11:53 +0100 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | """ |
| 49 | |
Muad Mohamed | a341c5a | 2020-11-08 20:30:02 +0000 | [diff] [blame^] | 50 | JSON_ERROR_CONTENT_NO_DETAIL = b""" |
| 51 | { |
| 52 | "error": { |
| 53 | "errors": [ |
| 54 | { |
| 55 | "domain": "global", |
| 56 | "reason": "required", |
| 57 | "message": "country is required", |
| 58 | "locationType": "parameter", |
| 59 | "location": "country" |
| 60 | } |
| 61 | ], |
| 62 | "code": 400, |
| 63 | "message": "country is required" |
| 64 | } |
| 65 | } |
| 66 | """ |
| 67 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 68 | |
| 69 | def fake_response(data, headers, reason="Ok"): |
| 70 | response = httplib2.Response(headers) |
| 71 | response.reason = reason |
| 72 | return response, data |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class Error(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 76 | """Test handling of error bodies.""" |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 77 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 78 | def test_json_body(self): |
| 79 | """Test a nicely formed, expected error response.""" |
| 80 | resp, content = fake_response( |
| 81 | JSON_ERROR_CONTENT, |
| 82 | {"status": "400", "content-type": "application/json"}, |
| 83 | reason="Failed", |
| 84 | ) |
| 85 | error = HttpError(resp, content, uri="http://example.org") |
| 86 | self.assertEqual( |
| 87 | str(error), |
| 88 | '<HttpError 400 when requesting http://example.org returned "country is required". Details: "error details">', |
| 89 | ) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 90 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 91 | def test_bad_json_body(self): |
| 92 | """Test handling of bodies with invalid json.""" |
| 93 | resp, content = fake_response( |
| 94 | b"{", {"status": "400", "content-type": "application/json"}, reason="Failed" |
| 95 | ) |
| 96 | error = HttpError(resp, content) |
| 97 | self.assertEqual(str(error), '<HttpError 400 "Failed">') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 98 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 99 | def test_with_uri(self): |
| 100 | """Test handling of passing in the request uri.""" |
| 101 | resp, content = fake_response( |
| 102 | b"{", |
| 103 | {"status": "400", "content-type": "application/json"}, |
| 104 | reason="Failure", |
| 105 | ) |
| 106 | error = HttpError(resp, content, uri="http://example.org") |
| 107 | self.assertEqual( |
| 108 | str(error), |
| 109 | '<HttpError 400 when requesting http://example.org returned "Failure">', |
| 110 | ) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 111 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 112 | def test_missing_message_json_body(self): |
| 113 | """Test handling of bodies with missing expected 'message' element.""" |
| 114 | resp, content = fake_response( |
| 115 | b"{}", |
| 116 | {"status": "400", "content-type": "application/json"}, |
| 117 | reason="Failed", |
| 118 | ) |
| 119 | error = HttpError(resp, content) |
| 120 | self.assertEqual(str(error), '<HttpError 400 "Failed">') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 121 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 122 | def test_non_json(self): |
| 123 | """Test handling of non-JSON bodies""" |
| 124 | resp, content = fake_response(b"}NOT OK", {"status": "400"}) |
| 125 | error = HttpError(resp, content) |
| 126 | self.assertEqual(str(error), '<HttpError 400 "Ok">') |
Joe Gregorio | e7bbbb9 | 2013-02-20 15:37:24 -0500 | [diff] [blame] | 127 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 128 | def test_missing_reason(self): |
| 129 | """Test an empty dict with a missing resp.reason.""" |
| 130 | resp, content = fake_response(b"}NOT OK", {"status": "400"}, reason=None) |
| 131 | error = HttpError(resp, content) |
| 132 | self.assertEqual(str(error), '<HttpError 400 "">') |
Muad Mohamed | a341c5a | 2020-11-08 20:30:02 +0000 | [diff] [blame^] | 133 | |
| 134 | def test_error_detail_for_missing_message_in_error(self): |
| 135 | """Test handling of data with missing 'details' or 'detail' element.""" |
| 136 | resp, content = fake_response( |
| 137 | JSON_ERROR_CONTENT_NO_DETAIL, |
| 138 | {"status": "400", "content-type": "application/json"}, |
| 139 | reason="Failed", |
| 140 | ) |
| 141 | error = HttpError(resp, content) |
| 142 | self.assertEqual(str(error), '<HttpError 400 when requesting None returned "country is required". Details: "country is required">') |
| 143 | self.assertEqual(error.error_details, 'country is required') |