Package cherrypy :: Package test :: Module test_wsgiapps
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_wsgiapps

  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 def reversing_middleware(app): 30 def _app(environ, start_response): 31 results = app(environ, start_response) 32 if not isinstance(results, basestring): 33 results = "".join(results) 34 results = list(results) 35 results.reverse() 36 return ["".join(results)] 37 return _app 38 39 class Root: 40 def index(self): 41 return "I'm a regular CherryPy page handler!" 42 index.exposed = True 43 44 45 class HostedWSGI(object): 46 _cp_config = {'tools.wsgiapp.on': True, 47 'tools.wsgiapp.app': test_app, 48 } 49 50 cherrypy.config.update({'environment': 'test_suite'}) 51 cherrypy.tree.mount(Root()) 52 53 conf0 = {'/static': {'tools.staticdir.on': True, 54 'tools.staticdir.root': curdir, 55 'tools.staticdir.dir': 'static', 56 }} 57 cherrypy.tree.mount(HostedWSGI(), '/hosted/app0', conf0) 58 cherrypy.tree.graft(test_app, '/hosted/app1') 59 cherrypy.tree.graft(test_empty_string_app, '/hosted/app3') 60 61 # Set script_name explicitly to None to signal CP that it should 62 # be pulled from the WSGI environ each time. 63 app = cherrypy.Application(Root(), script_name=None) 64 cherrypy.tree.graft(reversing_middleware(app), '/hosted/app2') 65 66 from cherrypy.test import helper 67 68
69 -class WSGIAppTest(helper.CPWebCase):
70 71 wsgi_output = '''Hello, world! 72 This is a wsgi app running within CherryPy!''' 73
74 - def test_01_standard_app(self):
75 self.getPage("/") 76 self.assertBody("I'm a regular CherryPy page handler!")
77
78 - def test_02_wrapped_wsgi(self):
79 self.getPage("/hosted/app0") 80 self.assertHeader("Content-Type", "text/plain") 81 self.assertInBody(self.wsgi_output)
82
83 - def test_03_static_subdir(self):
84 self.getPage("/hosted/app0/static/index.html") 85 self.assertStatus('200 OK') 86 self.assertHeader('Content-Type', 'text/html') 87 self.assertBody('Hello, world\r\n')
88
89 - def test_04_pure_wsgi(self):
90 self.getPage("/hosted/app1") 91 self.assertHeader("Content-Type", "text/plain") 92 self.assertInBody(self.wsgi_output)
93
94 - def test_05_wrapped_cp_app(self):
95 self.getPage("/hosted/app2/") 96 body = list("I'm a regular CherryPy page handler!") 97 body.reverse() 98 body = "".join(body) 99 self.assertInBody(body)
100
101 - def test_06_empty_string_app(self):
102 self.getPage("/hosted/app3") 103 self.assertHeader("Content-Type", "text/plain") 104 self.assertInBody('Hello world')
105 106 if __name__ == '__main__': 107 setup_server() 108 helper.testmain() 109