| 1 |
from cherrypy.test 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 |
output = ['Hello, world!\n', |
|---|
| 16 |
'This is a wsgi app running within CherryPy!\n\n'] |
|---|
| 17 |
keys = environ.keys() |
|---|
| 18 |
keys.sort() |
|---|
| 19 |
for k in keys: |
|---|
| 20 |
output.append('%s: %s\n' % (k,environ[k])) |
|---|
| 21 |
return output |
|---|
| 22 |
|
|---|
| 23 |
def test_empty_string_app(environ, start_response): |
|---|
| 24 |
status = '200 OK' |
|---|
| 25 |
response_headers = [('Content-type', 'text/plain')] |
|---|
| 26 |
start_response(status, response_headers) |
|---|
| 27 |
return ['Hello', '', ' ', '', 'world'] |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
class WSGIResponse(object): |
|---|
| 31 |
|
|---|
| 32 |
def __init__(self, appresults): |
|---|
| 33 |
self.appresults = appresults |
|---|
| 34 |
self.iter = iter(appresults) |
|---|
| 35 |
|
|---|
| 36 |
def __iter__(self): |
|---|
| 37 |
return self |
|---|
| 38 |
|
|---|
| 39 |
def next(self): |
|---|
| 40 |
return self.iter.next() |
|---|
| 41 |
|
|---|
| 42 |
def close(self): |
|---|
| 43 |
if hasattr(self.appresults, "close"): |
|---|
| 44 |
self.appresults.close() |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
class ReversingMiddleware(object): |
|---|
| 48 |
|
|---|
| 49 |
def __init__(self, app): |
|---|
| 50 |
self.app = app |
|---|
| 51 |
|
|---|
| 52 |
def __call__(self, environ, start_response): |
|---|
| 53 |
results = app(environ, start_response) |
|---|
| 54 |
class Reverser(WSGIResponse): |
|---|
| 55 |
def next(this): |
|---|
| 56 |
line = list(this.iter.next()) |
|---|
| 57 |
line.reverse() |
|---|
| 58 |
return "".join(line) |
|---|
| 59 |
return Reverser(results) |
|---|
| 60 |
|
|---|
| 61 |
class Root: |
|---|
| 62 |
def index(self): |
|---|
| 63 |
return "I'm a regular CherryPy page handler!" |
|---|
| 64 |
index.exposed = True |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
cherrypy.config.update({'environment': 'test_suite'}) |
|---|
| 68 |
cherrypy.tree.mount(Root()) |
|---|
| 69 |
|
|---|
| 70 |
cherrypy.tree.graft(test_app, '/hosted/app1') |
|---|
| 71 |
cherrypy.tree.graft(test_empty_string_app, '/hosted/app3') |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
app = cherrypy.Application(Root(), script_name=None) |
|---|
| 76 |
cherrypy.tree.graft(ReversingMiddleware(app), '/hosted/app2') |
|---|
| 77 |
|
|---|
| 78 |
from cherrypy.test import helper |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
class WSGIGraftTests(helper.CPWebCase): |
|---|
| 82 |
|
|---|
| 83 |
wsgi_output = '''Hello, world! |
|---|
| 84 |
This is a wsgi app running within CherryPy!''' |
|---|
| 85 |
|
|---|
| 86 |
def test_01_standard_app(self): |
|---|
| 87 |
self.getPage("/") |
|---|
| 88 |
self.assertBody("I'm a regular CherryPy page handler!") |
|---|
| 89 |
|
|---|
| 90 |
def test_04_pure_wsgi(self): |
|---|
| 91 |
import cherrypy |
|---|
| 92 |
if not cherrypy.server.using_wsgi: |
|---|
| 93 |
print "skipped (not using WSGI)...", |
|---|
| 94 |
return |
|---|
| 95 |
self.getPage("/hosted/app1") |
|---|
| 96 |
self.assertHeader("Content-Type", "text/plain") |
|---|
| 97 |
self.assertInBody(self.wsgi_output) |
|---|
| 98 |
|
|---|
| 99 |
def test_05_wrapped_cp_app(self): |
|---|
| 100 |
import cherrypy |
|---|
| 101 |
if not cherrypy.server.using_wsgi: |
|---|
| 102 |
print "skipped (not using WSGI)...", |
|---|
| 103 |
return |
|---|
| 104 |
self.getPage("/hosted/app2/") |
|---|
| 105 |
body = list("I'm a regular CherryPy page handler!") |
|---|
| 106 |
body.reverse() |
|---|
| 107 |
body = "".join(body) |
|---|
| 108 |
self.assertInBody(body) |
|---|
| 109 |
|
|---|
| 110 |
def test_06_empty_string_app(self): |
|---|
| 111 |
import cherrypy |
|---|
| 112 |
if not cherrypy.server.using_wsgi: |
|---|
| 113 |
print "skipped (not using WSGI)...", |
|---|
| 114 |
return |
|---|
| 115 |
self.getPage("/hosted/app3") |
|---|
| 116 |
self.assertHeader("Content-Type", "text/plain") |
|---|
| 117 |
self.assertInBody('Hello world') |
|---|
| 118 |
|
|---|
| 119 |
if __name__ == '__main__': |
|---|
| 120 |
setup_server() |
|---|
| 121 |
helper.testmain() |
|---|
| 122 |
|
|---|