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 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 24 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 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 |
Bu Sun Kim | f706cfd | 2020-04-20 14:05:05 -0700 | [diff] [blame] | 29 | import pkg_resources |
Bu Sun Kim | 07f647c | 2019-08-09 14:55:24 -0700 | [diff] [blame] | 30 | import platform |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 31 | import unittest2 as unittest |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 32 | import httplib2 |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 33 | import googleapiclient.model |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 34 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 35 | from googleapiclient.errors import HttpError |
| 36 | from googleapiclient.model import JsonModel |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 37 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 38 | from six.moves.urllib.parse import parse_qs |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 39 | |
Bu Sun Kim | f706cfd | 2020-04-20 14:05:05 -0700 | [diff] [blame] | 40 | _LIBRARY_VERSION = pkg_resources.get_distribution("google-api-python-client").version |
| 41 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 42 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 43 | class Model(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 44 | def test_json_no_body(self): |
| 45 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 46 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 47 | headers = {} |
| 48 | path_params = {} |
| 49 | query_params = {} |
| 50 | body = None |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 51 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 52 | headers, unused_params, query, body = model.request( |
| 53 | headers, path_params, query_params, body |
| 54 | ) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 55 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 56 | self.assertEqual(headers["accept"], "application/json") |
| 57 | self.assertTrue("content-type" not in headers) |
| 58 | self.assertNotEqual(query, "") |
| 59 | self.assertEqual(body, None) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 60 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 61 | def test_json_body(self): |
| 62 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 63 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 64 | headers = {} |
| 65 | path_params = {} |
| 66 | query_params = {} |
| 67 | body = {} |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 68 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 69 | headers, unused_params, query, body = model.request( |
| 70 | headers, path_params, query_params, body |
| 71 | ) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 72 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 73 | self.assertEqual(headers["accept"], "application/json") |
| 74 | self.assertEqual(headers["content-type"], "application/json") |
| 75 | self.assertNotEqual(query, "") |
| 76 | self.assertEqual(body, "{}") |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 77 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 78 | def test_json_body_data_wrapper(self): |
| 79 | model = JsonModel(data_wrapper=True) |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 80 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 81 | headers = {} |
| 82 | path_params = {} |
| 83 | query_params = {} |
| 84 | body = {} |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 85 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 86 | headers, unused_params, query, body = model.request( |
| 87 | headers, path_params, query_params, body |
| 88 | ) |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 89 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 90 | self.assertEqual(headers["accept"], "application/json") |
| 91 | self.assertEqual(headers["content-type"], "application/json") |
| 92 | self.assertNotEqual(query, "") |
| 93 | self.assertEqual(body, '{"data": {}}') |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 94 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 95 | def test_json_body_default_data(self): |
| 96 | """Test that a 'data' wrapper doesn't get added if one is already present.""" |
| 97 | model = JsonModel(data_wrapper=True) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 98 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 99 | headers = {} |
| 100 | path_params = {} |
| 101 | query_params = {} |
| 102 | body = {"data": "foo"} |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 103 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 104 | headers, unused_params, query, body = model.request( |
| 105 | headers, path_params, query_params, body |
| 106 | ) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 107 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 108 | self.assertEqual(headers["accept"], "application/json") |
| 109 | self.assertEqual(headers["content-type"], "application/json") |
| 110 | self.assertNotEqual(query, "") |
| 111 | self.assertEqual(body, '{"data": "foo"}') |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 112 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 113 | def test_json_build_query(self): |
| 114 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 115 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 116 | headers = {} |
| 117 | path_params = {} |
| 118 | query_params = { |
| 119 | "foo": 1, |
| 120 | "bar": u"\N{COMET}", |
| 121 | "baz": ["fe", "fi", "fo", "fum"], # Repeated parameters |
| 122 | "qux": [], |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 123 | } |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 124 | body = {} |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 125 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 126 | headers, unused_params, query, body = model.request( |
| 127 | headers, path_params, query_params, body |
| 128 | ) |
Joe Gregorio | 34044bc | 2011-03-07 16:58:33 -0500 | [diff] [blame] | 129 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 130 | self.assertEqual(headers["accept"], "application/json") |
| 131 | self.assertEqual(headers["content-type"], "application/json") |
Ali Afshar | 81fde8e | 2012-10-23 11:14:28 -0700 | [diff] [blame] | 132 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 133 | query_dict = parse_qs(query[1:]) |
| 134 | self.assertEqual(query_dict["foo"], ["1"]) |
| 135 | if six.PY3: |
| 136 | # Python 3, no need to encode |
| 137 | self.assertEqual(query_dict["bar"], [u"\N{COMET}"]) |
| 138 | else: |
| 139 | # Python 2, encode string |
| 140 | self.assertEqual(query_dict["bar"], [u"\N{COMET}".encode("utf-8")]) |
| 141 | self.assertEqual(query_dict["baz"], ["fe", "fi", "fo", "fum"]) |
| 142 | self.assertTrue("qux" not in query_dict) |
| 143 | self.assertEqual(body, "{}") |
Ali Afshar | 81fde8e | 2012-10-23 11:14:28 -0700 | [diff] [blame] | 144 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 145 | def test_user_agent(self): |
| 146 | model = JsonModel(data_wrapper=False) |
| 147 | |
| 148 | headers = {"user-agent": "my-test-app/1.23.4"} |
| 149 | path_params = {} |
| 150 | query_params = {} |
| 151 | body = {} |
| 152 | |
| 153 | headers, unused_params, unused_query, body = model.request( |
| 154 | headers, path_params, query_params, body |
| 155 | ) |
| 156 | |
| 157 | self.assertEqual(headers["user-agent"], "my-test-app/1.23.4 (gzip)") |
| 158 | |
| 159 | def test_x_goog_api_client(self): |
| 160 | model = JsonModel(data_wrapper=False) |
| 161 | |
| 162 | # test header composition for cloud clients that wrap discovery |
| 163 | headers = {"x-goog-api-client": "gccl/1.23.4"} |
| 164 | path_params = {} |
| 165 | query_params = {} |
| 166 | body = {} |
| 167 | |
| 168 | headers, unused_params, unused_query, body = model.request( |
| 169 | headers, path_params, query_params, body |
| 170 | ) |
| 171 | |
| 172 | self.assertEqual( |
| 173 | headers["x-goog-api-client"], |
| 174 | "gccl/1.23.4" |
| 175 | + " gdcl/" |
Bu Sun Kim | f706cfd | 2020-04-20 14:05:05 -0700 | [diff] [blame] | 176 | + _LIBRARY_VERSION |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 177 | + " gl-python/" |
| 178 | + platform.python_version(), |
| 179 | ) |
| 180 | |
| 181 | def test_bad_response(self): |
| 182 | model = JsonModel(data_wrapper=False) |
| 183 | resp = httplib2.Response({"status": "401"}) |
| 184 | resp.reason = "Unauthorized" |
| 185 | content = b'{"error": {"message": "not authorized"}}' |
| 186 | |
| 187 | try: |
| 188 | content = model.response(resp, content) |
| 189 | self.fail("Should have thrown an exception") |
| 190 | except HttpError as e: |
| 191 | self.assertTrue("not authorized" in str(e)) |
| 192 | |
| 193 | resp["content-type"] = "application/json" |
| 194 | |
| 195 | try: |
| 196 | content = model.response(resp, content) |
| 197 | self.fail("Should have thrown an exception") |
| 198 | except HttpError as e: |
| 199 | self.assertTrue("not authorized" in str(e)) |
| 200 | |
| 201 | def test_good_response(self): |
| 202 | model = JsonModel(data_wrapper=True) |
| 203 | resp = httplib2.Response({"status": "200"}) |
| 204 | resp.reason = "OK" |
| 205 | content = '{"data": "is good"}' |
| 206 | |
| 207 | content = model.response(resp, content) |
| 208 | self.assertEqual(content, "is good") |
| 209 | |
| 210 | def test_good_response_wo_data(self): |
| 211 | model = JsonModel(data_wrapper=False) |
| 212 | resp = httplib2.Response({"status": "200"}) |
| 213 | resp.reason = "OK" |
| 214 | content = '{"foo": "is good"}' |
| 215 | |
| 216 | content = model.response(resp, content) |
| 217 | self.assertEqual(content, {"foo": "is good"}) |
| 218 | |
| 219 | def test_good_response_wo_data_str(self): |
| 220 | model = JsonModel(data_wrapper=False) |
| 221 | resp = httplib2.Response({"status": "200"}) |
| 222 | resp.reason = "OK" |
| 223 | content = '"data goes here"' |
| 224 | |
| 225 | content = model.response(resp, content) |
| 226 | self.assertEqual(content, "data goes here") |
| 227 | |
| 228 | def test_no_content_response(self): |
| 229 | model = JsonModel(data_wrapper=False) |
| 230 | resp = httplib2.Response({"status": "204"}) |
| 231 | resp.reason = "No Content" |
| 232 | content = "" |
| 233 | |
| 234 | content = model.response(resp, content) |
| 235 | self.assertEqual(content, {}) |
| 236 | |
| 237 | def test_logging(self): |
| 238 | class MockLogging(object): |
| 239 | def __init__(self): |
| 240 | self.info_record = [] |
| 241 | self.debug_record = [] |
| 242 | |
| 243 | def info(self, message, *args): |
| 244 | self.info_record.append(message % args) |
| 245 | |
| 246 | def debug(self, message, *args): |
| 247 | self.debug_record.append(message % args) |
| 248 | |
| 249 | class MockResponse(dict): |
| 250 | def __init__(self, items): |
| 251 | super(MockResponse, self).__init__() |
| 252 | self.status = items["status"] |
| 253 | for key, value in six.iteritems(items): |
| 254 | self[key] = value |
| 255 | |
| 256 | old_logging = googleapiclient.model.LOGGER |
| 257 | googleapiclient.model.LOGGER = MockLogging() |
| 258 | googleapiclient.model.dump_request_response = True |
| 259 | model = JsonModel() |
| 260 | request_body = {"field1": "value1", "field2": "value2"} |
| 261 | body_string = model.request({}, {}, {}, request_body)[-1] |
| 262 | json_body = json.loads(body_string) |
| 263 | self.assertEqual(request_body, json_body) |
| 264 | |
| 265 | response = { |
| 266 | "status": 200, |
| 267 | "response_field_1": "response_value_1", |
| 268 | "response_field_2": "response_value_2", |
| 269 | } |
| 270 | response_body = model.response(MockResponse(response), body_string) |
| 271 | self.assertEqual(request_body, response_body) |
| 272 | self.assertEqual( |
| 273 | googleapiclient.model.LOGGER.info_record[:2], |
| 274 | ["--request-start--", "-headers-start-"], |
| 275 | ) |
| 276 | self.assertTrue( |
| 277 | "response_field_1: response_value_1" |
| 278 | in googleapiclient.model.LOGGER.info_record |
| 279 | ) |
| 280 | self.assertTrue( |
| 281 | "response_field_2: response_value_2" |
| 282 | in googleapiclient.model.LOGGER.info_record |
| 283 | ) |
| 284 | self.assertEqual( |
| 285 | json.loads(googleapiclient.model.LOGGER.info_record[-2]), request_body |
| 286 | ) |
| 287 | self.assertEqual( |
| 288 | googleapiclient.model.LOGGER.info_record[-1], "--response-end--" |
| 289 | ) |
| 290 | googleapiclient.model.LOGGER = old_logging |
| 291 | |
| 292 | def test_no_data_wrapper_deserialize(self): |
| 293 | model = JsonModel(data_wrapper=False) |
| 294 | resp = httplib2.Response({"status": "200"}) |
| 295 | resp.reason = "OK" |
| 296 | content = '{"data": "is good"}' |
| 297 | content = model.response(resp, content) |
| 298 | self.assertEqual(content, {"data": "is good"}) |
| 299 | |
| 300 | def test_data_wrapper_deserialize(self): |
| 301 | model = JsonModel(data_wrapper=True) |
| 302 | resp = httplib2.Response({"status": "200"}) |
| 303 | resp.reason = "OK" |
| 304 | content = '{"data": "is good"}' |
| 305 | content = model.response(resp, content) |
| 306 | self.assertEqual(content, "is good") |
| 307 | |
| 308 | def test_data_wrapper_deserialize_nodata(self): |
| 309 | model = JsonModel(data_wrapper=True) |
| 310 | resp = httplib2.Response({"status": "200"}) |
| 311 | resp.reason = "OK" |
| 312 | content = '{"atad": "is good"}' |
| 313 | content = model.response(resp, content) |
| 314 | self.assertEqual(content, {"atad": "is good"}) |
Ali Afshar | 81fde8e | 2012-10-23 11:14:28 -0700 | [diff] [blame] | 315 | |
| 316 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 317 | if __name__ == "__main__": |
| 318 | unittest.main() |