| 1 |
import test |
|---|
| 2 |
test.prefer_parent_path() |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
def setup_server(): |
|---|
| 6 |
import os |
|---|
| 7 |
curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) |
|---|
| 8 |
|
|---|
| 9 |
import cherrypy |
|---|
| 10 |
|
|---|
| 11 |
def test_app(environ, start_response): |
|---|
| 12 |
status = '200 OK' |
|---|
| 13 |
response_headers = [('Content-type', 'text/plain')] |
|---|
| 14 |
start_response(status, response_headers) |
|---|
| 15 |
yield 'Hello, world!\n' |
|---|
| 16 |
yield 'This is a wsgi app running within CherryPy!\n\n' |
|---|
| 17 |
keys = environ.keys() |
|---|
| 18 |
keys.sort() |
|---|
| 19 |
for k in keys: |
|---|
| 20 |
yield '%s: %s\n' % (k,environ[k]) |
|---|
| 21 |
|
|---|
| 22 |
def reversing_middleware(app): |
|---|
| 23 |
def _app(environ, start_response): |
|---|
| 24 |
results = app(environ, start_response) |
|---|
| 25 |
if not isinstance(results, basestring): |
|---|
| 26 |
results = "".join(results) |
|---|
| 27 |
results = list(results) |
|---|
| 28 |
results.reverse() |
|---|
| 29 |
return "".join(results) |
|---|
| 30 |
return _app |
|---|
| 31 |
|
|---|
| 32 |
class Root: |
|---|
| 33 |
def index(self): |
|---|
| 34 |
return "I'm a regular CherryPy page handler!" |
|---|
| 35 |
index.exposed = True |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
class HostedWSGI(object): |
|---|
| 39 |
_cp_config = {'tools.wsgiapp.on': True, |
|---|
| 40 |
'tools.wsgiapp.app': test_app, |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
cherrypy.config.update({'log_to_screen': False, |
|---|
| 44 |
'environment': 'production', |
|---|
| 45 |
'show_tracebacks': True, |
|---|
| 46 |
}) |
|---|
| 47 |
cherrypy.tree.mount(Root()) |
|---|
| 48 |
|
|---|
| 49 |
conf0 = {'/static': {'tools.staticdir.on': True, |
|---|
| 50 |
'tools.staticdir.root': curdir, |
|---|
| 51 |
'tools.staticdir.dir': 'static', |
|---|
| 52 |
}} |
|---|
| 53 |
cherrypy.tree.mount(HostedWSGI(), '/hosted/app0', conf0) |
|---|
| 54 |
cherrypy.tree.mount(test_app, '/hosted/app1', wrap=False) |
|---|
| 55 |
|
|---|
| 56 |
app = reversing_middleware(cherrypy.Application(Root())) |
|---|
| 57 |
cherrypy.tree.mount(app, '/hosted/app2', wrap=False) |
|---|
| 58 |
|
|---|
| 59 |
import helper |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
class WSGIAppTest(helper.CPWebCase): |
|---|
| 63 |
|
|---|
| 64 |
wsgi_output = '''Hello, world! |
|---|
| 65 |
This is a wsgi app running within CherryPy!''' |
|---|
| 66 |
|
|---|
| 67 |
def test_01_standard_app(self): |
|---|
| 68 |
self.getPage("/") |
|---|
| 69 |
self.assertBody("I'm a regular CherryPy page handler!") |
|---|
| 70 |
|
|---|
| 71 |
def test_02_wrapped_wsgi(self): |
|---|
| 72 |
self.getPage("/hosted/app0") |
|---|
| 73 |
self.assertHeader("Content-Type", "text/plain") |
|---|
| 74 |
self.assertInBody(self.wsgi_output) |
|---|
| 75 |
|
|---|
| 76 |
def test_03_static_subdir(self): |
|---|
| 77 |
self.getPage("/hosted/app0/static/index.html") |
|---|
| 78 |
self.assertStatus('200 OK') |
|---|
| 79 |
self.assertHeader('Content-Type', 'text/html') |
|---|
| 80 |
self.assertBody('Hello, world\r\n') |
|---|
| 81 |
|
|---|
| 82 |
def test_04_pure_wsgi(self): |
|---|
| 83 |
self.getPage("/hosted/app1") |
|---|
| 84 |
self.assertHeader("Content-Type", "text/plain") |
|---|
| 85 |
self.assertInBody(self.wsgi_output) |
|---|
| 86 |
|
|---|
| 87 |
def test_05_wrapped_cp_app(self): |
|---|
| 88 |
self.getPage("/hosted/app2/") |
|---|
| 89 |
body = list("I'm a regular CherryPy page handler!") |
|---|
| 90 |
body.reverse() |
|---|
| 91 |
body = "".join(body) |
|---|
| 92 |
self.assertInBody(body) |
|---|
| 93 |
|
|---|
| 94 |
if __name__ == '__main__': |
|---|
| 95 |
setup_server() |
|---|
| 96 |
helper.testmain() |
|---|
| 97 |
|
|---|