2 from cherrypy
.test
import helper
4 from cherrypy
._cpcompat
import json
6 class JsonTest(helper
.CPWebCase
):
13 def json_string(self
):
15 json_string
.exposed
= True
16 json_string
._cp
_config
= {'tools.json_out.on': True}
20 json_list
.exposed
= True
21 json_list
._cp
_config
= {'tools.json_out.on': True}
25 json_dict
.exposed
= True
26 json_dict
._cp
_config
= {'tools.json_out.on': True}
29 if cherrypy
.request
.json
== [13, 'c']:
33 json_post
.exposed
= True
34 json_post
._cp
_config
= {'tools.json_in.on': True}
37 cherrypy
.tree
.mount(root
)
38 setup_server
= staticmethod(setup_server
)
40 def test_json_output(self
):
42 self
.skip("json not found ")
45 self
.getPage("/plain")
46 self
.assertBody("hello")
48 self
.getPage("/json_string")
49 self
.assertBody('"hello"')
51 self
.getPage("/json_list")
52 self
.assertBody('["a", "b", 42]')
54 self
.getPage("/json_dict")
55 self
.assertBody('{"answer": 42}')
57 def test_json_input(self
):
59 self
.skip("json not found ")
63 headers
= [('Content-Type', 'application/json'),
64 ('Content-Length', str(len(body
)))]
65 self
.getPage("/json_post", method
="POST", headers
=headers
, body
=body
)
69 headers
= [('Content-Type', 'text/plain'),
70 ('Content-Length', str(len(body
)))]
71 self
.getPage("/json_post", method
="POST", headers
=headers
, body
=body
)
72 self
.assertStatus(415, 'Expected an application/json content type')
75 headers
= [('Content-Type', 'application/json'),
76 ('Content-Length', str(len(body
)))]
77 self
.getPage("/json_post", method
="POST", headers
=headers
, body
=body
)
78 self
.assertStatus(400, 'Invalid JSON document')