Package cherrypy :: Package test :: Module test_config_server
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_config_server

  1  """Tests for the CherryPy configuration system.""" 
  2   
  3  import os 
  4  import sys 
  5  localDir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
  6  import socket 
  7  import time 
  8   
  9  import cherrypy 
 10   
 11   
 12  #                             Client-side code                             # 
 13   
 14  from cherrypy.test import helper 
 15   
 16   
17 -class ServerConfigTests(helper.CPWebCase):
18
19 - def setup_server():
20 21 class Root: 22 23 def index(self): 24 return cherrypy.request.wsgi_environ['SERVER_PORT']
25 index.exposed = True 26 27 def upload(self, file): 28 return "Size: %s" % len(file.file.read())
29 upload.exposed = True 30 31 def tinyupload(self): 32 return cherrypy.request.body.read() 33 tinyupload.exposed = True 34 tinyupload._cp_config = {'request.body.maxbytes': 100} 35 36 cherrypy.tree.mount(Root()) 37 38 cherrypy.config.update({ 39 'server.socket_host': '0.0.0.0', 40 'server.socket_port': 9876, 41 'server.max_request_body_size': 200, 42 'server.max_request_header_size': 500, 43 'server.socket_timeout': 0.5, 44 45 # Test explicit server.instance 46 'server.2.instance': 'cherrypy._cpwsgi_server.CPWSGIServer', 47 'server.2.socket_port': 9877, 48 49 # Test non-numeric <servername> 50 # Also test default server.instance = builtin server 51 'server.yetanother.socket_port': 9878, 52 }) 53 setup_server = staticmethod(setup_server) 54 55 PORT = 9876 56
57 - def testBasicConfig(self):
58 self.getPage("/") 59 self.assertBody(str(self.PORT))
60
61 - def testAdditionalServers(self):
62 if self.scheme == 'https': 63 return self.skip("not available under ssl") 64 self.PORT = 9877 65 self.getPage("/") 66 self.assertBody(str(self.PORT)) 67 self.PORT = 9878 68 self.getPage("/") 69 self.assertBody(str(self.PORT))
70
71 - def testMaxRequestSizePerHandler(self):
72 if getattr(cherrypy.server, "using_apache", False): 73 return self.skip("skipped due to known Apache differences... ") 74 75 self.getPage('/tinyupload', method="POST", 76 headers=[('Content-Type', 'text/plain'), 77 ('Content-Length', '100')], 78 body="x" * 100) 79 self.assertStatus(200) 80 self.assertBody("x" * 100) 81 82 self.getPage('/tinyupload', method="POST", 83 headers=[('Content-Type', 'text/plain'), 84 ('Content-Length', '101')], 85 body="x" * 101) 86 self.assertStatus(413)
87
88 - def testMaxRequestSize(self):
89 if getattr(cherrypy.server, "using_apache", False): 90 return self.skip("skipped due to known Apache differences... ") 91 92 for size in (500, 5000, 50000): 93 self.getPage("/", headers=[('From', "x" * 500)]) 94 self.assertStatus(413) 95 96 # Test for https://bitbucket.org/cherrypy/cherrypy/issue/421 97 # (Incorrect border condition in readline of SizeCheckWrapper). 98 # This hangs in rev 891 and earlier. 99 lines256 = "x" * 248 100 self.getPage("/", 101 headers=[('Host', '%s:%s' % (self.HOST, self.PORT)), 102 ('From', lines256)]) 103 104 # Test upload 105 cd = ( 106 'Content-Disposition: form-data; ' 107 'name="file"; ' 108 'filename="hello.txt"' 109 ) 110 body = '\r\n'.join([ 111 '--x', 112 cd, 113 'Content-Type: text/plain', 114 '', 115 '%s', 116 '--x--']) 117 partlen = 200 - len(body) 118 b = body % ("x" * partlen) 119 h = [("Content-type", "multipart/form-data; boundary=x"), 120 ("Content-Length", "%s" % len(b))] 121 self.getPage('/upload', h, "POST", b) 122 self.assertBody('Size: %d' % partlen) 123 124 b = body % ("x" * 200) 125 h = [("Content-type", "multipart/form-data; boundary=x"), 126 ("Content-Length", "%s" % len(b))] 127 self.getPage('/upload', h, "POST", b) 128 self.assertStatus(413)
129