blob: 4479b13d9d63c7f7b020d04caa6a1d0538629493 [file] [log] [blame]
Craig Citro15744b12015-03-02 13:34:32 -08001#!/usr/bin/env python
Matt McDonald2a5f4132011-04-29 16:32:27 -04002#
Craig Citro751b7fb2014-09-23 11:20:38 -07003# Copyright 2014 Google Inc. All Rights Reserved.
Matt McDonald2a5f4132011-04-29 16:32:27 -04004#
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
19Unit tests for the Protocol Buffer model.
20"""
INADA Naokid898a372015-03-04 03:52:46 +090021from __future__ import absolute_import
Matt McDonald2a5f4132011-04-29 16:32:27 -040022
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070023__author__ = "mmcdonald@google.com (Matt McDonald)"
Matt McDonald2a5f4132011-04-29 16:32:27 -040024
Pat Ferate497a90f2015-03-09 09:52:54 -070025import unittest2 as unittest
Matt McDonald2a5f4132011-04-29 16:32:27 -040026import httplib2
Matt McDonald2a5f4132011-04-29 16:32:27 -040027
John Asmuth864311d2014-04-24 15:46:08 -040028from googleapiclient.model import ProtocolBufferModel
Matt McDonald2a5f4132011-04-29 16:32:27 -040029
Matt McDonald2a5f4132011-04-29 16:32:27 -040030
31class MockProtocolBuffer(object):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070032 def __init__(self, data=None):
33 self.data = data
Matt McDonald2a5f4132011-04-29 16:32:27 -040034
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070035 def __eq__(self, other):
36 return self.data == other.data
Matt McDonald2a5f4132011-04-29 16:32:27 -040037
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070038 @classmethod
39 def FromString(cls, string):
40 return cls(string)
Matt McDonald2a5f4132011-04-29 16:32:27 -040041
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070042 def SerializeToString(self):
43 return self.data
Matt McDonald2a5f4132011-04-29 16:32:27 -040044
45
46class Model(unittest.TestCase):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070047 def setUp(self):
48 self.model = ProtocolBufferModel(MockProtocolBuffer)
Matt McDonald2a5f4132011-04-29 16:32:27 -040049
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070050 def test_no_body(self):
51 headers = {}
52 path_params = {}
53 query_params = {}
54 body = None
Matt McDonald2a5f4132011-04-29 16:32:27 -040055
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070056 headers, params, query, body = self.model.request(
57 headers, path_params, query_params, body
58 )
Matt McDonald2a5f4132011-04-29 16:32:27 -040059
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070060 self.assertEqual(headers["accept"], "application/x-protobuf")
61 self.assertTrue("content-type" not in headers)
62 self.assertNotEqual(query, "")
63 self.assertEqual(body, None)
Matt McDonald2a5f4132011-04-29 16:32:27 -040064
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070065 def test_body(self):
66 headers = {}
67 path_params = {}
68 query_params = {}
69 body = MockProtocolBuffer("data")
Matt McDonald2a5f4132011-04-29 16:32:27 -040070
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070071 headers, params, query, body = self.model.request(
72 headers, path_params, query_params, body
73 )
Matt McDonald2a5f4132011-04-29 16:32:27 -040074
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070075 self.assertEqual(headers["accept"], "application/x-protobuf")
76 self.assertEqual(headers["content-type"], "application/x-protobuf")
77 self.assertNotEqual(query, "")
78 self.assertEqual(body, "data")
Matt McDonald2a5f4132011-04-29 16:32:27 -040079
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070080 def test_good_response(self):
81 resp = httplib2.Response({"status": "200"})
82 resp.reason = "OK"
83 content = "data"
Matt McDonald2a5f4132011-04-29 16:32:27 -040084
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070085 content = self.model.response(resp, content)
86 self.assertEqual(content, MockProtocolBuffer("data"))
Matt McDonald2a5f4132011-04-29 16:32:27 -040087
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070088 def test_no_content_response(self):
89 resp = httplib2.Response({"status": "204"})
90 resp.reason = "No Content"
91 content = ""
Matt McDonald2a5f4132011-04-29 16:32:27 -040092
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070093 content = self.model.response(resp, content)
94 self.assertEqual(content, MockProtocolBuffer())
Matt McDonald2a5f4132011-04-29 16:32:27 -040095
96
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070097if __name__ == "__main__":
98 unittest.main()