1 """Wrapper for mod_python, for use as a CherryPy HTTP server when testing.
3 To autostart modpython, the "apache" executable or script must be
4 on your system path, or you must override the global APACHE_PATH.
5 On some platforms, "apache" may be called "apachectl" or "apache2ctl"--
6 create a symlink to them if needed.
8 If you wish to test the WSGI interface instead of our _cpmodpy interface,
9 you also need the 'modpython_gateway' module at:
10 http://projects.amor.org/misc/wiki/ModPythonGateway
16 1. Apache processes Range headers automatically; CherryPy's truncated
17 output is then truncated again by Apache. See test_core.testRanges.
18 This was worked around in http://www.cherrypy.org/changeset/1319.
19 2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
20 See test_core.testHTTPMethods.
21 3. Max request header and body settings do not work with Apache.
22 4. Apache replaces status "reason phrases" automatically. For example,
23 CherryPy may set "304 Not modified" but Apache will write out
24 "304 Not Modified" (capital "M").
25 5. Apache does not allow custom error codes as per the spec.
26 6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
27 Request-URI too early.
28 7. mod_python will not read request bodies which use the "chunked"
29 transfer-coding (it passes REQUEST_CHUNKED_ERROR to ap_setup_client_block
30 instead of REQUEST_CHUNKED_DECHUNK, see Apache2's http_protocol.c and
31 mod_python's requestobject.c).
32 8. Apache will output a "Content-Length: 0" response header even if there's
33 no response entity body. This isn't really a bug; it just differs from
38 curdir
= os
.path
.join(os
.getcwd(), os
.path
.dirname(__file__
))
42 from cherrypy
.test
import helper
45 def read_process(cmd
, args
=""):
46 pipein
, pipeout
= os
.popen4("%s %s" % (cmd
, args
))
48 firstline
= pipeout
.readline()
49 if (re
.search(r
"(not recognized|No such file|not found)", firstline
,
51 raise IOError('%s must be on your system path.' % cmd
)
52 output
= firstline
+ pipeout
.read()
59 CONF_PATH
= "test_mp.conf"
61 conf_modpython_gateway
= """
62 # Apache2 server conf file for testing CherryPy with modpython_gateway.
67 LoadModule python_module modules/mod_python.so
69 SetHandler python-program
70 PythonFixupHandler cherrypy.test.modpy::wsgisetup
71 PythonOption testmod %(modulename)s
72 PythonHandler modpython_gateway::handler
73 PythonOption wsgi.application cherrypy::tree
74 PythonOption socket_host %(host)s
79 # Apache2 server conf file for testing CherryPy with _cpmodpy.
84 LoadModule python_module modules/mod_python.so
86 SetHandler python-program
87 PythonFixupHandler cherrypy.test.modpy::cpmodpysetup
88 PythonHandler cherrypy._cpmodpy::handler
89 PythonOption cherrypy.setup cherrypy.test.%(modulename)s::setup_server
90 PythonOption socket_host %(host)s
94 class ModPythonSupervisor(helper
.Supervisor
):
101 return "ModPython Server on %s:%s" % (self
.host
, self
.port
)
103 def start(self
, modulename
):
105 if not os
.path
.isabs(mpconf
):
106 mpconf
= os
.path
.join(curdir
, mpconf
)
108 f
= open(mpconf
, 'wb')
110 f
.write(self
.template
%
111 {'port': self
.port
, 'modulename': modulename
,
116 result
= read_process(APACHE_PATH
, "-k start -f %s" % mpconf
)
121 """Gracefully shutdown a server that is serving forever."""
122 read_process(APACHE_PATH
, "-k stop")
130 options
= req
.get_options()
133 cherrypy
.config
.update({
134 "log.error_file": os
.path
.join(curdir
, "test.log"),
135 "environment": "test_suite",
136 "server.socket_host": options
['socket_host'],
139 modname
= options
['testmod']
140 mod
= __import__(modname
, globals(), locals(), [''])
143 cherrypy
.server
.unsubscribe()
144 cherrypy
.engine
.start()
145 from mod_python
import apache
149 def cpmodpysetup(req
):
153 options
= req
.get_options()
156 cherrypy
.config
.update({
157 "log.error_file": os
.path
.join(curdir
, "test.log"),
158 "environment": "test_suite",
159 "server.socket_host": options
['socket_host'],
161 from mod_python
import apache