summaryrefslogtreecommitdiff
path: root/ftests/test_vhosting.py
blob: 873b5460d28da6440c73de633586d052abb79b03 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Functional tests for virtual hosting.

$Id$
"""
import unittest
from zope.app.tests import ztapi
from zope.app.tests.functional import BrowserTestCase
from zope.app.folder import Folder
import transaction
from zope.app.publisher.browser.resource import Resource
from zope.app.traversing.api import traverse
from zope.security.checker import defineChecker, NamesChecker, NoProxy
from zope.app.container.contained import Contained
from zope.app.zptpage.zptpage import ZPTPage

class MyObj(Contained):
    def __getitem__(self, key):
        return traverse(self, '/foo/bar/' + key)

defineChecker(MyObj, NoProxy)

class TestVirtualHosting(BrowserTestCase):

    def test_request_url(self):
        self.addPage('/pt', u'<span tal:replace="request/URL"/>')
        self.verify('/pt', 'http://localhost/pt/index.html\n')
        self.verify('/++vh++/++/pt',
                    'http://localhost/pt/index.html\n')
        self.verify('/++vh++https:otherhost:443/++/pt',
                    'https://otherhost/pt/index.html\n')
        self.verify('/++vh++https:otherhost:443/fake/folders/++/pt',
                    'https://otherhost/fake/folders/pt/index.html\n')

        self.addPage('/foo/bar/pt', u'<span tal:replace="request/URL"/>')
        self.verify('/foo/bar/pt', 'http://localhost/foo/bar/pt/index.html\n')
        self.verify('/foo/bar/++vh++/++/pt',
                    'http://localhost/pt/index.html\n')
        self.verify('/foo/bar/++vh++https:otherhost:443/++/pt',
                    'https://otherhost/pt/index.html\n')
        self.verify('/foo/++vh++https:otherhost:443/fake/folders/++/bar/pt',
                    'https://otherhost/fake/folders/bar/pt/index.html\n')

    def test_request_base(self):
        self.addPage('/pt', u'<head></head>')
        self.verify('/pt',
                    '<head>\n<base href="http://localhost/pt/index.html" />\n'
                    '</head>\n')
        self.verify('/++vh++/++/pt',
                    '<head>\n<base href="http://localhost/pt/index.html" />\n'
                    '</head>\n')
        self.verify('/++vh++https:otherhost:443/++/pt',
                    '<head>\n'
                    '<base href="https://otherhost/pt/index.html" />'
                    '\n</head>\n')
        self.verify('/++vh++https:otherhost:443/fake/folders/++/pt',
                    '<head>\n<base href='
                    '"https://otherhost/fake/folders/pt/index.html" />'
                    '\n</head>\n')

        self.addPage('/foo/bar/pt', u'<head></head>')
        self.verify('/foo/bar/pt',
                    '<head>\n<base '
                    'href="http://localhost/foo/bar/pt/index.html" />\n'
                    '</head>\n')
        self.verify('/foo/bar/++vh++/++/pt',
                    '<head>\n<base href="http://localhost/pt/index.html" />\n'
                    '</head>\n')
        self.verify('/foo/bar/++vh++https:otherhost:443/++/pt',
                    '<head>\n'
                    '<base href="https://otherhost/pt/index.html" />'
                    '\n</head>\n')
        self.verify('/foo/++vh++https:otherhost:443/fake/folders/++/bar/pt',
                    '<head>\n<base href='
                    '"https://otherhost/fake/folders/bar/pt/index.html" />'
                    '\n</head>\n')

    def test_request_redirect(self):
        self.addPage('/foo/index.html', u'Spam')
        self.verifyRedirect('/foo', 'http://localhost/foo/index.html')
        self.verifyRedirect('/++vh++https:otherhost:443/++/foo',
                            'https://otherhost/foo/index.html')
        self.verifyRedirect('/foo/++vh++https:otherhost:443/bar/++',
                            'https://otherhost/bar/index.html')

    def test_absolute_url(self):
        self.addPage('/pt', u'<span tal:replace="template/@@absolute_url"/>')
        self.verify('/pt', 'http://localhost/pt\n')
        self.verify('/++vh++/++/pt',
                    'http://localhost/pt\n')
        self.verify('/++vh++https:otherhost:443/++/pt',
                    'https://otherhost/pt\n')
        self.verify('/++vh++https:otherhost:443/fake/folders/++/pt',
                    'https://otherhost/fake/folders/pt\n')

        self.addPage('/foo/bar/pt',
                     u'<span tal:replace="template/@@absolute_url"/>')
        self.verify('/foo/bar/pt', 'http://localhost/foo/bar/pt\n')
        self.verify('/foo/bar/++vh++/++/pt',
                    'http://localhost/pt\n')
        self.verify('/foo/bar/++vh++https:otherhost:443/++/pt',
                    'https://otherhost/pt\n')
        self.verify('/foo/++vh++https:otherhost:443/fake/folders/++/bar/pt',
                    'https://otherhost/fake/folders/bar/pt\n')

    def test_absolute_url_absolute_traverse(self):
        self.createObject('/foo/bar/obj', MyObj())
        self.addPage('/foo/bar/pt',
                     u'<span tal:replace="container/obj/pt/@@absolute_url"/>')
        self.verify('/foo/bar/pt', 'http://localhost/foo/bar/pt\n')
        self.verify('/foo/++vh++https:otherhost:443/++/bar/pt',
                    'https://otherhost/bar/pt\n')

    def test_resources(self):
        ztapi.browserResource('quux', Resource)
        defineChecker(Resource, NamesChecker(['__call__']))
        self.addPage('/foo/bar/pt',
                     u'<span tal:replace="context/++resource++quux" />')
        self.verify('/foo/bar/pt', 'http://localhost/@@/quux\n')
        self.verify('/foo/++vh++https:otherhost:443/fake/folders/++/bar/pt',
                    'https://otherhost/fake/folders/@@/quux\n')

    def createFolders(self, path):
        """addFolders('/a/b/c/d') would traverse and/or create three nested
        folders (a, b, c) and return a tuple (c, 'd') where c is a Folder
        instance at /a/b/c."""
        folder = self.getRootFolder()
        if path[0] == '/':
            path = path[1:]
        path = path.split('/')
        for id in path[:-1]:
            try:
                folder = folder[id]
            except KeyError:
                folder[id] = Folder()
                folder = folder[id]
        return folder, path[-1]

    def createObject(self, path, obj):
        folder, id = self.createFolders(path)
        folder[id] = obj
        transaction.commit()

    def addPage(self, path, content):
        page = ZPTPage()
        page.source = content
        self.createObject(path, page)

    def verify(self, path, content):
        result = self.publish(path)
        self.assertEquals(result.getStatus(), 200)
        self.assertEquals(result.getBody(), content)

    def verifyRedirect(self, path, location):
        result = self.publish(path)
        self.assertEquals(result.getStatus(), 302)
        self.assertEquals(result.getHeader('Location'), location)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestVirtualHosting))
    return suite


if __name__ == '__main__':
    unittest.main()