blob: 01015f151fa008850049e1845ca3eb2acfc2b59f [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 Gregorioc5c5a372010-09-22 11:42:32 -040024from apiclient.discovery import JsonModel, HttpError
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040025import os
26import unittest
Joe Gregorioc5c5a372010-09-22 11:42:32 -040027import httplib2
ade@google.comd69e5e42010-08-31 15:28:20 +010028
29# Python 2.5 requires different modules
30try:
31 from urlparse import parse_qs
32except ImportError:
33 from cgi import parse_qs
34
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040035
36
37class Model(unittest.TestCase):
38 def test_json_no_body(self):
39 model = JsonModel()
40
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):
54 model = JsonModel()
55
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, '')
66 self.assertEqual(body, '{"data": {}}')
67
Joe Gregorio8963ff92010-10-11 13:14:43 -040068 def test_json_body_default_data(self):
69 """Test that a 'data' wrapper doesn't get added if one is already present."""
70 model = JsonModel()
71
72 headers = {}
73 path_params = {}
74 query_params = {}
75 body = {'data': 'foo'}
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": "foo"}')
83
Joe Gregoriofe695fb2010-08-30 12:04:04 -040084 def test_json_build_query(self):
85 model = JsonModel()
86
87 headers = {}
88 path_params = {}
89 query_params = {'foo': 1, 'bar': u'\N{COMET}'}
90 body = {}
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
ade@google.comd69e5e42010-08-31 15:28:20 +010097 query_dict = parse_qs(query)
Joe Gregoriofe695fb2010-08-30 12:04:04 -040098 self.assertEqual(query_dict['foo'], ['1'])
99 self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')])
100 self.assertEqual(body, '{"data": {}}')
101
Joe Gregorioc5c5a372010-09-22 11:42:32 -0400102 def test_user_agent(self):
103 model = JsonModel()
104
105 headers = {'user-agent': 'my-test-app/1.23.4'}
106 path_params = {}
107 query_params = {}
108 body = {}
109
110 headers, params, query, body = model.request(headers, path_params, query_params, body)
111
112 self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0')
113
114 def test_bad_response(self):
115 model = JsonModel()
116 resp = httplib2.Response({'status': '401'})
117 resp.reason = 'Unauthorized'
118 content = '{"error": "not authorized"}'
119
120 try:
121 content = model.response(resp, content)
122 self.fail('Should have thrown an exception')
123 except HttpError, e:
124 self.assertTrue('Unauthorized' in str(e))
125
126 resp['content-type'] = 'application/json'
127
128 try:
129 content = model.response(resp, content)
130 self.fail('Should have thrown an exception')
131 except HttpError, e:
132 self.assertTrue('not authorized' in str(e))
133
134
135 def test_good_response(self):
136 model = JsonModel()
137 resp = httplib2.Response({'status': '200'})
138 resp.reason = 'OK'
139 content = '{"data": "is good"}'
140
141 content = model.response(resp, content)
142 self.assertEqual(content, 'is good')
Joe Gregoriofe695fb2010-08-30 12:04:04 -0400143
Joe Gregorioba9ea7f2010-08-19 15:49:04 -0400144if __name__ == '__main__':
145 unittest.main()