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 | |
| 24 | from apiclient.discovery import JsonModel |
| 25 | import os |
| 26 | import unittest |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 27 | |
| 28 | # Python 2.5 requires different modules |
| 29 | try: |
| 30 | from urlparse import parse_qs |
| 31 | except ImportError: |
| 32 | from cgi import parse_qs |
| 33 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 34 | |
| 35 | |
| 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 | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 67 | def test_json_build_query(self): |
| 68 | model = JsonModel() |
| 69 | |
| 70 | headers = {} |
| 71 | path_params = {} |
| 72 | query_params = {'foo': 1, 'bar': u'\N{COMET}'} |
| 73 | body = {} |
| 74 | |
| 75 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 76 | |
| 77 | self.assertEqual(headers['accept'], 'application/json') |
| 78 | self.assertEqual(headers['content-type'], 'application/json') |
| 79 | |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 80 | query_dict = parse_qs(query) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 81 | self.assertEqual(query_dict['foo'], ['1']) |
| 82 | self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')]) |
| 83 | self.assertEqual(body, '{"data": {}}') |
| 84 | |
| 85 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 86 | if __name__ == '__main__': |
| 87 | unittest.main() |