NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

test_json.py (2561B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\test\test_json.py
      2 import cherrypy
      3 from cherrypy.test import helper
      4 from cherrypy._cpcompat import json
      5 
      6 class JsonTest(helper.CPWebCase):
      7 
      8     def setup_server():
      9 
     10         class Root(object):
     11 
     12             def plain(self):
     13                 return 'hello'
     14 
     15             plain.exposed = True
     16 
     17             def json_string(self):
     18                 return 'hello'
     19 
     20             json_string.exposed = True
     21             json_string._cp_config = {'tools.json_out.on': True}
     22 
     23             def json_list(self):
     24                 return ['a', 'b', 42]
     25 
     26             json_list.exposed = True
     27             json_list._cp_config = {'tools.json_out.on': True}
     28 
     29             def json_dict(self):
     30                 return {'answer': 42}
     31 
     32             json_dict.exposed = True
     33             json_dict._cp_config = {'tools.json_out.on': True}
     34 
     35             def json_post(self):
     36                 if cherrypy.request.json == [13, 'c']:
     37                     return 'ok'
     38                 else:
     39                     return 'nok'
     40 
     41             json_post.exposed = True
     42             json_post._cp_config = {'tools.json_in.on': True}
     43 
     44         root = Root()
     45         cherrypy.tree.mount(root)
     46 
     47     setup_server = staticmethod(setup_server)
     48 
     49     def test_json_output(self):
     50         if json is None:
     51             self.skip('json not found ')
     52             return
     53         self.getPage('/plain')
     54         self.assertBody('hello')
     55         self.getPage('/json_string')
     56         self.assertBody('"hello"')
     57         self.getPage('/json_list')
     58         self.assertBody('["a", "b", 42]')
     59         self.getPage('/json_dict')
     60         self.assertBody('{"answer": 42}')
     61 
     62     def test_json_input(self):
     63         if json is None:
     64             self.skip('json not found ')
     65             return
     66         body = '[13, "c"]'
     67         headers = [('Content-Type', 'application/json'), ('Content-Length', str(len(body)))]
     68         self.getPage('/json_post', method='POST', headers=headers, body=body)
     69         self.assertBody('ok')
     70         body = '[13, "c"]'
     71         headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(body)))]
     72         self.getPage('/json_post', method='POST', headers=headers, body=body)
     73         self.assertStatus(415, 'Expected an application/json content type')
     74         body = '[13, -]'
     75         headers = [('Content-Type', 'application/json'), ('Content-Length', str(len(body)))]
     76         self.getPage('/json_post', method='POST', headers=headers, body=body)
     77         self.assertStatus(400, 'Invalid JSON document')