blob: 3a3c3fd9cf84ed9129c9a70b0c8ef830c17a4f85 [file] [log] [blame]
Ali Afshar2dcc6522010-12-16 10:11:53 +01001#!/usr/bin/python2.4
2#
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"""
19
20__author__ = 'afshar@google.com (Ali Afshar)'
21
22
23import unittest
24import httplib2
25
26
John Asmuth864311d2014-04-24 15:46:08 -040027from googleapiclient.errors import HttpError
Ali Afshar2dcc6522010-12-16 10:11:53 +010028
29
Joe Gregorio49396552011-03-08 10:39:00 -050030JSON_ERROR_CONTENT = """
Ali Afshar2dcc6522010-12-16 10:11:53 +010031{
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 }
45}
46"""
47
Joe Gregorio20b54fb2012-07-26 09:59:35 -040048def fake_response(data, headers, reason='Ok'):
49 response = httplib2.Response(headers)
50 response.reason = reason
51 return response, data
Joe Gregorio49396552011-03-08 10:39:00 -050052
53
54class Error(unittest.TestCase):
55 """Test handling of error bodies."""
56
57 def test_json_body(self):
58 """Test a nicely formed, expected error response."""
59 resp, content = fake_response(JSON_ERROR_CONTENT,
Joe Gregorio20b54fb2012-07-26 09:59:35 -040060 {'status':'400', 'content-type': 'application/json'},
61 reason='Failed')
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040062 error = HttpError(resp, content, uri='http://example.org')
Joe Gregorio20b54fb2012-07-26 09:59:35 -040063 self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required">')
Joe Gregorio49396552011-03-08 10:39:00 -050064
65 def test_bad_json_body(self):
66 """Test handling of bodies with invalid json."""
67 resp, content = fake_response('{',
Joe Gregorio20b54fb2012-07-26 09:59:35 -040068 { 'status':'400', 'content-type': 'application/json'},
69 reason='Failed')
Joe Gregorio49396552011-03-08 10:39:00 -050070 error = HttpError(resp, content)
Joe Gregorio20b54fb2012-07-26 09:59:35 -040071 self.assertEqual(str(error), '<HttpError 400 "Failed">')
Joe Gregorio49396552011-03-08 10:39:00 -050072
73 def test_with_uri(self):
74 """Test handling of passing in the request uri."""
75 resp, content = fake_response('{',
Joe Gregorio20b54fb2012-07-26 09:59:35 -040076 {'status':'400', 'content-type': 'application/json'},
77 reason='Failure')
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040078 error = HttpError(resp, content, uri='http://example.org')
Joe Gregorio20b54fb2012-07-26 09:59:35 -040079 self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "Failure">')
Joe Gregorio49396552011-03-08 10:39:00 -050080
81 def test_missing_message_json_body(self):
82 """Test handling of bodies with missing expected 'message' element."""
83 resp, content = fake_response('{}',
Joe Gregorio20b54fb2012-07-26 09:59:35 -040084 {'status':'400', 'content-type': 'application/json'},
85 reason='Failed')
Joe Gregorio49396552011-03-08 10:39:00 -050086 error = HttpError(resp, content)
Joe Gregorio20b54fb2012-07-26 09:59:35 -040087 self.assertEqual(str(error), '<HttpError 400 "Failed">')
Joe Gregorio49396552011-03-08 10:39:00 -050088
89 def test_non_json(self):
90 """Test handling of non-JSON bodies"""
Joe Gregorio20b54fb2012-07-26 09:59:35 -040091 resp, content = fake_response('}NOT OK', {'status':'400'})
Joe Gregorio49396552011-03-08 10:39:00 -050092 error = HttpError(resp, content)
93 self.assertEqual(str(error), '<HttpError 400 "Ok">')
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050094
95 def test_missing_reason(self):
96 """Test an empty dict with a missing resp.reason."""
Daniel Hermesf7b648f2013-03-06 09:38:53 -080097 resp, content = fake_response('}NOT OK', {'status': '400'}, reason=None)
Joe Gregorioe7bbbb92013-02-20 15:37:24 -050098 error = HttpError(resp, content)
99 self.assertEqual(str(error), '<HttpError 400 "">')