Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 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 Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 16 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 17 | """JSON Model tests |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 18 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 19 | Unit tests for the JSON model. |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 20 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 21 | from __future__ import absolute_import |
| 22 | import six |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 23 | |
| 24 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 25 | |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 26 | import copy |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 27 | import json |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 28 | import os |
| 29 | import unittest |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 30 | import httplib2 |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 31 | import googleapiclient.model |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 32 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 33 | from googleapiclient import __version__ |
| 34 | from googleapiclient.errors import HttpError |
| 35 | from googleapiclient.model import JsonModel |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 36 | |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 37 | # Python 2.5 requires different modules |
| 38 | try: |
| 39 | from urlparse import parse_qs |
| 40 | except ImportError: |
| 41 | from cgi import parse_qs |
| 42 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 43 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 44 | class Model(unittest.TestCase): |
| 45 | def test_json_no_body(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 46 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 47 | |
| 48 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 49 | path_params = {} |
| 50 | query_params = {} |
| 51 | body = None |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 52 | |
Joe Gregorio | 5a4c328 | 2013-07-29 13:48:06 -0400 | [diff] [blame] | 53 | headers, unused_params, query, body = model.request( |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 54 | headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 55 | |
| 56 | self.assertEqual(headers['accept'], 'application/json') |
| 57 | self.assertTrue('content-type' not in headers) |
| 58 | self.assertNotEqual(query, '') |
| 59 | self.assertEqual(body, None) |
| 60 | |
| 61 | def test_json_body(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 62 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 63 | |
| 64 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 65 | path_params = {} |
| 66 | query_params = {} |
| 67 | body = {} |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 68 | |
Joe Gregorio | 5a4c328 | 2013-07-29 13:48:06 -0400 | [diff] [blame] | 69 | headers, unused_params, query, body = model.request( |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 70 | headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 71 | |
| 72 | self.assertEqual(headers['accept'], 'application/json') |
| 73 | self.assertEqual(headers['content-type'], 'application/json') |
| 74 | self.assertNotEqual(query, '') |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 75 | self.assertEqual(body, '{}') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 76 | |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 77 | def test_json_body_data_wrapper(self): |
| 78 | model = JsonModel(data_wrapper=True) |
| 79 | |
| 80 | headers = {} |
| 81 | path_params = {} |
| 82 | query_params = {} |
| 83 | body = {} |
| 84 | |
Joe Gregorio | 5a4c328 | 2013-07-29 13:48:06 -0400 | [diff] [blame] | 85 | headers, unused_params, query, body = model.request( |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 86 | headers, path_params, query_params, body) |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 87 | |
| 88 | self.assertEqual(headers['accept'], 'application/json') |
| 89 | self.assertEqual(headers['content-type'], 'application/json') |
| 90 | self.assertNotEqual(query, '') |
| 91 | self.assertEqual(body, '{"data": {}}') |
| 92 | |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 93 | def test_json_body_default_data(self): |
| 94 | """Test that a 'data' wrapper doesn't get added if one is already present.""" |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 95 | model = JsonModel(data_wrapper=True) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 96 | |
| 97 | headers = {} |
| 98 | path_params = {} |
| 99 | query_params = {} |
| 100 | body = {'data': 'foo'} |
| 101 | |
Joe Gregorio | 5a4c328 | 2013-07-29 13:48:06 -0400 | [diff] [blame] | 102 | headers, unused_params, query, body = model.request( |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 103 | headers, path_params, query_params, body) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 104 | |
| 105 | self.assertEqual(headers['accept'], 'application/json') |
| 106 | self.assertEqual(headers['content-type'], 'application/json') |
| 107 | self.assertNotEqual(query, '') |
| 108 | self.assertEqual(body, '{"data": "foo"}') |
| 109 | |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 110 | def test_json_build_query(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 111 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 112 | |
| 113 | headers = {} |
| 114 | path_params = {} |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 115 | query_params = {'foo': 1, 'bar': u'\N{COMET}', |
| 116 | 'baz': ['fe', 'fi', 'fo', 'fum'], # Repeated parameters |
| 117 | 'qux': []} |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 118 | body = {} |
| 119 | |
Joe Gregorio | 5a4c328 | 2013-07-29 13:48:06 -0400 | [diff] [blame] | 120 | headers, unused_params, query, body = model.request( |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 121 | headers, path_params, query_params, body) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 122 | |
| 123 | self.assertEqual(headers['accept'], 'application/json') |
| 124 | self.assertEqual(headers['content-type'], 'application/json') |
| 125 | |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 126 | query_dict = parse_qs(query[1:]) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 127 | self.assertEqual(query_dict['foo'], ['1']) |
| 128 | self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')]) |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 129 | self.assertEqual(query_dict['baz'], ['fe', 'fi', 'fo', 'fum']) |
| 130 | self.assertTrue('qux' not in query_dict) |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 131 | self.assertEqual(body, '{}') |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 132 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 133 | def test_user_agent(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 134 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 135 | |
| 136 | headers = {'user-agent': 'my-test-app/1.23.4'} |
| 137 | path_params = {} |
| 138 | query_params = {} |
| 139 | body = {} |
| 140 | |
Joe Gregorio | 5a4c328 | 2013-07-29 13:48:06 -0400 | [diff] [blame] | 141 | headers, unused_params, unused_query, body = model.request( |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 142 | headers, path_params, query_params, body) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 143 | |
Joe Gregorio | a314b1f | 2013-04-05 16:25:11 -0400 | [diff] [blame] | 144 | self.assertEqual(headers['user-agent'], |
Joe Gregorio | c02f563 | 2013-05-13 11:28:56 -0400 | [diff] [blame] | 145 | 'my-test-app/1.23.4 google-api-python-client/' + __version__ + |
| 146 | ' (gzip)') |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 147 | |
| 148 | def test_bad_response(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 149 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 150 | resp = httplib2.Response({'status': '401'}) |
| 151 | resp.reason = 'Unauthorized' |
Joe Gregorio | d4e1456 | 2011-01-04 09:51:45 -0500 | [diff] [blame] | 152 | content = '{"error": {"message": "not authorized"}}' |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 153 | |
| 154 | try: |
| 155 | content = model.response(resp, content) |
| 156 | self.fail('Should have thrown an exception') |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 157 | except HttpError as e: |
Joe Gregorio | 20b54fb | 2012-07-26 09:59:35 -0400 | [diff] [blame] | 158 | self.assertTrue('not authorized' in str(e)) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 159 | |
| 160 | resp['content-type'] = 'application/json' |
| 161 | |
| 162 | try: |
| 163 | content = model.response(resp, content) |
| 164 | self.fail('Should have thrown an exception') |
INADA Naoki | c1505df | 2014-08-20 15:19:53 +0900 | [diff] [blame] | 165 | except HttpError as e: |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 166 | self.assertTrue('not authorized' in str(e)) |
| 167 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 168 | def test_good_response(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 169 | model = JsonModel(data_wrapper=True) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 170 | resp = httplib2.Response({'status': '200'}) |
| 171 | resp.reason = 'OK' |
| 172 | content = '{"data": "is good"}' |
| 173 | |
| 174 | content = model.response(resp, content) |
| 175 | self.assertEqual(content, 'is good') |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 176 | |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 177 | def test_good_response_wo_data(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 178 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 179 | resp = httplib2.Response({'status': '200'}) |
| 180 | resp.reason = 'OK' |
| 181 | content = '{"foo": "is good"}' |
| 182 | |
| 183 | content = model.response(resp, content) |
| 184 | self.assertEqual(content, {'foo': 'is good'}) |
| 185 | |
| 186 | def test_good_response_wo_data_str(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 187 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 188 | resp = httplib2.Response({'status': '200'}) |
| 189 | resp.reason = 'OK' |
| 190 | content = '"data goes here"' |
| 191 | |
| 192 | content = model.response(resp, content) |
| 193 | self.assertEqual(content, 'data goes here') |
| 194 | |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 195 | def test_no_content_response(self): |
| 196 | model = JsonModel(data_wrapper=False) |
| 197 | resp = httplib2.Response({'status': '204'}) |
| 198 | resp.reason = 'No Content' |
| 199 | content = '' |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 200 | |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 201 | content = model.response(resp, content) |
| 202 | self.assertEqual(content, {}) |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 203 | |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 204 | def test_logging(self): |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 205 | class MockLogging(object): |
| 206 | def __init__(self): |
| 207 | self.info_record = [] |
| 208 | self.debug_record = [] |
| 209 | def info(self, message, *args): |
| 210 | self.info_record.append(message % args) |
| 211 | |
| 212 | def debug(self, message, *args): |
| 213 | self.debug_record.append(message % args) |
| 214 | |
| 215 | class MockResponse(dict): |
| 216 | def __init__(self, items): |
| 217 | super(MockResponse, self).__init__() |
| 218 | self.status = items['status'] |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 219 | for key, value in six.iteritems(items): |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 220 | self[key] = value |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 221 | old_logging = googleapiclient.model.logging |
| 222 | googleapiclient.model.logging = MockLogging() |
| 223 | googleapiclient.model.dump_request_response = True |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 224 | model = JsonModel() |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 225 | request_body = { |
| 226 | 'field1': 'value1', |
| 227 | 'field2': 'value2' |
| 228 | } |
| 229 | body_string = model.request({}, {}, {}, request_body)[-1] |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 230 | json_body = json.loads(body_string) |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 231 | self.assertEqual(request_body, json_body) |
| 232 | |
| 233 | response = {'status': 200, |
| 234 | 'response_field_1': 'response_value_1', |
| 235 | 'response_field_2': 'response_value_2'} |
| 236 | response_body = model.response(MockResponse(response), body_string) |
| 237 | self.assertEqual(request_body, response_body) |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 238 | self.assertEqual(googleapiclient.model.logging.info_record[:2], |
Joe Gregorio | afdf50b | 2011-03-08 09:41:52 -0500 | [diff] [blame] | 239 | ['--request-start--', |
| 240 | '-headers-start-']) |
| 241 | self.assertTrue('response_field_1: response_value_1' in |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 242 | googleapiclient.model.logging.info_record) |
Joe Gregorio | afdf50b | 2011-03-08 09:41:52 -0500 | [diff] [blame] | 243 | self.assertTrue('response_field_2: response_value_2' in |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 244 | googleapiclient.model.logging.info_record) |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 245 | self.assertEqual(json.loads(googleapiclient.model.logging.info_record[-2]), |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 246 | request_body) |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 247 | self.assertEqual(googleapiclient.model.logging.info_record[-1], |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 248 | '--response-end--') |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 249 | googleapiclient.model.logging = old_logging |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 250 | |
Ali Afshar | 81fde8e | 2012-10-23 11:14:28 -0700 | [diff] [blame] | 251 | def test_no_data_wrapper_deserialize(self): |
| 252 | model = JsonModel(data_wrapper=False) |
| 253 | resp = httplib2.Response({'status': '200'}) |
| 254 | resp.reason = 'OK' |
| 255 | content = '{"data": "is good"}' |
| 256 | content = model.response(resp, content) |
| 257 | self.assertEqual(content, {'data': 'is good'}) |
| 258 | |
| 259 | def test_data_wrapper_deserialize(self): |
| 260 | model = JsonModel(data_wrapper=True) |
| 261 | resp = httplib2.Response({'status': '200'}) |
| 262 | resp.reason = 'OK' |
| 263 | content = '{"data": "is good"}' |
| 264 | content = model.response(resp, content) |
| 265 | self.assertEqual(content, 'is good') |
| 266 | |
| 267 | def test_data_wrapper_deserialize_nodata(self): |
| 268 | model = JsonModel(data_wrapper=True) |
| 269 | resp = httplib2.Response({'status': '200'}) |
| 270 | resp.reason = 'OK' |
| 271 | content = '{"atad": "is good"}' |
| 272 | content = model.response(resp, content) |
| 273 | self.assertEqual(content, {'atad': 'is good'}) |
| 274 | |
| 275 | |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 276 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 277 | if __name__ == '__main__': |
| 278 | unittest.main() |