1 """Tests for the CherryPy configuration system."""
2
3 import os, sys
4 localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
5 import socket
6 import time
7
8 import cherrypy
9
10
11
12
13 from cherrypy.test import helper
14
16
18
19 class Root:
20 def index(self):
21 return cherrypy.request.wsgi_environ['SERVER_PORT']
22 index.exposed = True
23
24 def upload(self, file):
25 return "Size: %s" % len(file.file.read())
26 upload.exposed = True
27
28 def tinyupload(self):
29 return cherrypy.request.body.read()
30 tinyupload.exposed = True
31 tinyupload._cp_config = {'request.body.maxbytes': 100}
32
33 cherrypy.tree.mount(Root())
34
35 cherrypy.config.update({
36 'server.socket_host': '0.0.0.0',
37 'server.socket_port': 9876,
38 'server.max_request_body_size': 200,
39 'server.max_request_header_size': 500,
40 'server.socket_timeout': 0.5,
41
42
43 'server.2.instance': 'cherrypy._cpwsgi_server.CPWSGIServer',
44 'server.2.socket_port': 9877,
45
46
47
48 'server.yetanother.socket_port': 9878,
49 })
50 setup_server = staticmethod(setup_server)
51
52 PORT = 9876
53
57
67
69 if getattr(cherrypy.server, "using_apache", False):
70 return self.skip("skipped due to known Apache differences... ")
71
72 self.getPage('/tinyupload', method="POST",
73 headers=[('Content-Type', 'text/plain'),
74 ('Content-Length', '100')],
75 body="x" * 100)
76 self.assertStatus(200)
77 self.assertBody("x" * 100)
78
79 self.getPage('/tinyupload', method="POST",
80 headers=[('Content-Type', 'text/plain'),
81 ('Content-Length', '101')],
82 body="x" * 101)
83 self.assertStatus(413)
84
86 if getattr(cherrypy.server, "using_apache", False):
87 return self.skip("skipped due to known Apache differences... ")
88
89 for size in (500, 5000, 50000):
90 self.getPage("/", headers=[('From', "x" * 500)])
91 self.assertStatus(413)
92
93
94
95
96 lines256 = "x" * 248
97 self.getPage("/",
98 headers=[('Host', '%s:%s' % (self.HOST, self.PORT)),
99 ('From', lines256)])
100
101
102 body = '\r\n'.join([
103 '--x',
104 'Content-Disposition: form-data; name="file"; filename="hello.txt"',
105 'Content-Type: text/plain',
106 '',
107 '%s',
108 '--x--'])
109 partlen = 200 - len(body)
110 b = body % ("x" * partlen)
111 h = [("Content-type", "multipart/form-data; boundary=x"),
112 ("Content-Length", "%s" % len(b))]
113 self.getPage('/upload', h, "POST", b)
114 self.assertBody('Size: %d' % partlen)
115
116 b = body % ("x" * 200)
117 h = [("Content-type", "multipart/form-data; boundary=x"),
118 ("Content-Length", "%s" % len(b))]
119 self.getPage('/upload', h, "POST", b)
120 self.assertStatus(413)
121