blob: fbb477824786889c31d9d2048c35584b4a4800cb [file] [log] [blame]
Ali Afshar2dcc6522010-12-16 10:11:53 +01001#!/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
23import unittest
24import httplib2
25
26
27from apiclient.errors import HttpError
28
29
30def fake_response(data, headers):
31 return httplib2.Response(headers), data
32
33
34class Error(unittest.TestCase):
35 """Test handling of error bodies
36 """
37
38 def test_json_body(self):
39 """Test a nicely formed, expected error response
40 """
41 resp, content = fake_response(json_error_content,
42 {'status':'400', 'content-type': 'application/json'})
43 error = HttpError(resp, content)
44 self.assertEqual(str(error), '<HttpError 400 "country is required">')
45
46 def test_bad_json_body(self):
47 """Test handling of bodies with invalid json
48 """
49 resp, content = fake_response('{',
50 {'status':'400', 'content-type': 'application/json'})
51 error = HttpError(resp, content)
52 self.assertEqual(str(error), '<HttpError 400 "{">')
53
54 def test_missing_message_json_body(self):
55 """Test handling of bodies with missing expected 'message' element
56 """
57 resp, content = fake_response('{}',
58 {'status':'400', 'content-type': 'application/json'})
59 error = HttpError(resp, content)
60 self.assertEqual(str(error), '<HttpError 400 "{}">')
61
62 def test_non_json(self):
63 """Test handling of non-JSON bodies
64 """
65 resp, content = fake_response('NOT OK', {'status':'400'})
66 error = HttpError(resp, content)
67 self.assertEqual(str(error), '<HttpError 400 "Ok">')
68
69
70json_error_content = """
71{
72 "error": {
73 "errors": [
74 {
75 "domain": "global",
76 "reason": "required",
77 "message": "country is required",
78 "locationType": "parameter",
79 "location": "country"
80 }
81 ],
82 "code": 400,
83 "message": "country is required"
84 }
85}
86"""
87