blob: 3014ee4aab94051b5366ce3e5ed548c7131ca4a6 [file] [log] [blame]
Joe Gregorioba9ea7f2010-08-19 15:49:04 -04001#!/usr/bin/python2.4
2#
Joe Gregorio6d5e94f2010-08-25 23:49:30 -04003# 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 Gregorioba9ea7f2010-08-19 15:49:04 -040016
Joe Gregorio6d5e94f2010-08-25 23:49:30 -040017"""JSON Model tests
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040018
Joe Gregorio6d5e94f2010-08-25 23:49:30 -040019Unit tests for the JSON model.
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040020"""
21
22__author__ = 'jcgregorio@google.com (Joe Gregorio)'
23
24from apiclient.discovery import JsonModel
25import os
26import unittest
Joe Gregoriofe695fb2010-08-30 12:04:04 -040027import urlparse
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040028
29
30class Model(unittest.TestCase):
31 def test_json_no_body(self):
32 model = JsonModel()
33
34 headers = {}
ade@google.com850cf552010-08-20 23:24:56 +010035 path_params = {}
36 query_params = {}
37 body = None
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040038
ade@google.com850cf552010-08-20 23:24:56 +010039 headers, params, query, body = model.request(headers, path_params, query_params, body)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040040
41 self.assertEqual(headers['accept'], 'application/json')
42 self.assertTrue('content-type' not in headers)
43 self.assertNotEqual(query, '')
44 self.assertEqual(body, None)
45
46 def test_json_body(self):
47 model = JsonModel()
48
49 headers = {}
ade@google.com850cf552010-08-20 23:24:56 +010050 path_params = {}
51 query_params = {}
52 body = {}
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040053
ade@google.com850cf552010-08-20 23:24:56 +010054 headers, params, query, body = model.request(headers, path_params, query_params, body)
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040055
56 self.assertEqual(headers['accept'], 'application/json')
57 self.assertEqual(headers['content-type'], 'application/json')
58 self.assertNotEqual(query, '')
59 self.assertEqual(body, '{"data": {}}')
60
Joe Gregoriofe695fb2010-08-30 12:04:04 -040061 def test_json_build_query(self):
62 model = JsonModel()
63
64 headers = {}
65 path_params = {}
66 query_params = {'foo': 1, 'bar': u'\N{COMET}'}
67 body = {}
68
69 headers, params, query, body = model.request(headers, path_params, query_params, body)
70
71 self.assertEqual(headers['accept'], 'application/json')
72 self.assertEqual(headers['content-type'], 'application/json')
73
74 query_dict = urlparse.parse_qs(query)
75 self.assertEqual(query_dict['foo'], ['1'])
76 self.assertEqual(query_dict['bar'], [u'\N{COMET}'.encode('utf-8')])
77 self.assertEqual(body, '{"data": {}}')
78
79
Joe Gregorioba9ea7f2010-08-19 15:49:04 -040080if __name__ == '__main__':
81 unittest.main()