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 | |
| 21 | __author__ = 'afshar@google.com (Ali Afshar)' |
| 22 | |
| 23 | |
| 24 | import unittest |
| 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 | |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 31 | JSON_ERROR_CONTENT = """ |
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, |
| 44 | "message": "country is required" |
| 45 | } |
| 46 | } |
| 47 | """ |
| 48 | |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 49 | def fake_response(data, headers, reason='Ok'): |
| 50 | response = httplib2.Response(headers) |
| 51 | response.reason = reason |
| 52 | return response, data |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class 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 Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 61 | {'status':'400', 'content-type': 'application/json'}, |
| 62 | reason='Failed') |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 63 | error = HttpError(resp, content, uri='http://example.org') |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 64 | self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required">') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 65 | |
| 66 | def test_bad_json_body(self): |
| 67 | """Test handling of bodies with invalid json.""" |
| 68 | resp, content = fake_response('{', |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 69 | { 'status':'400', 'content-type': 'application/json'}, |
| 70 | reason='Failed') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 71 | error = HttpError(resp, content) |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 72 | self.assertEqual(str(error), '<HttpError 400 "Failed">') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 73 | |
| 74 | def test_with_uri(self): |
| 75 | """Test handling of passing in the request uri.""" |
| 76 | resp, content = fake_response('{', |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 77 | {'status':'400', 'content-type': 'application/json'}, |
| 78 | reason='Failure') |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 79 | error = HttpError(resp, content, uri='http://example.org') |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 80 | self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "Failure">') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 81 | |
| 82 | def test_missing_message_json_body(self): |
| 83 | """Test handling of bodies with missing expected 'message' element.""" |
| 84 | resp, content = fake_response('{}', |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 85 | {'status':'400', 'content-type': 'application/json'}, |
| 86 | reason='Failed') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 87 | error = HttpError(resp, content) |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 88 | self.assertEqual(str(error), '<HttpError 400 "Failed">') |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 89 | |
| 90 | def test_non_json(self): |
| 91 | """Test handling of non-JSON bodies""" |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 92 | resp, content = fake_response('}NOT OK', {'status':'400'}) |
Joe Gregorio | 4939655 | 2011-03-08 10:39:00 -0500 | [diff] [blame] | 93 | error = HttpError(resp, content) |
| 94 | self.assertEqual(str(error), '<HttpError 400 "Ok">') |
Joe Gregorio | e7bbbb9 | 2013-02-20 15:37:24 -0500 | [diff] [blame] | 95 | |
| 96 | def test_missing_reason(self): |
| 97 | """Test an empty dict with a missing resp.reason.""" |
Daniel Hermes | f7b648f | 2013-03-06 09:38:53 -0800 | [diff] [blame] | 98 | resp, content = fake_response('}NOT OK', {'status': '400'}, reason=None) |
Joe Gregorio | e7bbbb9 | 2013-02-20 15:37:24 -0500 | [diff] [blame] | 99 | error = HttpError(resp, content) |
| 100 | self.assertEqual(str(error), '<HttpError 400 "">') |