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 | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 24 | from apiclient.discovery import JsonModel, HttpError |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 25 | import os |
| 26 | import unittest |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 27 | import httplib2 |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 28 | |
| 29 | # Python 2.5 requires different modules |
| 30 | try: |
| 31 | from urlparse import parse_qs |
| 32 | except ImportError: |
| 33 | from cgi import parse_qs |
| 34 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 35 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 36 | class Model(unittest.TestCase): |
| 37 | def test_json_no_body(self): |
| 38 | model = JsonModel() |
| 39 | |
| 40 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 41 | path_params = {} |
| 42 | query_params = {} |
| 43 | body = None |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 44 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 45 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 46 | |
| 47 | self.assertEqual(headers['accept'], 'application/json') |
| 48 | self.assertTrue('content-type' not in headers) |
| 49 | self.assertNotEqual(query, '') |
| 50 | self.assertEqual(body, None) |
| 51 | |
| 52 | def test_json_body(self): |
| 53 | model = JsonModel() |
| 54 | |
| 55 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 56 | path_params = {} |
| 57 | query_params = {} |
| 58 | body = {} |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 59 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 60 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 61 | |
| 62 | self.assertEqual(headers['accept'], 'application/json') |
| 63 | self.assertEqual(headers['content-type'], 'application/json') |
| 64 | self.assertNotEqual(query, '') |
| 65 | self.assertEqual(body, '{"data": {}}') |
| 66 | |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 67 | def test_json_body_default_data(self): |
| 68 | """Test that a 'data' wrapper doesn't get added if one is already present.""" |
| 69 | model = JsonModel() |
| 70 | |
| 71 | headers = {} |
| 72 | path_params = {} |
| 73 | query_params = {} |
| 74 | body = {'data': 'foo'} |
| 75 | |
| 76 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 77 | |
| 78 | self.assertEqual(headers['accept'], 'application/json') |
| 79 | self.assertEqual(headers['content-type'], 'application/json') |
| 80 | self.assertNotEqual(query, '') |
| 81 | self.assertEqual(body, '{"data": "foo"}') |
| 82 | |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 83 | def test_json_build_query(self): |
| 84 | model = JsonModel() |
| 85 | |
| 86 | headers = {} |
| 87 | path_params = {} |
| 88 | query_params = {'foo': 1, 'bar': u'\N{COMET}'} |
| 89 | body = {} |
| 90 | |
| 91 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 92 | |
| 93 | self.assertEqual(headers['accept'], 'application/json') |
| 94 | self.assertEqual(headers['content-type'], 'application/json') |
| 95 | |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 96 | query_dict = parse_qs(query) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 97 | self.assertEqual(query_dict['foo'], ['1']) |
| 98 | self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')]) |
| 99 | self.assertEqual(body, '{"data": {}}') |
| 100 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 101 | def test_user_agent(self): |
| 102 | model = JsonModel() |
| 103 | |
| 104 | headers = {'user-agent': 'my-test-app/1.23.4'} |
| 105 | path_params = {} |
| 106 | query_params = {} |
| 107 | body = {} |
| 108 | |
| 109 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 110 | |
| 111 | self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0') |
| 112 | |
| 113 | def test_bad_response(self): |
| 114 | model = JsonModel() |
| 115 | resp = httplib2.Response({'status': '401'}) |
| 116 | resp.reason = 'Unauthorized' |
| 117 | content = '{"error": "not authorized"}' |
| 118 | |
| 119 | try: |
| 120 | content = model.response(resp, content) |
| 121 | self.fail('Should have thrown an exception') |
| 122 | except HttpError, e: |
| 123 | self.assertTrue('Unauthorized' in str(e)) |
| 124 | |
| 125 | resp['content-type'] = 'application/json' |
| 126 | |
| 127 | try: |
| 128 | content = model.response(resp, content) |
| 129 | self.fail('Should have thrown an exception') |
| 130 | except HttpError, e: |
| 131 | self.assertTrue('not authorized' in str(e)) |
| 132 | |
| 133 | |
| 134 | def test_good_response(self): |
| 135 | model = JsonModel() |
| 136 | resp = httplib2.Response({'status': '200'}) |
| 137 | resp.reason = 'OK' |
| 138 | content = '{"data": "is good"}' |
| 139 | |
| 140 | content = model.response(resp, content) |
| 141 | self.assertEqual(content, 'is good') |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 142 | |
Joe Gregorio | 78a508d | 2010-10-26 16:36:36 -0400 | [diff] [blame] | 143 | def test_good_response_wo_data(self): |
| 144 | model = JsonModel() |
| 145 | resp = httplib2.Response({'status': '200'}) |
| 146 | resp.reason = 'OK' |
| 147 | content = '{"foo": "is good"}' |
| 148 | |
| 149 | content = model.response(resp, content) |
| 150 | self.assertEqual(content, {'foo': 'is good'}) |
| 151 | |
| 152 | def test_good_response_wo_data_str(self): |
| 153 | model = JsonModel() |
| 154 | resp = httplib2.Response({'status': '200'}) |
| 155 | resp.reason = 'OK' |
| 156 | content = '"data goes here"' |
| 157 | |
| 158 | content = model.response(resp, content) |
| 159 | self.assertEqual(content, 'data goes here') |
| 160 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 161 | if __name__ == '__main__': |
| 162 | unittest.main() |