1#!/usr/bin/env python 2# 3# Copyright 2014 Google Inc. All Rights Reserved. 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""" 21from __future__ import absolute_import 22 23__author__ = 'mmcdonald@google.com (Matt McDonald)' 24 25import unittest2 as unittest 26import httplib2 27import googleapiclient.model 28 29from googleapiclient.errors import HttpError 30from googleapiclient.model import ProtocolBufferModel 31 32from six.moves.urllib.parse import parse_qs 33 34 35class MockProtocolBuffer(object): 36 def __init__(self, data=None): 37 self.data = data 38 39 def __eq__(self, other): 40 return self.data == other.data 41 42 @classmethod 43 def FromString(cls, string): 44 return cls(string) 45 46 def SerializeToString(self): 47 return self.data 48 49 50class Model(unittest.TestCase): 51 def setUp(self): 52 self.model = ProtocolBufferModel(MockProtocolBuffer) 53 54 def test_no_body(self): 55 headers = {} 56 path_params = {} 57 query_params = {} 58 body = None 59 60 headers, params, query, body = self.model.request( 61 headers, path_params, query_params, body) 62 63 self.assertEqual(headers['accept'], 'application/x-protobuf') 64 self.assertTrue('content-type' not in headers) 65 self.assertNotEqual(query, '') 66 self.assertEqual(body, None) 67 68 def test_body(self): 69 headers = {} 70 path_params = {} 71 query_params = {} 72 body = MockProtocolBuffer('data') 73 74 headers, params, query, body = self.model.request( 75 headers, path_params, query_params, body) 76 77 self.assertEqual(headers['accept'], 'application/x-protobuf') 78 self.assertEqual(headers['content-type'], 'application/x-protobuf') 79 self.assertNotEqual(query, '') 80 self.assertEqual(body, 'data') 81 82 def test_good_response(self): 83 resp = httplib2.Response({'status': '200'}) 84 resp.reason = 'OK' 85 content = 'data' 86 87 content = self.model.response(resp, content) 88 self.assertEqual(content, MockProtocolBuffer('data')) 89 90 def test_no_content_response(self): 91 resp = httplib2.Response({'status': '204'}) 92 resp.reason = 'No Content' 93 content = '' 94 95 content = self.model.response(resp, content) 96 self.assertEqual(content, MockProtocolBuffer()) 97 98 99if __name__ == '__main__': 100 unittest.main() 101