CherryPy / cherrypy / test /

import cherrypy
from cherrypy._cpcompat import ntob
from cherrypy.test import helper

class WSGI_Namespace_Test(helper.CPWebCase):

    def setup_server():

        class WSGIResponse(object):

            def __init__(self, appresults):
                self.appresults = appresults
                self.iter = iter(appresults)

            def __iter__(self):
                return self

            def next(self):
            def __next__(self):
                return next(self.iter)

            def close(self):
                if hasattr(self.appresults, "close"):

        class ChangeCase(object):

            def __init__(self, app, to=None):
       = app
       = to

            def __call__(self, environ, start_response):
                res =, start_response)
                class CaseResults(WSGIResponse):
                    def next(this):
                        return getattr(,
                    def __next__(this):
                        return getattr(next(this.iter),
                return CaseResults(res)

        class Replacer(object):

            def __init__(self, app, map={}):
       = app
       = map

            def __call__(self, environ, start_response):
                res =, start_response)
                class ReplaceResults(WSGIResponse):
                    def next(this):
                        line =
                        for k, v in
                            line = line.replace(k, v)
                        return line
                    def __next__(this):
                        line = next(this.iter)
                        for k, v in
                            line = line.replace(k, v)
                        return line
                return ReplaceResults(res)

        class Root(object):

            def index(self):
                return "HellO WoRlD!"
   = True

        root_conf = {'wsgi.pipeline': [('replace', Replacer)],
                     '': {ntob('L'): ntob('X'),
                                          ntob('l'): ntob('r')},

        app = cherrypy.Application(Root())
        app.wsgiapp.pipeline.append(('changecase', ChangeCase))
        app.wsgiapp.config['changecase'] = {'to': 'upper'}
        cherrypy.tree.mount(app, config={'/': root_conf})
    setup_server = staticmethod(setup_server)

    def test_pipeline(self):
        if not cherrypy.server.httpserver:
            return self.skip()

        # If body is "HEXXO WORXD!", the middleware was applied out of order.
        self.assertBody("HERRO WORRD!")