Add support for Protocol Buffers as an API serialization format
diff --git a/tests/test_json_model.py b/tests/test_json_model.py
index 29130d2..7ab2f98 100644
--- a/tests/test_json_model.py
+++ b/tests/test_json_model.py
@@ -31,7 +31,6 @@
from apiclient.anyjson import simplejson
from apiclient.errors import HttpError
from apiclient.model import JsonModel
-from apiclient.model import LoggingJsonModel
FLAGS = gflags.FLAGS
@@ -186,10 +185,16 @@
content = model.response(resp, content)
self.assertEqual(content, 'data goes here')
+ def test_no_content_response(self):
+ model = JsonModel(data_wrapper=False)
+ resp = httplib2.Response({'status': '204'})
+ resp.reason = 'No Content'
+ content = ''
-class LoggingModel(unittest.TestCase):
+ content = model.response(resp, content)
+ self.assertEqual(content, {})
- def test_logging_json_model(self):
+ def test_logging(self):
class MockLogging(object):
def __init__(self):
self.info_record = []
@@ -206,10 +211,11 @@
self.status = items['status']
for key, value in items.iteritems():
self[key] = value
+ old_logging = apiclient.model.logging
apiclient.model.logging = MockLogging()
apiclient.model.FLAGS = copy.deepcopy(FLAGS)
apiclient.model.FLAGS.dump_request_response = True
- model = LoggingJsonModel()
+ model = JsonModel()
request_body = {
'field1': 'value1',
'field2': 'value2'
@@ -234,7 +240,7 @@
request_body)
self.assertEqual(apiclient.model.logging.info_record[-1],
'--response-end--')
-
+ apiclient.model.logging = old_logging
if __name__ == '__main__':
diff --git a/tests/test_protobuf_model.py b/tests/test_protobuf_model.py
new file mode 100644
index 0000000..02e8846
--- /dev/null
+++ b/tests/test_protobuf_model.py
@@ -0,0 +1,106 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2010 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Protocol Buffer Model tests
+
+Unit tests for the Protocol Buffer model.
+"""
+
+__author__ = 'mmcdonald@google.com (Matt McDonald)'
+
+import gflags
+import unittest
+import httplib2
+import apiclient.model
+
+from apiclient.errors import HttpError
+from apiclient.model import ProtocolBufferModel
+
+FLAGS = gflags.FLAGS
+
+# Python 2.5 requires different modules
+try:
+ from urlparse import parse_qs
+except ImportError:
+ from cgi import parse_qs
+
+
+class MockProtocolBuffer(object):
+ def __init__(self, data=None):
+ self.data = data
+
+ def __eq__(self, other):
+ return self.data == other.data
+
+ @classmethod
+ def FromString(cls, string):
+ return cls(string)
+
+ def SerializeToString(self):
+ return self.data
+
+
+class Model(unittest.TestCase):
+ def setUp(self):
+ self.model = ProtocolBufferModel(MockProtocolBuffer)
+
+ def test_no_body(self):
+ headers = {}
+ path_params = {}
+ query_params = {}
+ body = None
+
+ headers, params, query, body = self.model.request(
+ headers, path_params, query_params, body)
+
+ self.assertEqual(headers['accept'], 'application/x-protobuf')
+ self.assertTrue('content-type' not in headers)
+ self.assertNotEqual(query, '')
+ self.assertEqual(body, None)
+
+ def test_body(self):
+ headers = {}
+ path_params = {}
+ query_params = {}
+ body = MockProtocolBuffer('data')
+
+ headers, params, query, body = self.model.request(
+ headers, path_params, query_params, body)
+
+ self.assertEqual(headers['accept'], 'application/x-protobuf')
+ self.assertEqual(headers['content-type'], 'application/x-protobuf')
+ self.assertNotEqual(query, '')
+ self.assertEqual(body, 'data')
+
+ def test_good_response(self):
+ resp = httplib2.Response({'status': '200'})
+ resp.reason = 'OK'
+ content = 'data'
+
+ content = self.model.response(resp, content)
+ self.assertEqual(content, MockProtocolBuffer('data'))
+
+ def test_no_content_response(self):
+ resp = httplib2.Response({'status': '204'})
+ resp.reason = 'No Content'
+ content = ''
+
+ content = self.model.response(resp, content)
+ self.assertEqual(content, MockProtocolBuffer())
+
+
+if __name__ == '__main__':
+ unittest.main()