~j5-dev/+junk/cherrypy3-3.2.0rc1

« back to all changes in this revision

Viewing changes to cherrypy/test/test_json.py

  • Committer: steveh at sjsoft
  • Date: 2010-07-01 13:07:15 UTC
  • Revision ID: steveh@sjsoft.com-20100701130715-w56oim8346qzqlka
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from cherrypy.test import test, helper
 
2
test.prefer_parent_path()
 
3
 
 
4
import cherrypy
 
5
 
 
6
from cherrypy.lib.jsontools import json
 
7
if json is None:
 
8
    print "skipped (simplejson not found) "
 
9
else:
 
10
    def setup_server():
 
11
        class Root(object):
 
12
            def plain(self):
 
13
                return 'hello'
 
14
            plain.exposed = True
 
15
 
 
16
            def json_string(self):
 
17
                return 'hello'
 
18
            json_string.exposed = True
 
19
            json_string._cp_config = {'tools.json_out.on': True}
 
20
 
 
21
            def json_list(self):
 
22
                return ['a', 'b', 42]
 
23
            json_list.exposed = True
 
24
            json_list._cp_config = {'tools.json_out.on': True}
 
25
 
 
26
            def json_dict(self):
 
27
                return {'answer': 42}
 
28
            json_dict.exposed = True
 
29
            json_dict._cp_config = {'tools.json_out.on': True}
 
30
 
 
31
            def json_post(self):
 
32
                if cherrypy.request.json == [13, 'c']:
 
33
                    return 'ok'
 
34
                else:
 
35
                    return 'nok'
 
36
            json_post.exposed = True
 
37
            json_post._cp_config = {'tools.json_in.on': True}
 
38
 
 
39
        root = Root()
 
40
        cherrypy.tree.mount(root)
 
41
 
 
42
    class JsonTest(helper.CPWebCase):
 
43
        def test_json_output(self):
 
44
            self.getPage("/plain")
 
45
            self.assertBody("hello")
 
46
 
 
47
            self.getPage("/json_string")
 
48
            self.assertBody('"hello"')
 
49
 
 
50
            self.getPage("/json_list")
 
51
            self.assertBody('["a", "b", 42]')
 
52
 
 
53
            self.getPage("/json_dict")
 
54
            self.assertBody('{"answer": 42}')
 
55
 
 
56
        def test_json_input(self):
 
57
            body = '[13, "c"]'
 
58
            headers = [('Content-Type', 'application/json'),
 
59
                       ('Content-Length', str(len(body)))]
 
60
            self.getPage("/json_post", method="POST", headers=headers, body=body)
 
61
            self.assertBody('ok')
 
62
            
 
63
            body = '[13, "c"]'
 
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')
 
68
            
 
69
            body = '[13, -]'
 
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')
 
74
 
 
75
if __name__ == '__main__':
 
76
    helper.testmain()
 
77