Craig Citro | 15744b1 | 2015-03-02 13:34:32 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -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. |
| 16 | |
| 17 | """Protocol Buffer Model tests |
| 18 | |
| 19 | Unit tests for the Protocol Buffer model. |
| 20 | """ |
INADA Naoki | d898a37 | 2015-03-04 03:52:46 +0900 | [diff] [blame] | 21 | from __future__ import absolute_import |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 22 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 23 | __author__ = "mmcdonald@google.com (Matt McDonald)" |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 24 | |
Pat Ferate | 497a90f | 2015-03-09 09:52:54 -0700 | [diff] [blame] | 25 | import unittest2 as unittest |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 26 | import httplib2 |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 27 | import googleapiclient.model |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 28 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 29 | from googleapiclient.errors import HttpError |
| 30 | from googleapiclient.model import ProtocolBufferModel |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 31 | |
Pat Ferate | d5b61bd | 2015-03-03 16:04:11 -0800 | [diff] [blame] | 32 | from six.moves.urllib.parse import parse_qs |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class MockProtocolBuffer(object): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 36 | def __init__(self, data=None): |
| 37 | self.data = data |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 38 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 39 | def __eq__(self, other): |
| 40 | return self.data == other.data |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 41 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 42 | @classmethod |
| 43 | def FromString(cls, string): |
| 44 | return cls(string) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 45 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 46 | def SerializeToString(self): |
| 47 | return self.data |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class Model(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 51 | def setUp(self): |
| 52 | self.model = ProtocolBufferModel(MockProtocolBuffer) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 53 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 54 | def test_no_body(self): |
| 55 | headers = {} |
| 56 | path_params = {} |
| 57 | query_params = {} |
| 58 | body = None |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 59 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 60 | headers, params, query, body = self.model.request( |
| 61 | headers, path_params, query_params, body |
| 62 | ) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 63 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 64 | self.assertEqual(headers["accept"], "application/x-protobuf") |
| 65 | self.assertTrue("content-type" not in headers) |
| 66 | self.assertNotEqual(query, "") |
| 67 | self.assertEqual(body, None) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 68 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 69 | def test_body(self): |
| 70 | headers = {} |
| 71 | path_params = {} |
| 72 | query_params = {} |
| 73 | body = MockProtocolBuffer("data") |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 74 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 75 | headers, params, query, body = self.model.request( |
| 76 | headers, path_params, query_params, body |
| 77 | ) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 78 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 79 | self.assertEqual(headers["accept"], "application/x-protobuf") |
| 80 | self.assertEqual(headers["content-type"], "application/x-protobuf") |
| 81 | self.assertNotEqual(query, "") |
| 82 | self.assertEqual(body, "data") |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 83 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 84 | def test_good_response(self): |
| 85 | resp = httplib2.Response({"status": "200"}) |
| 86 | resp.reason = "OK" |
| 87 | content = "data" |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 88 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 89 | content = self.model.response(resp, content) |
| 90 | self.assertEqual(content, MockProtocolBuffer("data")) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 91 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 92 | def test_no_content_response(self): |
| 93 | resp = httplib2.Response({"status": "204"}) |
| 94 | resp.reason = "No Content" |
| 95 | content = "" |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 96 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 97 | content = self.model.response(resp, content) |
| 98 | self.assertEqual(content, MockProtocolBuffer()) |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 99 | |
| 100 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 101 | if __name__ == "__main__": |
| 102 | unittest.main() |