1
from cherrypy.test import test, helper
2
test.prefer_parent_path()
6
from cherrypy.lib.jsontools import json
8
print "skipped (simplejson not found) "
16
def json_string(self):
18
json_string.exposed = True
19
json_string._cp_config = {'tools.json_out.on': True}
23
json_list.exposed = True
24
json_list._cp_config = {'tools.json_out.on': True}
28
json_dict.exposed = True
29
json_dict._cp_config = {'tools.json_out.on': True}
32
if cherrypy.request.json == [13, 'c']:
36
json_post.exposed = True
37
json_post._cp_config = {'tools.json_in.on': True}
40
cherrypy.tree.mount(root)
42
class JsonTest(helper.CPWebCase):
43
def test_json_output(self):
44
self.getPage("/plain")
45
self.assertBody("hello")
47
self.getPage("/json_string")
48
self.assertBody('"hello"')
50
self.getPage("/json_list")
51
self.assertBody('["a", "b", 42]')
53
self.getPage("/json_dict")
54
self.assertBody('{"answer": 42}')
56
def test_json_input(self):
58
headers = [('Content-Type', 'application/json'),
59
('Content-Length', str(len(body)))]
60
self.getPage("/json_post", method="POST", headers=headers, body=body)
64
headers = [('Content-Type', 'text/plain'),
65
('Content-Length', str(len(body)))]
66
self.getPage("/json_post", method="POST", headers=headers, body=body)
67
self.assertStatus(415, 'Expected an application/json content type')
70
headers = [('Content-Type', 'application/json'),
71
('Content-Length', str(len(body)))]
72
self.getPage("/json_post", method="POST", headers=headers, body=body)
73
self.assertStatus(400, 'Invalid JSON document')
75
if __name__ == '__main__':