summaryrefslogtreecommitdiff
path: root/cherrypy/test/test_wsgi_ns.py
blob: 2a5fedd499c5a10a264e5a7ac88b75f4d1393ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import cherrypy
from cherrypy._cpcompat import ntob
from cherrypy.test import helper


class WSGI_Namespace_Test(helper.CPWebCase):

    @staticmethod
    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):
                return self.iter.next()

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

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

        class ChangeCase(object):

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

            def __call__(self, environ, start_response):
                res = self.app(environ, start_response)

                class CaseResults(WSGIResponse):

                    def next(this):
                        return getattr(this.iter.next(), self.to)()

                    def __next__(this):
                        return getattr(next(this.iter), self.to)()
                return CaseResults(res)

        class Replacer(object):

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

            def __call__(self, environ, start_response):
                res = self.app(environ, start_response)

                class ReplaceResults(WSGIResponse):

                    def next(this):
                        line = this.iter.next()
                        for k, v in self.map.iteritems():
                            line = line.replace(k, v)
                        return line

                    def __next__(this):
                        line = next(this.iter)
                        for k, v in self.map.items():
                            line = line.replace(k, v)
                        return line
                return ReplaceResults(res)

        class Root(object):

            @cherrypy.expose
            def index(self):
                return 'HellO WoRlD!'

        root_conf = {'wsgi.pipeline': [('replace', Replacer)],
                     'wsgi.replace.map': {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})

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

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