Fixed python_path problem.
[smonitor.git] / lib / cherrypy / test / test_config_server.py
blob0b9718da2b2d6b179357de27b66fd82d20b2b598
1 """Tests for the CherryPy configuration system."""
3 import os, sys
4 localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
5 import socket
6 import time
8 import cherrypy
11 # Client-side code #
13 from cherrypy.test import helper
15 class ServerConfigTests(helper.CPWebCase):
17 def setup_server():
19 class Root:
20 def index(self):
21 return cherrypy.request.wsgi_environ['SERVER_PORT']
22 index.exposed = True
24 def upload(self, file):
25 return "Size: %s" % len(file.file.read())
26 upload.exposed = True
28 def tinyupload(self):
29 return cherrypy.request.body.read()
30 tinyupload.exposed = True
31 tinyupload._cp_config = {'request.body.maxbytes': 100}
33 cherrypy.tree.mount(Root())
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,
42 # Test explicit server.instance
43 'server.2.instance': 'cherrypy._cpwsgi_server.CPWSGIServer',
44 'server.2.socket_port': 9877,
46 # Test non-numeric <servername>
47 # Also test default server.instance = builtin server
48 'server.yetanother.socket_port': 9878,
50 setup_server = staticmethod(setup_server)
52 PORT = 9876
54 def testBasicConfig(self):
55 self.getPage("/")
56 self.assertBody(str(self.PORT))
58 def testAdditionalServers(self):
59 if self.scheme == 'https':
60 return self.skip("not available under ssl")
61 self.PORT = 9877
62 self.getPage("/")
63 self.assertBody(str(self.PORT))
64 self.PORT = 9878
65 self.getPage("/")
66 self.assertBody(str(self.PORT))
68 def testMaxRequestSizePerHandler(self):
69 if getattr(cherrypy.server, "using_apache", False):
70 return self.skip("skipped due to known Apache differences... ")
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)
79 self.getPage('/tinyupload', method="POST",
80 headers=[('Content-Type', 'text/plain'),
81 ('Content-Length', '101')],
82 body="x" * 101)
83 self.assertStatus(413)
85 def testMaxRequestSize(self):
86 if getattr(cherrypy.server, "using_apache", False):
87 return self.skip("skipped due to known Apache differences... ")
89 for size in (500, 5000, 50000):
90 self.getPage("/", headers=[('From', "x" * 500)])
91 self.assertStatus(413)
93 # Test for http://www.cherrypy.org/ticket/421
94 # (Incorrect border condition in readline of SizeCheckWrapper).
95 # This hangs in rev 891 and earlier.
96 lines256 = "x" * 248
97 self.getPage("/",
98 headers=[('Host', '%s:%s' % (self.HOST, self.PORT)),
99 ('From', lines256)])
101 # Test upload
102 body = '\r\n'.join([
103 '--x',
104 'Content-Disposition: form-data; name="file"; filename="hello.txt"',
105 'Content-Type: text/plain',
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)
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)