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