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

root/trunk/cherrypy/test/test_wsgiapps.py

Revision 2637 (checked in by jtate, 2 weeks ago)

Convert the tests to use nose instead of our own runner. This strips out much coverage and profiling (handled by nose) and lets you focus on writing tests.

The biggest changes that have to be done in the tests classes is you have to put the "setup_server" method on the class(es) that need them when running. If you need it for multiple classes, you can use staticmethod() to attach it to multiple classes without using inheritance.

  • Property svn:eol-style set to native
Line 
1 from cherrypy.test import test
2
3
4
5
6 from cherrypy.test import helper
7
8
9 class WSGIGraftTests(helper.CPWebCase):
10     @staticmethod
11     def setup_server():
12         import os
13         curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
14        
15         import cherrypy
16        
17         def test_app(environ, start_response):
18             status = '200 OK'
19             response_headers = [('Content-type', 'text/plain')]
20             start_response(status, response_headers)
21             output = ['Hello, world!\n',
22                       'This is a wsgi app running within CherryPy!\n\n']
23             keys = list(environ.keys())
24             keys.sort()
25             for k in keys:
26                 output.append('%s: %s\n' % (k,environ[k]))
27             return output
28        
29         def test_empty_string_app(environ, start_response):
30             status = '200 OK'
31             response_headers = [('Content-type', 'text/plain')]
32             start_response(status, response_headers)
33             return ['Hello', '', ' ', '', 'world']
34        
35        
36         class WSGIResponse(object):
37            
38             def __init__(self, appresults):
39                 self.appresults = appresults
40                 self.iter = iter(appresults)
41            
42             def __iter__(self):
43                 return self
44            
45             def next(self):
46                 return self.iter.next()
47            
48             def close(self):
49                 if hasattr(self.appresults, "close"):
50                     self.appresults.close()
51        
52        
53         class ReversingMiddleware(object):
54            
55             def __init__(self, app):
56                 self.app = app
57            
58             def __call__(self, environ, start_response):
59                 results = app(environ, start_response)
60                 class Reverser(WSGIResponse):
61                     def next(this):
62                         line = list(this.iter.next())
63                         line.reverse()
64                         return "".join(line)
65                 return Reverser(results)
66        
67         class Root:
68             def index(self):
69                 return "I'm a regular CherryPy page handler!"
70             index.exposed = True
71        
72        
73         cherrypy.tree.mount(Root())
74        
75         cherrypy.tree.graft(test_app, '/hosted/app1')
76         cherrypy.tree.graft(test_empty_string_app, '/hosted/app3')
77        
78         # Set script_name explicitly to None to signal CP that it should
79         # be pulled from the WSGI environ each time.
80         app = cherrypy.Application(Root(), script_name=None)
81         cherrypy.tree.graft(ReversingMiddleware(app), '/hosted/app2')
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             return self.skip("skipped (not using WSGI)... ")
94         self.getPage("/hosted/app1")
95         self.assertHeader("Content-Type", "text/plain")
96         self.assertInBody(self.wsgi_output)
97
98     def test_05_wrapped_cp_app(self):
99         import cherrypy
100         if not cherrypy.server.using_wsgi:
101             return self.skip("skipped (not using WSGI)... ")
102         self.getPage("/hosted/app2/")
103         body = list("I'm a regular CherryPy page handler!")
104         body.reverse()
105         body = "".join(body)
106         self.assertInBody(body)
107
108     def test_06_empty_string_app(self):
109         import cherrypy
110         if not cherrypy.server.using_wsgi:
111             return self.skip("skipped (not using WSGI)... ")
112         self.getPage("/hosted/app3")
113         self.assertHeader("Content-Type", "text/plain")
114         self.assertInBody('Hello world')
115
116 if __name__ == '__main__':
117     helper.testmain()
118
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets