Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/trunk/cherrypy/test/test_wsgiapps.py

Revision 2670 (checked in by fumanchu, 4 months ago)

Removed test.py etc.

  • Property svn:eol-style set to native
Line 
1 from cherrypy.test import helper
2
3
4 class WSGIGraftTests(helper.CPWebCase):
5     @staticmethod
6     def setup_server():
7         import os
8         curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
9        
10         import cherrypy
11        
12         def test_app(environ, start_response):
13             status = '200 OK'
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())
19             keys.sort()
20             for k in keys:
21                 output.append('%s: %s\n' % (k,environ[k]))
22             return output
23        
24         def test_empty_string_app(environ, start_response):
25             status = '200 OK'
26             response_headers = [('Content-type', 'text/plain')]
27             start_response(status, response_headers)
28             return ['Hello', '', ' ', '', 'world']
29        
30        
31         class WSGIResponse(object):
32            
33             def __init__(self, appresults):
34                 self.appresults = appresults
35                 self.iter = iter(appresults)
36            
37             def __iter__(self):
38                 return self
39            
40             def next(self):
41                 return self.iter.next()
42            
43             def close(self):
44                 if hasattr(self.appresults, "close"):
45                     self.appresults.close()
46        
47        
48         class ReversingMiddleware(object):
49            
50             def __init__(self, app):
51                 self.app = app
52            
53             def __call__(self, environ, start_response):
54                 results = app(environ, start_response)
55                 class Reverser(WSGIResponse):
56                     def next(this):
57                         line = list(this.iter.next())
58                         line.reverse()
59                         return "".join(line)
60                 return Reverser(results)
61        
62         class Root:
63             def index(self):
64                 return "I'm a regular CherryPy page handler!"
65             index.exposed = True
66        
67        
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         # 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
78     wsgi_output = '''Hello, world!
79 This is a wsgi app running within CherryPy!'''
80
81     def test_01_standard_app(self):
82         self.getPage("/")
83         self.assertBody("I'm a regular CherryPy page handler!")
84    
85     def test_04_pure_wsgi(self):
86         import cherrypy
87         if not cherrypy.server.using_wsgi:
88             return self.skip("skipped (not using WSGI)... ")
89         self.getPage("/hosted/app1")
90         self.assertHeader("Content-Type", "text/plain")
91         self.assertInBody(self.wsgi_output)
92
93     def test_05_wrapped_cp_app(self):
94         import cherrypy
95         if not cherrypy.server.using_wsgi:
96             return self.skip("skipped (not using WSGI)... ")
97         self.getPage("/hosted/app2/")
98         body = list("I'm a regular CherryPy page handler!")
99         body.reverse()
100         body = "".join(body)
101         self.assertInBody(body)
102
103     def test_06_empty_string_app(self):
104         import cherrypy
105         if not cherrypy.server.using_wsgi:
106             return self.skip("skipped (not using WSGI)... ")
107         self.getPage("/hosted/app3")
108         self.assertHeader("Content-Type", "text/plain")
109         self.assertInBody('Hello world')
110
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets