Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 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. |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 16 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 17 | """JSON Model tests |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 18 | |
Joe Gregorio | 6d5e94f | 2010-08-25 23:49:30 -0400 | [diff] [blame] | 19 | Unit tests for the JSON model. |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 20 | """ |
| 21 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 23 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 24 | import os |
| 25 | import unittest |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 26 | import httplib2 |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 27 | |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 28 | from apiclient.model import JsonModel |
| 29 | from apiclient.errors import HttpError |
| 30 | |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [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 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 37 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 38 | class Model(unittest.TestCase): |
| 39 | def test_json_no_body(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 40 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 41 | |
| 42 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 43 | path_params = {} |
| 44 | query_params = {} |
| 45 | body = None |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 46 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 47 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 48 | |
| 49 | self.assertEqual(headers['accept'], 'application/json') |
| 50 | self.assertTrue('content-type' not in headers) |
| 51 | self.assertNotEqual(query, '') |
| 52 | self.assertEqual(body, None) |
| 53 | |
| 54 | def test_json_body(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 55 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 56 | |
| 57 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 58 | path_params = {} |
| 59 | query_params = {} |
| 60 | body = {} |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 61 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 62 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 63 | |
| 64 | self.assertEqual(headers['accept'], 'application/json') |
| 65 | self.assertEqual(headers['content-type'], 'application/json') |
| 66 | self.assertNotEqual(query, '') |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 67 | self.assertEqual(body, '{}') |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 68 | |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 69 | def test_json_body_data_wrapper(self): |
| 70 | model = JsonModel(data_wrapper=True) |
| 71 | |
| 72 | headers = {} |
| 73 | path_params = {} |
| 74 | query_params = {} |
| 75 | body = {} |
| 76 | |
| 77 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 78 | |
| 79 | self.assertEqual(headers['accept'], 'application/json') |
| 80 | self.assertEqual(headers['content-type'], 'application/json') |
| 81 | self.assertNotEqual(query, '') |
| 82 | self.assertEqual(body, '{"data": {}}') |
| 83 | |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 84 | def test_json_body_default_data(self): |
| 85 | """Test that a 'data' wrapper doesn't get added if one is already present.""" |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 86 | model = JsonModel(data_wrapper=True) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 87 | |
| 88 | headers = {} |
| 89 | path_params = {} |
| 90 | query_params = {} |
| 91 | body = {'data': 'foo'} |
| 92 | |
| 93 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 94 | |
| 95 | self.assertEqual(headers['accept'], 'application/json') |
| 96 | self.assertEqual(headers['content-type'], 'application/json') |
| 97 | self.assertNotEqual(query, '') |
| 98 | self.assertEqual(body, '{"data": "foo"}') |
| 99 | |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 100 | def test_json_build_query(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 101 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 102 | |
| 103 | headers = {} |
| 104 | path_params = {} |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 105 | query_params = {'foo': 1, 'bar': u'\N{COMET}', |
| 106 | 'baz': ['fe', 'fi', 'fo', 'fum'], # Repeated parameters |
| 107 | 'qux': []} |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 108 | body = {} |
| 109 | |
| 110 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 111 | |
| 112 | self.assertEqual(headers['accept'], 'application/json') |
| 113 | self.assertEqual(headers['content-type'], 'application/json') |
| 114 | |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 115 | query_dict = parse_qs(query[1:]) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 116 | self.assertEqual(query_dict['foo'], ['1']) |
| 117 | self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')]) |
Joe Gregorio | 61d7e96 | 2011-02-22 22:52:07 -0500 | [diff] [blame] | 118 | self.assertEqual(query_dict['baz'], ['fe', 'fi', 'fo', 'fum']) |
| 119 | self.assertTrue('qux' not in query_dict) |
Joe Gregorio | 913e70d | 2010-11-05 15:38:23 -0400 | [diff] [blame] | 120 | self.assertEqual(body, '{}') |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 121 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 122 | def test_user_agent(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 123 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 124 | |
| 125 | headers = {'user-agent': 'my-test-app/1.23.4'} |
| 126 | path_params = {} |
| 127 | query_params = {} |
| 128 | body = {} |
| 129 | |
| 130 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 131 | |
| 132 | self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0') |
| 133 | |
| 134 | def test_bad_response(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 135 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 136 | resp = httplib2.Response({'status': '401'}) |
| 137 | resp.reason = 'Unauthorized' |
Joe Gregorio | d4e1456 | 2011-01-04 09:51:45 -0500 | [diff] [blame] | 138 | content = '{"error": {"message": "not authorized"}}' |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 139 | |
| 140 | try: |
| 141 | content = model.response(resp, content) |
| 142 | self.fail('Should have thrown an exception') |
| 143 | except HttpError, e: |
| 144 | self.assertTrue('Unauthorized' in str(e)) |
| 145 | |
| 146 | resp['content-type'] = 'application/json' |
| 147 | |
| 148 | try: |
| 149 | content = model.response(resp, content) |
| 150 | self.fail('Should have thrown an exception') |
| 151 | except HttpError, e: |
| 152 | self.assertTrue('not authorized' in str(e)) |
| 153 | |
| 154 | |
| 155 | def test_good_response(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 156 | model = JsonModel(data_wrapper=True) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 157 | resp = httplib2.Response({'status': '200'}) |
| 158 | resp.reason = 'OK' |
| 159 | content = '{"data": "is good"}' |
| 160 | |
| 161 | content = model.response(resp, content) |
| 162 | self.assertEqual(content, 'is good') |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 163 | |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 164 | def test_good_response_wo_data(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 165 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 166 | resp = httplib2.Response({'status': '200'}) |
| 167 | resp.reason = 'OK' |
| 168 | content = '{"foo": "is good"}' |
| 169 | |
| 170 | content = model.response(resp, content) |
| 171 | self.assertEqual(content, {'foo': 'is good'}) |
| 172 | |
| 173 | def test_good_response_wo_data_str(self): |
Joe Gregorio | d433b2a | 2011-02-22 10:51:51 -0500 | [diff] [blame] | 174 | model = JsonModel(data_wrapper=False) |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 175 | resp = httplib2.Response({'status': '200'}) |
| 176 | resp.reason = 'OK' |
| 177 | content = '"data goes here"' |
| 178 | |
| 179 | content = model.response(resp, content) |
| 180 | self.assertEqual(content, 'data goes here') |
| 181 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 182 | if __name__ == '__main__': |
| 183 | unittest.main() |