CherryPy / cherrypy / test /

from cherrypy.test import test

import cherrypy

script_names = ["", "/path/to/myapp"]

def setup_server():
    # Set up site
        'environment': 'test_suite',
        'tools.proxy.on': True,
        'tools.proxy.base': '',
    # Set up application
    class Root:
        def __init__(self, sn):
            # Calculate a URL outside of any requests.
            self.thisnewpage = cherrypy.url("/this/new/page", script_name=sn)
        def pageurl(self):
            return self.thisnewpage = True
        def index(self):
            raise cherrypy.HTTPRedirect('dummy') = True
        def remoteip(self):
            return cherrypy.request.remote.ip = True
        def xhost(self):
            raise cherrypy.HTTPRedirect('blah') = True
        xhost._cp_config = {'tools.proxy.local': 'X-Host',
                            'tools.trailing_slash.extra': True,
        def base(self):
            return cherrypy.request.base = True
        def newurl(self):
            return ("Browse to <a href='%s'>this page</a>."
                    % cherrypy.url("/this/new/page")) = True
    for sn in script_names:
        cherrypy.tree.mount(Root(sn), sn)

from cherrypy.test import helper

class ProxyTest(helper.CPWebCase):
    def testProxy(self):
                          "%s://" %
                          (self.scheme, self.prefix()))
        # Test X-Forwarded-Host (Apache 1.3.33+ and Apache 2)
        self.getPage("/", headers=[('X-Forwarded-Host', '')])
        self.assertHeader('Location', "")
        self.getPage("/", headers=[('X-Forwarded-Host', '')])
        self.assertHeader('Location', "%s://" % self.scheme)
        # Test X-Forwarded-For (Apache2)
                     headers=[('X-Forwarded-For', '')])
                     headers=[('X-Forwarded-For', ',')])
        # Test X-Host (lighttpd; see
        self.getPage("/xhost", headers=[('X-Host', '')])
        self.assertHeader('Location', "%s://" % self.scheme)
        # Test X-Forwarded-Proto (lighttpd)
        self.getPage("/base", headers=[('X-Forwarded-Proto', 'https')])
        # Test cherrypy.url()
        for sn in script_names:
            # Test the value inside requests
            self.getPage(sn + "/newurl")
            self.assertBody("Browse to <a href='%s://" % self.scheme
                            + sn + "/this/new/page'>this page</a>.")
            self.getPage(sn + "/newurl", headers=[('X-Forwarded-Host',
            self.assertBody("Browse to <a href='"
                            + sn + "/this/new/page'>this page</a>.")
            # Test the value outside requests
            port = ""
            if self.scheme == "http" and self.PORT != 80:
                port = ":%s" % self.PORT
            elif self.scheme == "https" and self.PORT != 443:
                port = ":%s" % self.PORT
            host = self.HOST
            if host in ('', '::'):
                import socket
                host = socket.gethostname()
            expected = ("%s://%s%s%s/this/new/page"
                        % (self.scheme, host, port, sn))
            self.getPage(sn + "/pageurl")
        # Test trailing slash (see
        self.getPage("/xhost/", headers=[('X-Host', '')])
        self.assertHeader('Location', "%s://"
                          % self.scheme)

if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.