blob: fc92654fa4f40f409ccf0c0f1fcef4e54d80b51f [file] [log] [blame]
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001#!/usr/bin/python2.4
2#
Joe Gregorio6d5e94f2010-08-25 23:49:30 -04003# 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.
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040016
Joe Gregorio6d5e94f2010-08-25 23:49:30 -040017"""JSON Model tests
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040018
Joe Gregorio6d5e94f2010-08-25 23:49:30 -040019Unit tests for the JSON model.
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040020"""
21
22__author__ = 'jcgregorio@google.com (Joe Gregorio)'
23
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040024import os
25import unittest
Joe Gregorioc5c5a372010-09-22 11:42:32 -040026import httplib2
ade@google.comd69e5e42010-08-31 15:28:20 +010027
Joe Gregorioe1de4162011-02-23 11:30:29 -050028from apiclient.model import JsonModel
29from apiclient.errors import HttpError
30
ade@google.comd69e5e42010-08-31 15:28:20 +010031# Python 2.5 requires different modules
32try:
33 from urlparse import parse_qs
34except ImportError:
35 from cgi import parse_qs
36
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040037
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040038class Model(unittest.TestCase):
39 def test_json_no_body(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -050040 model = JsonModel(data_wrapper=False)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040041
42 headers = {}
ade@google.com850cf552010-08-20 23:24:56 +010043 path_params = {}
44 query_params = {}
45 body = None
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040046
ade@google.com850cf552010-08-20 23:24:56 +010047 headers, params, query, body = model.request(headers, path_params, query_params, body)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040048
49 self.assertEqual(headers['accept'], 'application/json')
50 self.assertTrue('content-type' not in headers)
51 self.assertNotEqual(query, '')
52 self.assertEqual(body, None)
53
54 def test_json_body(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -050055 model = JsonModel(data_wrapper=False)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040056
57 headers = {}
ade@google.com850cf552010-08-20 23:24:56 +010058 path_params = {}
59 query_params = {}
60 body = {}
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040061
ade@google.com850cf552010-08-20 23:24:56 +010062 headers, params, query, body = model.request(headers, path_params, query_params, body)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040063
64 self.assertEqual(headers['accept'], 'application/json')
65 self.assertEqual(headers['content-type'], 'application/json')
66 self.assertNotEqual(query, '')
Joe Gregorio913e70d2010-11-05 15:38:23 -040067 self.assertEqual(body, '{}')
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040068
Joe Gregoriod433b2a2011-02-22 10:51:51 -050069 def test_json_body_data_wrapper(self):
70 model = JsonModel(data_wrapper=True)
71
72 headers = {}
73 path_params = {}
74 query_params = {}
75 body = {}
76
77 headers, params, query, body = model.request(headers, path_params, query_params, body)
78
79 self.assertEqual(headers['accept'], 'application/json')
80 self.assertEqual(headers['content-type'], 'application/json')
81 self.assertNotEqual(query, '')
82 self.assertEqual(body, '{"data": {}}')
83
Joe Gregorio8963ff92010-10-11 13:14:43 -040084 def test_json_body_default_data(self):
85 """Test that a 'data' wrapper doesn't get added if one is already present."""
Joe Gregoriod433b2a2011-02-22 10:51:51 -050086 model = JsonModel(data_wrapper=True)
Joe Gregorio8963ff92010-10-11 13:14:43 -040087
88 headers = {}
89 path_params = {}
90 query_params = {}
91 body = {'data': 'foo'}
92
93 headers, params, query, body = model.request(headers, path_params, query_params, body)
94
95 self.assertEqual(headers['accept'], 'application/json')
96 self.assertEqual(headers['content-type'], 'application/json')
97 self.assertNotEqual(query, '')
98 self.assertEqual(body, '{"data": "foo"}')
99
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400100 def test_json_build_query(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500101 model = JsonModel(data_wrapper=False)
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400102
103 headers = {}
104 path_params = {}
Joe Gregorio61d7e962011-02-22 22:52:07 -0500105 query_params = {'foo': 1, 'bar': u'\N{COMET}',
106 'baz': ['fe', 'fi', 'fo', 'fum'], # Repeated parameters
107 'qux': []}
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400108 body = {}
109
110 headers, params, query, body = model.request(headers, path_params, query_params, body)
111
112 self.assertEqual(headers['accept'], 'application/json')
113 self.assertEqual(headers['content-type'], 'application/json')
114
Joe Gregorio61d7e962011-02-22 22:52:07 -0500115 query_dict = parse_qs(query[1:])
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400116 self.assertEqual(query_dict['foo'], ['1'])
117 self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')])
Joe Gregorio61d7e962011-02-22 22:52:07 -0500118 self.assertEqual(query_dict['baz'], ['fe', 'fi', 'fo', 'fum'])
119 self.assertTrue('qux' not in query_dict)
Joe Gregorio913e70d2010-11-05 15:38:23 -0400120 self.assertEqual(body, '{}')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400121
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400122 def test_user_agent(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500123 model = JsonModel(data_wrapper=False)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400124
125 headers = {'user-agent': 'my-test-app/1.23.4'}
126 path_params = {}
127 query_params = {}
128 body = {}
129
130 headers, params, query, body = model.request(headers, path_params, query_params, body)
131
132 self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0')
133
134 def test_bad_response(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500135 model = JsonModel(data_wrapper=False)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400136 resp = httplib2.Response({'status': '401'})
137 resp.reason = 'Unauthorized'
Joe Gregoriod4e14562011-01-04 09:51:45 -0500138 content = '{"error": {"message": "not authorized"}}'
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400139
140 try:
141 content = model.response(resp, content)
142 self.fail('Should have thrown an exception')
143 except HttpError, e:
144 self.assertTrue('Unauthorized' in str(e))
145
146 resp['content-type'] = 'application/json'
147
148 try:
149 content = model.response(resp, content)
150 self.fail('Should have thrown an exception')
151 except HttpError, e:
152 self.assertTrue('not authorized' in str(e))
153
154
155 def test_good_response(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500156 model = JsonModel(data_wrapper=True)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400157 resp = httplib2.Response({'status': '200'})
158 resp.reason = 'OK'
159 content = '{"data": "is good"}'
160
161 content = model.response(resp, content)
162 self.assertEqual(content, 'is good')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400163
Joe Gregorio78a508d2010-10-26 16:36:36 -0400164 def test_good_response_wo_data(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500165 model = JsonModel(data_wrapper=False)
Joe Gregorio78a508d2010-10-26 16:36:36 -0400166 resp = httplib2.Response({'status': '200'})
167 resp.reason = 'OK'
168 content = '{"foo": "is good"}'
169
170 content = model.response(resp, content)
171 self.assertEqual(content, {'foo': 'is good'})
172
173 def test_good_response_wo_data_str(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500174 model = JsonModel(data_wrapper=False)
Joe Gregorio78a508d2010-10-26 16:36:36 -0400175 resp = httplib2.Response({'status': '200'})
176 resp.reason = 'OK'
177 content = '"data goes here"'
178
179 content = model.response(resp, content)
180 self.assertEqual(content, 'data goes here')
181
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400182if __name__ == '__main__':
183 unittest.main()