blob: fa7ad05a863ec4e622a99fc60caca9feab57f52d [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 = {}
104 query_params = {'foo': 1, 'bar': u'\N{COMET}'}
105 body = {}
106
107 headers, params, query, body = model.request(headers, path_params, query_params, body)
108
109 self.assertEqual(headers['accept'], 'application/json')
110 self.assertEqual(headers['content-type'], 'application/json')
111
ade@google.comd69e5e42010-08-31 15:28:20 +0100112 query_dict = parse_qs(query)
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400113 self.assertEqual(query_dict['foo'], ['1'])
114 self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')])
Joe Gregorio913e70d2010-11-05 15:38:23 -0400115 self.assertEqual(body, '{}')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400116
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400117 def test_user_agent(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500118 model = JsonModel(data_wrapper=False)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400119
120 headers = {'user-agent': 'my-test-app/1.23.4'}
121 path_params = {}
122 query_params = {}
123 body = {}
124
125 headers, params, query, body = model.request(headers, path_params, query_params, body)
126
127 self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0')
128
129 def test_bad_response(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500130 model = JsonModel(data_wrapper=False)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400131 resp = httplib2.Response({'status': '401'})
132 resp.reason = 'Unauthorized'
Joe Gregoriod4e14562011-01-04 09:51:45 -0500133 content = '{"error": {"message": "not authorized"}}'
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400134
135 try:
136 content = model.response(resp, content)
137 self.fail('Should have thrown an exception')
138 except HttpError, e:
139 self.assertTrue('Unauthorized' in str(e))
140
141 resp['content-type'] = 'application/json'
142
143 try:
144 content = model.response(resp, content)
145 self.fail('Should have thrown an exception')
146 except HttpError, e:
147 self.assertTrue('not authorized' in str(e))
148
149
150 def test_good_response(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500151 model = JsonModel(data_wrapper=True)
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400152 resp = httplib2.Response({'status': '200'})
153 resp.reason = 'OK'
154 content = '{"data": "is good"}'
155
156 content = model.response(resp, content)
157 self.assertEqual(content, 'is good')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400158
Joe Gregorio78a508d2010-10-26 16:36:36 -0400159 def test_good_response_wo_data(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500160 model = JsonModel(data_wrapper=False)
Joe Gregorio78a508d2010-10-26 16:36:36 -0400161 resp = httplib2.Response({'status': '200'})
162 resp.reason = 'OK'
163 content = '{"foo": "is good"}'
164
165 content = model.response(resp, content)
166 self.assertEqual(content, {'foo': 'is good'})
167
168 def test_good_response_wo_data_str(self):
Joe Gregoriod433b2a2011-02-22 10:51:51 -0500169 model = JsonModel(data_wrapper=False)
Joe Gregorio78a508d2010-10-26 16:36:36 -0400170 resp = httplib2.Response({'status': '200'})
171 resp.reason = 'OK'
172 content = '"data goes here"'
173
174 content = model.response(resp, content)
175 self.assertEqual(content, 'data goes here')
176
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400177if __name__ == '__main__':
178 unittest.main()