blob: 955526ff307cdb902ab7d5a7ef7a7510697c2718 [file] [log] [blame]
Craig Citro15744b12015-03-02 13:34:32 -08001#!/usr/bin/env python
Ali Afshar2dcc6522010-12-16 10:11:53 +01002#
Craig Citro751b7fb2014-09-23 11:20:38 -07003# Copyright 2014 Google Inc. All Rights Reserved.
Ali Afshar2dcc6522010-12-16 10:11:53 +01004#
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 Naokid898a372015-03-04 03:52:46 +090019from __future__ import absolute_import
Ali Afshar2dcc6522010-12-16 10:11:53 +010020
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070021__author__ = "afshar@google.com (Ali Afshar)"
Ali Afshar2dcc6522010-12-16 10:11:53 +010022
23
Pat Ferate497a90f2015-03-09 09:52:54 -070024import unittest2 as unittest
Ali Afshar2dcc6522010-12-16 10:11:53 +010025import httplib2
26
27
John Asmuth864311d2014-04-24 15:46:08 -040028from googleapiclient.errors import HttpError
Ali Afshar2dcc6522010-12-16 10:11:53 +010029
30
INADA Naoki09157612015-03-25 01:51:03 +090031JSON_ERROR_CONTENT = b"""
Ali Afshar2dcc6522010-12-16 10:11:53 +010032{
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 CHUf6e26612017-07-17 18:02:18 +020044 "message": "country is required",
45 "details": "error details"
Ali Afshar2dcc6522010-12-16 10:11:53 +010046 }
47}
48"""
49
Muad Mohameda341c5a2020-11-08 20:30:02 +000050JSON_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 Kim66bb32c2019-10-30 10:11:58 -070068
69def fake_response(data, headers, reason="Ok"):
70 response = httplib2.Response(headers)
71 response.reason = reason
72 return response, data
Joe Gregorio49396552011-03-08 10:39:00 -050073
74
75class Error(unittest.TestCase):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070076 """Test handling of error bodies."""
Joe Gregorio49396552011-03-08 10:39:00 -050077
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070078 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")
William Marquardtdb2a7662021-03-17 16:02:04 -030086 self.assertEqual(error.error_details, "error details")
87 self.assertEqual(error.status_code, 400)
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070088 self.assertEqual(
89 str(error),
90 '<HttpError 400 when requesting http://example.org returned "country is required". Details: "error details">',
91 )
Joe Gregorio49396552011-03-08 10:39:00 -050092
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070093 def test_bad_json_body(self):
94 """Test handling of bodies with invalid json."""
95 resp, content = fake_response(
96 b"{", {"status": "400", "content-type": "application/json"}, reason="Failed"
97 )
98 error = HttpError(resp, content)
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -050099 self.assertEqual(str(error), '<HttpError 400 when requesting None returned "Failed". Details: "{">')
Joe Gregorio49396552011-03-08 10:39:00 -0500100
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700101 def test_with_uri(self):
102 """Test handling of passing in the request uri."""
103 resp, content = fake_response(
104 b"{",
105 {"status": "400", "content-type": "application/json"},
106 reason="Failure",
107 )
108 error = HttpError(resp, content, uri="http://example.org")
109 self.assertEqual(
110 str(error),
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -0500111 '<HttpError 400 when requesting http://example.org returned "Failure". Details: "{">',
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700112 )
Joe Gregorio49396552011-03-08 10:39:00 -0500113
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700114 def test_missing_message_json_body(self):
115 """Test handling of bodies with missing expected 'message' element."""
116 resp, content = fake_response(
117 b"{}",
118 {"status": "400", "content-type": "application/json"},
119 reason="Failed",
120 )
121 error = HttpError(resp, content)
122 self.assertEqual(str(error), '<HttpError 400 "Failed">')
Joe Gregorio49396552011-03-08 10:39:00 -0500123
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700124 def test_non_json(self):
125 """Test handling of non-JSON bodies"""
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -0500126 resp, content = fake_response(b"Invalid request", {"status": "400"})
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700127 error = HttpError(resp, content)
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -0500128 self.assertEqual(str(error), '<HttpError 400 when requesting None returned "Ok". Details: "Invalid request">')
Joe Gregorioe7bbbb92013-02-20 15:37:24 -0500129
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700130 def test_missing_reason(self):
131 """Test an empty dict with a missing resp.reason."""
132 resp, content = fake_response(b"}NOT OK", {"status": "400"}, reason=None)
133 error = HttpError(resp, content)
Anthonios Parthenioue6a1da32020-12-09 17:00:03 -0500134 self.assertEqual(str(error), '<HttpError 400 when requesting None returned "". Details: "}NOT OK">')
Muad Mohameda341c5a2020-11-08 20:30:02 +0000135
136 def test_error_detail_for_missing_message_in_error(self):
137 """Test handling of data with missing 'details' or 'detail' element."""
138 resp, content = fake_response(
139 JSON_ERROR_CONTENT_NO_DETAIL,
140 {"status": "400", "content-type": "application/json"},
141 reason="Failed",
142 )
143 error = HttpError(resp, content)
vinay-googlea5d20812021-04-03 03:00:05 -0700144 expected_error_details = "[{'domain': 'global', 'reason': 'required', 'message': 'country is required', 'locationType': 'parameter', 'location': 'country'}]"
145 self.assertEqual(str(error), '<HttpError 400 when requesting None returned "country is required". Details: "%s">' % expected_error_details)
146 self.assertEqual(str(error.error_details), expected_error_details)