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 | |
| 36 | |
| 37 | class Model(unittest.TestCase): |
| 38 | def test_json_no_body(self): |
| 39 | model = JsonModel() |
| 40 | |
| 41 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 42 | path_params = {} |
| 43 | query_params = {} |
| 44 | body = None |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 45 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 46 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 47 | |
| 48 | self.assertEqual(headers['accept'], 'application/json') |
| 49 | self.assertTrue('content-type' not in headers) |
| 50 | self.assertNotEqual(query, '') |
| 51 | self.assertEqual(body, None) |
| 52 | |
| 53 | def test_json_body(self): |
| 54 | model = JsonModel() |
| 55 | |
| 56 | headers = {} |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 57 | path_params = {} |
| 58 | query_params = {} |
| 59 | body = {} |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 60 | |
ade@google.com | 850cf55 | 2010-08-20 23:24:56 +0100 | [diff] [blame] | 61 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 62 | |
| 63 | self.assertEqual(headers['accept'], 'application/json') |
| 64 | self.assertEqual(headers['content-type'], 'application/json') |
| 65 | self.assertNotEqual(query, '') |
| 66 | self.assertEqual(body, '{"data": {}}') |
| 67 | |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 68 | def test_json_build_query(self): |
| 69 | model = JsonModel() |
| 70 | |
| 71 | headers = {} |
| 72 | path_params = {} |
| 73 | query_params = {'foo': 1, 'bar': u'\N{COMET}'} |
| 74 | body = {} |
| 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 | |
ade@google.com | d69e5e4 | 2010-08-31 15:28:20 +0100 | [diff] [blame] | 81 | query_dict = parse_qs(query) |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 82 | self.assertEqual(query_dict['foo'], ['1']) |
| 83 | self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')]) |
| 84 | self.assertEqual(body, '{"data": {}}') |
| 85 | |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame^] | 86 | def test_user_agent(self): |
| 87 | model = JsonModel() |
| 88 | |
| 89 | headers = {'user-agent': 'my-test-app/1.23.4'} |
| 90 | path_params = {} |
| 91 | query_params = {} |
| 92 | body = {} |
| 93 | |
| 94 | headers, params, query, body = model.request(headers, path_params, query_params, body) |
| 95 | |
| 96 | self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0') |
| 97 | |
| 98 | def test_bad_response(self): |
| 99 | model = JsonModel() |
| 100 | resp = httplib2.Response({'status': '401'}) |
| 101 | resp.reason = 'Unauthorized' |
| 102 | content = '{"error": "not authorized"}' |
| 103 | |
| 104 | try: |
| 105 | content = model.response(resp, content) |
| 106 | self.fail('Should have thrown an exception') |
| 107 | except HttpError, e: |
| 108 | self.assertTrue('Unauthorized' in str(e)) |
| 109 | |
| 110 | resp['content-type'] = 'application/json' |
| 111 | |
| 112 | try: |
| 113 | content = model.response(resp, content) |
| 114 | self.fail('Should have thrown an exception') |
| 115 | except HttpError, e: |
| 116 | self.assertTrue('not authorized' in str(e)) |
| 117 | |
| 118 | |
| 119 | def test_good_response(self): |
| 120 | model = JsonModel() |
| 121 | resp = httplib2.Response({'status': '200'}) |
| 122 | resp.reason = 'OK' |
| 123 | content = '{"data": "is good"}' |
| 124 | |
| 125 | content = model.response(resp, content) |
| 126 | self.assertEqual(content, 'is good') |
Joe Gregorio | fe695fb | 2010-08-30 12:04:04 -0400 | [diff] [blame] | 127 | |
Joe Gregorio | ba9ea7f | 2010-08-19 15:49:04 -0400 | [diff] [blame] | 128 | if __name__ == '__main__': |
| 129 | unittest.main() |