blob: 0cef8923c23e2df0fdcd2c477a20bfaf568ce21c [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
21__author__ = 'afshar@google.com (Ali Afshar)'
22
23
24import unittest
25import httplib2
26
27
John Asmuth864311d2014-04-24 15:46:08 -040028from googleapiclient.errors import HttpError
Ali Afshar2dcc6522010-12-16 10:11:53 +010029
30
Joe Gregorio49396552011-03-08 10:39:00 -050031JSON_ERROR_CONTENT = """
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,
44 "message": "country is required"
45 }
46}
47"""
48
Joe Gregorio20b54fb2012-07-26 09:59:35 -040049def fake_response(data, headers, reason='Ok'):
50 response = httplib2.Response(headers)
51 response.reason = reason
52 return response, data
Joe Gregorio49396552011-03-08 10:39:00 -050053
54
55class Error(unittest.TestCase):
56 """Test handling of error bodies."""
57
58 def test_json_body(self):
59 """Test a nicely formed, expected error response."""
60 resp, content = fake_response(JSON_ERROR_CONTENT,
Joe Gregorio20b54fb2012-07-26 09:59:35 -040061 {'status':'400', 'content-type': 'application/json'},
62 reason='Failed')
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040063 error = HttpError(resp, content, uri='http://example.org')
Joe Gregorio20b54fb2012-07-26 09:59:35 -040064 self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required">')
Joe Gregorio49396552011-03-08 10:39:00 -050065
66 def test_bad_json_body(self):
67 """Test handling of bodies with invalid json."""
68 resp, content = fake_response('{',
Joe Gregorio20b54fb2012-07-26 09:59:35 -040069 { 'status':'400', 'content-type': 'application/json'},
70 reason='Failed')
Joe Gregorio49396552011-03-08 10:39:00 -050071 error = HttpError(resp, content)
Joe Gregorio20b54fb2012-07-26 09:59:35 -040072 self.assertEqual(str(error), '<HttpError 400 "Failed">')
Joe Gregorio49396552011-03-08 10:39:00 -050073
74 def test_with_uri(self):
75 """Test handling of passing in the request uri."""
76 resp, content = fake_response('{',
Joe Gregorio20b54fb2012-07-26 09:59:35 -040077 {'status':'400', 'content-type': 'application/json'},
78 reason='Failure')
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040079 error = HttpError(resp, content, uri='http://example.org')
Joe Gregorio20b54fb2012-07-26 09:59:35 -040080 self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "Failure">')
Joe Gregorio49396552011-03-08 10:39:00 -050081
82 def test_missing_message_json_body(self):
83 """Test handling of bodies with missing expected 'message' element."""
84 resp, content = fake_response('{}',
Joe Gregorio20b54fb2012-07-26 09:59:35 -040085 {'status':'400', 'content-type': 'application/json'},
86 reason='Failed')
Joe Gregorio49396552011-03-08 10:39:00 -050087 error = HttpError(resp, content)
Joe Gregorio20b54fb2012-07-26 09:59:35 -040088 self.assertEqual(str(error), '<HttpError 400 "Failed">')
Joe Gregorio49396552011-03-08 10:39:00 -050089
90 def test_non_json(self):
91 """Test handling of non-JSON bodies"""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040092 resp, content = fake_response('}NOT OK', {'status':'400'})
Joe Gregorio49396552011-03-08 10:39:00 -050093 error = HttpError(resp, content)
94 self.assertEqual(str(error), '<HttpError 400 "Ok">')
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050095
96 def test_missing_reason(self):
97 """Test an empty dict with a missing resp.reason."""
Daniel Hermesf7b648f2013-03-06 09:38:53 -080098 resp, content = fake_response('}NOT OK', {'status': '400'}, reason=None)
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050099 error = HttpError(resp, content)
100 self.assertEqual(str(error), '<HttpError 400 "">')