| 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 |
from cherrypy.filters.wsgiappfilter import WSGIAppFilter |
|---|
| 11 |
from cherrypy.lib.cptools import WSGIApp |
|---|
| 12 |
|
|---|
| 13 |
def test_app(environ, start_response): |
|---|
| 14 |
status = '200 OK' |
|---|
| 15 |
response_headers = [('Content-type', 'text/plain')] |
|---|
| 16 |
start_response(status, response_headers) |
|---|
| 17 |
yield 'Hello, world!\n' |
|---|
| 18 |
yield 'This is a wsgi app running within CherryPy!\n\n' |
|---|
| 19 |
keys = environ.keys() |
|---|
| 20 |
keys.sort() |
|---|
| 21 |
for k in keys: |
|---|
| 22 |
yield '%s: %s\n' % (k,environ[k]) |
|---|
| 23 |
|
|---|
| 24 |
class Root: |
|---|
| 25 |
def index(self): |
|---|
| 26 |
return "I'm a regular CherryPy page handler!" |
|---|
| 27 |
index.exposed = True |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
class HostedWSGI(object): |
|---|
| 31 |
_cp_filters = [WSGIAppFilter(test_app),] |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
conf = {'server.log_to_screen': False, |
|---|
| 35 |
'server.environment': 'production', |
|---|
| 36 |
'server.show_tracebacks': True, |
|---|
| 37 |
} |
|---|
| 38 |
cherrypy.tree.mount(Root(), '/', conf) |
|---|
| 39 |
conf0 = {'/static': {'static_filter.on': True, |
|---|
| 40 |
'static_filter.root': curdir, |
|---|
| 41 |
'static_filter.dir': 'static', |
|---|
| 42 |
}} |
|---|
| 43 |
cherrypy.tree.mount(HostedWSGI(), '/hosted/app0', conf0) |
|---|
| 44 |
cherrypy.tree.mount(WSGIApp(test_app), '/hosted/app1') |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
import helper |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
class WSGIAppFilterTest(helper.CPWebCase): |
|---|
| 51 |
|
|---|
| 52 |
wsgi_output = '''Hello, world! |
|---|
| 53 |
This is a wsgi app running within CherryPy!''' |
|---|
| 54 |
|
|---|
| 55 |
def test_01_standard_app(self): |
|---|
| 56 |
self.getPage("/") |
|---|
| 57 |
self.assertBody("I'm a regular CherryPy page handler!") |
|---|
| 58 |
|
|---|
| 59 |
def test_02_cp_filters(self): |
|---|
| 60 |
self.getPage("/hosted/app0") |
|---|
| 61 |
self.assertHeader("Content-Type", "text/plain") |
|---|
| 62 |
self.assertInBody(self.wsgi_output) |
|---|
| 63 |
|
|---|
| 64 |
def test_03_wsgiapp_class(self): |
|---|
| 65 |
self.getPage("/hosted/app1") |
|---|
| 66 |
self.assertHeader("Content-Type", "text/plain") |
|---|
| 67 |
self.assertInBody(self.wsgi_output) |
|---|
| 68 |
|
|---|
| 69 |
def test_04_static_subdir(self): |
|---|
| 70 |
self.getPage("/hosted/app0/static/index.html") |
|---|
| 71 |
self.assertStatus('200 OK') |
|---|
| 72 |
self.assertHeader('Content-Type', 'text/html') |
|---|
| 73 |
self.assertBody('Hello, world\r\n') |
|---|
| 74 |
|
|---|
| 75 |
if __name__ == '__main__': |
|---|
| 76 |
setup_server() |
|---|
| 77 |
helper.testmain() |
|---|
| 78 |
|
|---|