Home | Trees | Indices | Help |
|
---|
|
1 import cherrypy 2 from cherrypy.test import helper 3 4 from cherrypy._cpcompat import json 5 6 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 def json_cached(self): 40 return 'hello there' 41 json_cached.exposed = True 42 json_cached._cp_config = { 43 'tools.json_out.on': True, 44 'tools.caching.on': True, 45 } 46 47 root = Root() 48 cherrypy.tree.mount(root) 49 setup_server = staticmethod(setup_server) 5052 if json is None: 53 self.skip("json not found ") 54 return 55 56 self.getPage("/plain") 57 self.assertBody("hello") 58 59 self.getPage("/json_string") 60 self.assertBody('"hello"') 61 62 self.getPage("/json_list") 63 self.assertBody('["a", "b", 42]') 64 65 self.getPage("/json_dict") 66 self.assertBody('{"answer": 42}')6769 if json is None: 70 self.skip("json not found ") 71 return 72 73 body = '[13, "c"]' 74 headers = [('Content-Type', 'application/json'), 75 ('Content-Length', str(len(body)))] 76 self.getPage("/json_post", method="POST", headers=headers, body=body) 77 self.assertBody('ok') 78 79 body = '[13, "c"]' 80 headers = [('Content-Type', 'text/plain'), 81 ('Content-Length', str(len(body)))] 82 self.getPage("/json_post", method="POST", headers=headers, body=body) 83 self.assertStatus(415, 'Expected an application/json content type') 84 85 body = '[13, -]' 86 headers = [('Content-Type', 'application/json'), 87 ('Content-Length', str(len(body)))] 88 self.getPage("/json_post", method="POST", headers=headers, body=body) 89 self.assertStatus(400, 'Invalid JSON document')9092 if json is None: 93 self.skip("json not found ") 94 return 95 96 self.getPage("/json_cached") 97 self.assertStatus(200, '"hello"') 98 99 self.getPage("/json_cached") # 2'nd time to hit cache 100 self.assertStatus(200, '"hello"')101
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Tue Dec 2 09:59:42 2014 | http://epydoc.sourceforge.net |