blob: 88f24008415e10c76709581fe86e422b47d43309 [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 Gregorio3fada332011-01-07 17:07:45 -050024from apiclient.model import JsonModel
25from apiclient.errors import HttpError
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040026import os
27import unittest
Joe Gregorioc5c5a372010-09-22 11:42:32 -040028import httplib2
ade@google.comd69e5e42010-08-31 15:28:20 +010029
30# Python 2.5 requires different modules
31try:
32 from urlparse import parse_qs
33except ImportError:
34 from cgi import parse_qs
35
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040036
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040037class Model(unittest.TestCase):
38 def test_json_no_body(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -050039 model = JsonModel(data_wrapper=False)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040040
41 headers = {}
ade@google.com850cf552010-08-20 23:24:56 +010042 path_params = {}
43 query_params = {}
44 body = None
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040045
ade@google.com850cf552010-08-20 23:24:56 +010046 headers, params, query, body = model.request(headers, path_params, query_params, body)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040047
48 self.assertEqual(headers['accept'], 'application/json')
49 self.assertTrue('content-type' not in headers)
50 self.assertNotEqual(query, '')
51 self.assertEqual(body, None)
52
53 def test_json_body(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -050054 model = JsonModel(data_wrapper=False)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040055
56 headers = {}
ade@google.com850cf552010-08-20 23:24:56 +010057 path_params = {}
58 query_params = {}
59 body = {}
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040060
ade@google.com850cf552010-08-20 23:24:56 +010061 headers, params, query, body = model.request(headers, path_params, query_params, body)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040062
63 self.assertEqual(headers['accept'], 'application/json')
64 self.assertEqual(headers['content-type'], 'application/json')
65 self.assertNotEqual(query, '')
Joe Gregorio913e70d2010-11-05 15:38:23 -040066 self.assertEqual(body, '{}')
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040067
Joe Gregoriod433b2a2011-02-22 10:51:51 -050068 def test_json_body_data_wrapper(self):
69 model = JsonModel(data_wrapper=True)
70
71 headers = {}
72 path_params = {}
73 query_params = {}
74 body = {}
75
76 headers, params, query, body = model.request(headers, path_params, query_params, body)
77
78 self.assertEqual(headers['accept'], 'application/json')
79 self.assertEqual(headers['content-type'], 'application/json')
80 self.assertNotEqual(query, '')
81 self.assertEqual(body, '{"data": {}}')
82
Joe Gregorio8963ff92010-10-11 13:14:43 -040083 def test_json_body_default_data(self):
84 """Test that a 'data' wrapper doesn't get added if one is already present."""
Joe Gregoriod433b2a2011-02-22 10:51:51 -050085 model = JsonModel(data_wrapper=True)
Joe Gregorio8963ff92010-10-11 13:14:43 -040086
87 headers = {}
88 path_params = {}
89 query_params = {}
90 body = {'data': 'foo'}
91
92 headers, params, query, body = model.request(headers, path_params, query_params, body)
93
94 self.assertEqual(headers['accept'], 'application/json')
95 self.assertEqual(headers['content-type'], 'application/json')
96 self.assertNotEqual(query, '')
97 self.assertEqual(body, '{"data": "foo"}')
98
Joe Gregoriofe695fb2010-08-30 12:04:04 -040099 def test_json_build_query(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500100 model = JsonModel(data_wrapper=False)
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400101
102 headers = {}
103 path_params = {}
Joe Gregorio61d7e962011-02-22 22:52:07 -0500104 query_params = {'foo': 1, 'bar': u'\N{COMET}',
105 'baz': ['fe', 'fi', 'fo', 'fum'], # Repeated parameters
106 'qux': []}
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400107 body = {}
108
109 headers, params, query, body = model.request(headers, path_params, query_params, body)
110
111 self.assertEqual(headers['accept'], 'application/json')
112 self.assertEqual(headers['content-type'], 'application/json')
113
Joe Gregorio61d7e962011-02-22 22:52:07 -0500114 query_dict = parse_qs(query[1:])
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400115 self.assertEqual(query_dict['foo'], ['1'])
116 self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')])
Joe Gregorio61d7e962011-02-22 22:52:07 -0500117 self.assertEqual(query_dict['baz'], ['fe', 'fi', 'fo', 'fum'])
118 self.assertTrue('qux' not in query_dict)
Joe Gregorio913e70d2010-11-05 15:38:23 -0400119 self.assertEqual(body, '{}')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400120
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400121 def test_user_agent(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500122 model = JsonModel(data_wrapper=False)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400123
124 headers = {'user-agent': 'my-test-app/1.23.4'}
125 path_params = {}
126 query_params = {}
127 body = {}
128
129 headers, params, query, body = model.request(headers, path_params, query_params, body)
130
131 self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0')
132
133 def test_bad_response(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500134 model = JsonModel(data_wrapper=False)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400135 resp = httplib2.Response({'status': '401'})
136 resp.reason = 'Unauthorized'
Joe Gregoriod4e14562011-01-04 09:51:45 -0500137 content = '{"error": {"message": "not authorized"}}'
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400138
139 try:
140 content = model.response(resp, content)
141 self.fail('Should have thrown an exception')
142 except HttpError, e:
143 self.assertTrue('Unauthorized' in str(e))
144
145 resp['content-type'] = 'application/json'
146
147 try:
148 content = model.response(resp, content)
149 self.fail('Should have thrown an exception')
150 except HttpError, e:
151 self.assertTrue('not authorized' in str(e))
152
153
154 def test_good_response(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500155 model = JsonModel(data_wrapper=True)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400156 resp = httplib2.Response({'status': '200'})
157 resp.reason = 'OK'
158 content = '{"data": "is good"}'
159
160 content = model.response(resp, content)
161 self.assertEqual(content, 'is good')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400162
Joe Gregorio78a508d2010-10-26 16:36:36 -0400163 def test_good_response_wo_data(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500164 model = JsonModel(data_wrapper=False)
Joe Gregorio78a508d2010-10-26 16:36:36 -0400165 resp = httplib2.Response({'status': '200'})
166 resp.reason = 'OK'
167 content = '{"foo": "is good"}'
168
169 content = model.response(resp, content)
170 self.assertEqual(content, {'foo': 'is good'})
171
172 def test_good_response_wo_data_str(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500173 model = JsonModel(data_wrapper=False)
Joe Gregorio78a508d2010-10-26 16:36:36 -0400174 resp = httplib2.Response({'status': '200'})
175 resp.reason = 'OK'
176 content = '"data goes here"'
177
178 content = model.response(resp, content)
179 self.assertEqual(content, 'data goes here')
180
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400181if __name__ == '__main__':
182 unittest.main()