blob: 02544e1aca922738aac9d2fd9a9e2faa8b6279f2 [file] [log] [blame]
Matt McDonald2a5f4132011-04-29 16:32:27 -04001#!/usr/bin/python2.4
2#
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"""
21
22__author__ = 'mmcdonald@google.com (Matt McDonald)'
23
Matt McDonald2a5f4132011-04-29 16:32:27 -040024import unittest
25import httplib2
John Asmuth864311d2014-04-24 15:46:08 -040026import googleapiclient.model
Matt McDonald2a5f4132011-04-29 16:32:27 -040027
John Asmuth864311d2014-04-24 15:46:08 -040028from googleapiclient.errors import HttpError
29from googleapiclient.model import ProtocolBufferModel
Matt McDonald2a5f4132011-04-29 16:32:27 -040030
Matt McDonald2a5f4132011-04-29 16:32:27 -040031# Python 2.5 requires different modules
32try:
33 from urlparse import parse_qs
34except ImportError:
35 from cgi import parse_qs
36
37
38class MockProtocolBuffer(object):
39 def __init__(self, data=None):
40 self.data = data
41
42 def __eq__(self, other):
43 return self.data == other.data
44
45 @classmethod
46 def FromString(cls, string):
47 return cls(string)
48
49 def SerializeToString(self):
50 return self.data
51
52
53class Model(unittest.TestCase):
54 def setUp(self):
55 self.model = ProtocolBufferModel(MockProtocolBuffer)
56
57 def test_no_body(self):
58 headers = {}
59 path_params = {}
60 query_params = {}
61 body = None
62
63 headers, params, query, body = self.model.request(
64 headers, path_params, query_params, body)
65
66 self.assertEqual(headers['accept'], 'application/x-protobuf')
67 self.assertTrue('content-type' not in headers)
68 self.assertNotEqual(query, '')
69 self.assertEqual(body, None)
70
71 def test_body(self):
72 headers = {}
73 path_params = {}
74 query_params = {}
75 body = MockProtocolBuffer('data')
76
77 headers, params, query, body = self.model.request(
78 headers, path_params, query_params, body)
79
80 self.assertEqual(headers['accept'], 'application/x-protobuf')
81 self.assertEqual(headers['content-type'], 'application/x-protobuf')
82 self.assertNotEqual(query, '')
83 self.assertEqual(body, 'data')
84
85 def test_good_response(self):
86 resp = httplib2.Response({'status': '200'})
87 resp.reason = 'OK'
88 content = 'data'
89
90 content = self.model.response(resp, content)
91 self.assertEqual(content, MockProtocolBuffer('data'))
92
93 def test_no_content_response(self):
94 resp = httplib2.Response({'status': '204'})
95 resp.reason = 'No Content'
96 content = ''
97
98 content = self.model.response(resp, content)
99 self.assertEqual(content, MockProtocolBuffer())
100
101
102if __name__ == '__main__':
103 unittest.main()