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

Source Code for Module cherrypy.test.test_wsgi_ns

 1  import cherrypy 
 2  from cherrypy._cpcompat import ntob 
 3  from cherrypy.test import helper 
 4   
 5   
6 -class WSGI_Namespace_Test(helper.CPWebCase):
7
8 - def setup_server():
9 10 class WSGIResponse(object): 11 12 def __init__(self, appresults): 13 self.appresults = appresults 14 self.iter = iter(appresults)
15 16 def __iter__(self): 17 return self
18 19 def next(self): 20 return self.iter.next() 21 def __next__(self): 22 return next(self.iter) 23 24 def close(self): 25 if hasattr(self.appresults, "close"): 26 self.appresults.close() 27 28 29 class ChangeCase(object): 30 31 def __init__(self, app, to=None): 32 self.app = app 33 self.to = to 34 35 def __call__(self, environ, start_response): 36 res = self.app(environ, start_response) 37 class CaseResults(WSGIResponse): 38 def next(this): 39 return getattr(this.iter.next(), self.to)() 40 def __next__(this): 41 return getattr(next(this.iter), self.to)() 42 return CaseResults(res) 43 44 class Replacer(object): 45 46 def __init__(self, app, map={}): 47 self.app = app 48 self.map = map 49 50 def __call__(self, environ, start_response): 51 res = self.app(environ, start_response) 52 class ReplaceResults(WSGIResponse): 53 def next(this): 54 line = this.iter.next() 55 for k, v in self.map.iteritems(): 56 line = line.replace(k, v) 57 return line 58 def __next__(this): 59 line = next(this.iter) 60 for k, v in self.map.items(): 61 line = line.replace(k, v) 62 return line 63 return ReplaceResults(res) 64 65 class Root(object): 66 67 def index(self): 68 return "HellO WoRlD!" 69 index.exposed = True 70 71 72 root_conf = {'wsgi.pipeline': [('replace', Replacer)], 73 'wsgi.replace.map': {ntob('L'): ntob('X'), 74 ntob('l'): ntob('r')}, 75 } 76 77 app = cherrypy.Application(Root()) 78 app.wsgiapp.pipeline.append(('changecase', ChangeCase)) 79 app.wsgiapp.config['changecase'] = {'to': 'upper'} 80 cherrypy.tree.mount(app, config={'/': root_conf}) 81 setup_server = staticmethod(setup_server) 82 83
84 - def test_pipeline(self):
85 if not cherrypy.server.httpserver: 86 return self.skip() 87 88 self.getPage("/") 89 # If body is "HEXXO WORXD!", the middleware was applied out of order. 90 self.assertBody("HERRO WORRD!")
91