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