summaryrefslogtreecommitdiff
path: root/cherrypy/test/test_virtualhost.py
blob: de88f9272aa6b519392c6bc2d4e5243bac543c36 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os

import cherrypy
from cherrypy.test import helper

curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))


class VirtualHostTest(helper.CPWebCase):

    @staticmethod
    def setup_server():
        class Root:

            @cherrypy.expose
            def index(self):
                return 'Hello, world'

            @cherrypy.expose
            def dom4(self):
                return 'Under construction'

            @cherrypy.expose
            def method(self, value):
                return 'You sent %s' % value

        class VHost:

            def __init__(self, sitename):
                self.sitename = sitename

            @cherrypy.expose
            def index(self):
                return 'Welcome to %s' % self.sitename

            @cherrypy.expose
            def vmethod(self, value):
                return 'You sent %s' % value

            @cherrypy.expose
            def url(self):
                return cherrypy.url('nextpage')

            # Test static as a handler (section must NOT include vhost prefix)
            static = cherrypy.tools.staticdir.handler(
                section='/static', dir=curdir)

        root = Root()
        root.mydom2 = VHost('Domain 2')
        root.mydom3 = VHost('Domain 3')
        hostmap = {'www.mydom2.com': '/mydom2',
                   'www.mydom3.com': '/mydom3',
                   'www.mydom4.com': '/dom4',
                   }
        cherrypy.tree.mount(root, config={
            '/': {
                'request.dispatch': cherrypy.dispatch.VirtualHost(**hostmap)
            },
            # Test static in config (section must include vhost prefix)
            '/mydom2/static2': {
                'tools.staticdir.on': True,
                'tools.staticdir.root': curdir,
                'tools.staticdir.dir': 'static',
                'tools.staticdir.index': 'index.html',
            },
        })

    def testVirtualHost(self):
        self.getPage('/', [('Host', 'www.mydom1.com')])
        self.assertBody('Hello, world')
        self.getPage('/mydom2/', [('Host', 'www.mydom1.com')])
        self.assertBody('Welcome to Domain 2')

        self.getPage('/', [('Host', 'www.mydom2.com')])
        self.assertBody('Welcome to Domain 2')
        self.getPage('/', [('Host', 'www.mydom3.com')])
        self.assertBody('Welcome to Domain 3')
        self.getPage('/', [('Host', 'www.mydom4.com')])
        self.assertBody('Under construction')

        # Test GET, POST, and positional params
        self.getPage('/method?value=root')
        self.assertBody('You sent root')
        self.getPage('/vmethod?value=dom2+GET', [('Host', 'www.mydom2.com')])
        self.assertBody('You sent dom2 GET')
        self.getPage('/vmethod', [('Host', 'www.mydom3.com')], method='POST',
                     body='value=dom3+POST')
        self.assertBody('You sent dom3 POST')
        self.getPage('/vmethod/pos', [('Host', 'www.mydom3.com')])
        self.assertBody('You sent pos')

        # Test that cherrypy.url uses the browser url, not the virtual url
        self.getPage('/url', [('Host', 'www.mydom2.com')])
        self.assertBody('%s://www.mydom2.com/nextpage' % self.scheme)

    def test_VHost_plus_Static(self):
        # Test static as a handler
        self.getPage('/static/style.css', [('Host', 'www.mydom2.com')])
        self.assertStatus('200 OK')
        self.assertHeader('Content-Type', 'text/css;charset=utf-8')

        # Test static in config
        self.getPage('/static2/dirback.jpg', [('Host', 'www.mydom2.com')])
        self.assertStatus('200 OK')
        self.assertHeaderIn('Content-Type', ['image/jpeg', 'image/pjpeg'])

        # Test static config with "index" arg
        self.getPage('/static2/', [('Host', 'www.mydom2.com')])
        self.assertStatus('200 OK')
        self.assertBody('Hello, world\r\n')
        # Since tools.trailing_slash is on by default, this should redirect
        self.getPage('/static2', [('Host', 'www.mydom2.com')])
        self.assertStatus(301)