Fixed python_path problem.
[smonitor.git] / lib / cherrypy / test / test_wsgi_ns.py
blobd57013c3a422214c3904f24a839a29eb6d78ce1b
1 import cherrypy
2 from cherrypy.test import helper
5 class WSGI_Namespace_Test(helper.CPWebCase):
7 def setup_server():
9 class WSGIResponse(object):
11 def __init__(self, appresults):
12 self.appresults = appresults
13 self.iter = iter(appresults)
15 def __iter__(self):
16 return self
18 def next(self):
19 return self.iter.next()
21 def close(self):
22 if hasattr(self.appresults, "close"):
23 self.appresults.close()
26 class ChangeCase(object):
28 def __init__(self, app, to=None):
29 self.app = app
30 self.to = to
32 def __call__(self, environ, start_response):
33 res = self.app(environ, start_response)
34 class CaseResults(WSGIResponse):
35 def next(this):
36 return getattr(this.iter.next(), self.to)()
37 return CaseResults(res)
39 class Replacer(object):
41 def __init__(self, app, map={}):
42 self.app = app
43 self.map = map
45 def __call__(self, environ, start_response):
46 res = self.app(environ, start_response)
47 class ReplaceResults(WSGIResponse):
48 def next(this):
49 line = this.iter.next()
50 for k, v in self.map.iteritems():
51 line = line.replace(k, v)
52 return line
53 return ReplaceResults(res)
55 class Root(object):
57 def index(self):
58 return "HellO WoRlD!"
59 index.exposed = True
62 root_conf = {'wsgi.pipeline': [('replace', Replacer)],
63 'wsgi.replace.map': {'L': 'X', 'l': 'r'},
66 app = cherrypy.Application(Root())
67 app.wsgiapp.pipeline.append(('changecase', ChangeCase))
68 app.wsgiapp.config['changecase'] = {'to': 'upper'}
69 cherrypy.tree.mount(app, config={'/': root_conf})
70 setup_server = staticmethod(setup_server)
73 def test_pipeline(self):
74 if not cherrypy.server.httpserver:
75 return self.skip()
77 self.getPage("/")
78 # If body is "HEXXO WORXD!", the middleware was applied out of order.
79 self.assertBody("HERRO WORRD!")