Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 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 | |
| 22 | __author__ = 'mmcdonald@google.com (Matt McDonald)' |
| 23 | |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 24 | import unittest |
| 25 | import httplib2 |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 26 | import googleapiclient.model |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 27 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 28 | from googleapiclient.errors import HttpError |
| 29 | from googleapiclient.model import ProtocolBufferModel |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 30 | |
Matt McDonald | 2a5f413 | 2011-04-29 16:32:27 -0400 | [diff] [blame] | 31 | # Python 2.5 requires different modules |
| 32 | try: |
| 33 | from urlparse import parse_qs |
| 34 | except ImportError: |
| 35 | from cgi import parse_qs |
| 36 | |
| 37 | |
| 38 | class 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 | |
| 53 | class 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 | |
| 102 | if __name__ == '__main__': |
| 103 | unittest.main() |