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