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