1 from cherrypy
.test
import helper
4 class WSGIGraftTests(helper
.CPWebCase
):
8 curdir
= os
.path
.join(os
.getcwd(), os
.path
.dirname(__file__
))
12 def test_app(environ
, start_response
):
14 response_headers
= [('Content-type', 'text/plain')]
15 start_response(status
, response_headers
)
16 output
= ['Hello, world!\n',
17 'This is a wsgi app running within CherryPy!\n\n']
18 keys
= list(environ
.keys())
21 output
.append('%s: %s\n' % (k
,environ
[k
]))
24 def test_empty_string_app(environ
, start_response
):
26 response_headers
= [('Content-type', 'text/plain')]
27 start_response(status
, response_headers
)
28 return ['Hello', '', ' ', '', 'world']
31 class WSGIResponse(object):
33 def __init__(self
, appresults
):
34 self
.appresults
= appresults
35 self
.iter = iter(appresults
)
41 return self
.iter.next()
44 if hasattr(self
.appresults
, "close"):
45 self
.appresults
.close()
48 class ReversingMiddleware(object):
50 def __init__(self
, app
):
53 def __call__(self
, environ
, start_response
):
54 results
= app(environ
, start_response
)
55 class Reverser(WSGIResponse
):
57 line
= list(this
.iter.next())
60 return Reverser(results
)
64 return "I'm a regular CherryPy page handler!"
68 cherrypy
.tree
.mount(Root())
70 cherrypy
.tree
.graft(test_app
, '/hosted/app1')
71 cherrypy
.tree
.graft(test_empty_string_app
, '/hosted/app3')
73 # Set script_name explicitly to None to signal CP that it should
74 # be pulled from the WSGI environ each time.
75 app
= cherrypy
.Application(Root(), script_name
=None)
76 cherrypy
.tree
.graft(ReversingMiddleware(app
), '/hosted/app2')
77 setup_server
= staticmethod(setup_server
)
79 wsgi_output
= '''Hello, world!
80 This is a wsgi app running within CherryPy!'''
82 def test_01_standard_app(self
):
84 self
.assertBody("I'm a regular CherryPy page handler!")
86 def test_04_pure_wsgi(self
):
88 if not cherrypy
.server
.using_wsgi
:
89 return self
.skip("skipped (not using WSGI)... ")
90 self
.getPage("/hosted/app1")
91 self
.assertHeader("Content-Type", "text/plain")
92 self
.assertInBody(self
.wsgi_output
)
94 def test_05_wrapped_cp_app(self
):
96 if not cherrypy
.server
.using_wsgi
:
97 return self
.skip("skipped (not using WSGI)... ")
98 self
.getPage("/hosted/app2/")
99 body
= list("I'm a regular CherryPy page handler!")
102 self
.assertInBody(body
)
104 def test_06_empty_string_app(self
):
106 if not cherrypy
.server
.using_wsgi
:
107 return self
.skip("skipped (not using WSGI)... ")
108 self
.getPage("/hosted/app3")
109 self
.assertHeader("Content-Type", "text/plain")
110 self
.assertInBody('Hello world')