2 from cherrypy
.test
import helper
5 class WSGI_Namespace_Test(helper
.CPWebCase
):
9 class WSGIResponse(object):
11 def __init__(self
, appresults
):
12 self
.appresults
= appresults
13 self
.iter = iter(appresults
)
19 return self
.iter.next()
22 if hasattr(self
.appresults
, "close"):
23 self
.appresults
.close()
26 class ChangeCase(object):
28 def __init__(self
, app
, to
=None):
32 def __call__(self
, environ
, start_response
):
33 res
= self
.app(environ
, start_response
)
34 class CaseResults(WSGIResponse
):
36 return getattr(this
.iter.next(), self
.to
)()
37 return CaseResults(res
)
39 class Replacer(object):
41 def __init__(self
, app
, map={}):
45 def __call__(self
, environ
, start_response
):
46 res
= self
.app(environ
, start_response
)
47 class ReplaceResults(WSGIResponse
):
49 line
= this
.iter.next()
50 for k
, v
in self
.map.iteritems():
51 line
= line
.replace(k
, v
)
53 return ReplaceResults(res
)
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
:
78 # If body is "HEXXO WORXD!", the middleware was applied out of order.
79 self
.assertBody("HERRO WORRD!")