| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Copyright 2014 Google Inc. All Rights Reserved. |
| 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 |
""" |
| 21 |
from __future__ import absolute_import |
| 22 |
|
| 23 |
__author__ = "[email protected] (Matt McDonald)" |
| 24 |
|
| 25 |
import unittest |
| 26 |
|
| 27 |
import httplib2 |
| 28 |
|
| 29 |
from googleapiclient.model import ProtocolBufferModel |
| 30 |
|
| 31 |
|
| 32 |
class MockProtocolBuffer(object): |
| 33 |
def __init__(self, data=None): |
| 34 |
self.data = data |
| 35 |
|
| 36 |
def __eq__(self, other): |
| 37 |
return self.data == other.data |
| 38 |
|
| 39 |
@classmethod |
| 40 |
def FromString(cls, string): |
| 41 |
return cls(string) |
| 42 |
|
| 43 |
def SerializeToString(self): |
| 44 |
return self.data |
| 45 |
|
| 46 |
|
| 47 |
class Model(unittest.TestCase): |
| 48 |
def setUp(self): |
| 49 |
self.model = ProtocolBufferModel(MockProtocolBuffer) |
| 50 |
|
| 51 |
def test_no_body(self): |
| 52 |
headers = {} |
| 53 |
path_params = {} |
| 54 |
query_params = {} |
| 55 |
body = None |
| 56 |
|
| 57 |
headers, params, query, body = self.model.request( |
| 58 |
headers, path_params, query_params, body |
| 59 |
) |
| 60 |
|
| 61 |
self.assertEqual(headers["accept"], "application/x-protobuf") |
| 62 |
self.assertTrue("content-type" not in headers) |
| 63 |
self.assertNotEqual(query, "") |
| 64 |
self.assertEqual(body, None) |
| 65 |
|
| 66 |
def test_body(self): |
| 67 |
headers = {} |
| 68 |
path_params = {} |
| 69 |
query_params = {} |
| 70 |
body = MockProtocolBuffer("data") |
| 71 |
|
| 72 |
headers, params, query, body = self.model.request( |
| 73 |
headers, path_params, query_params, body |
| 74 |
) |
| 75 |
|
| 76 |
self.assertEqual(headers["accept"], "application/x-protobuf") |
| 77 |
self.assertEqual(headers["content-type"], "application/x-protobuf") |
| 78 |
self.assertNotEqual(query, "") |
| 79 |
self.assertEqual(body, "data") |
| 80 |
|
| 81 |
def test_good_response(self): |
| 82 |
resp = httplib2.Response({"status": "200"}) |
| 83 |
resp.reason = "OK" |
| 84 |
content = "data" |
| 85 |
|
| 86 |
content = self.model.response(resp, content) |
| 87 |
self.assertEqual(content, MockProtocolBuffer("data")) |
| 88 |
|
| 89 |
def test_no_content_response(self): |
| 90 |
resp = httplib2.Response({"status": "204"}) |
| 91 |
resp.reason = "No Content" |
| 92 |
content = "" |
| 93 |
|
| 94 |
content = self.model.response(resp, content) |
| 95 |
self.assertEqual(content, MockProtocolBuffer()) |
| 96 |
|
| 97 |
|
| 98 |
if __name__ == "__main__": |
| 99 |
unittest.main() |