/usr/share/pyshared/zope/traversing/tests/test_vhosting.py is in python-zope.traversing 3.14.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | ##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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.
"""
import os
import unittest
from StringIO import StringIO
import transaction
from zope.browserresource.resource import Resource
from zope.configuration import xmlconfig
from zope.container.contained import Contained
from zope.pagetemplate.pagetemplate import PageTemplate
from zope.pagetemplate.engine import AppPT
from zope.publisher.browser import BrowserRequest
from zope.publisher.publish import publish
from zope.publisher.skinnable import setDefaultSkin
from zope.security.checker import defineChecker, NamesChecker, NoProxy
from zope.security.checker import _checkers, undefineChecker
from zope.site.folder import Folder
from zope.site.folder import rootFolder
from zope.testing.cleanup import cleanUp
from zope.traversing.api import traverse
from zope.traversing.testing import browserResource
class MyObj(Contained):
def __getitem__(self, key):
return traverse(self, '/foo/bar/' + key)
class MyPageTemplate(AppPT, PageTemplate):
def pt_getContext(self, instance, request, **_kw):
# instance is a View component
namespace = super(MyPageTemplate, self).pt_getContext(**_kw)
namespace['template'] = self
namespace['request'] = request
namespace['container'] = namespace['context'] = instance
return namespace
def render(self, instance, request, *args, **kw):
return self.pt_render(self.pt_getContext(instance, request))
class MyPageEval(object):
def index(self, **kw):
"""Call a Page Template"""
template = self.context
request = self.request
return template.render(template.__parent__, request, **kw)
class MyFolderPage(object):
def index(self, **kw):
"""My folder page"""
self.request.response.redirect('index.html')
return ''
class TestVirtualHosting(unittest.TestCase):
def setUp(self):
f = os.path.join(os.path.split(__file__)[0], 'ftesting.zcml')
xmlconfig.file(f)
self.app = rootFolder()
defineChecker(MyObj, NoProxy)
def tearDown(self):
undefineChecker(MyObj)
cleanUp()
def makeRequest(self, path=''):
env = {"HTTP_HOST": 'localhost',
"HTTP_REFERER": 'localhost'}
p = path.split('?')
if len(p) == 1:
env['PATH_INFO'] = p[0]
request = BrowserRequest(StringIO(''), env)
request.setPublication(DummyPublication(self.app))
setDefaultSkin(request)
return request
def publish(self, path):
return publish(self.makeRequest(path)).response
def test_request_url(self):
self.addPage('/pt', u'<span tal:replace="request/URL"/>')
self.verify('/pt', 'http://localhost/pt/index.html')
self.verify('/++vh++/++/pt',
'http://localhost/pt/index.html')
self.verify('/++vh++https:localhost:443/++/pt',
'https://localhost/pt/index.html')
self.verify('/++vh++https:localhost:443/fake/folders/++/pt',
'https://localhost/fake/folders/pt/index.html')
self.addPage('/foo/bar/pt', u'<span tal:replace="request/URL"/>')
self.verify('/foo/bar/pt', 'http://localhost/foo/bar/pt/index.html')
self.verify('/foo/bar/++vh++/++/pt',
'http://localhost/pt/index.html')
self.verify('/foo/bar/++vh++https:localhost:443/++/pt',
'https://localhost/pt/index.html')
self.verify('/foo/++vh++https:localhost:443/fake/folders/++/bar/pt',
'https://localhost/fake/folders/bar/pt/index.html')
def test_request_redirect(self):
self.addPage('/foo/index.html', u'Spam')
self.verifyRedirect('/foo', 'http://localhost/foo/index.html')
self.verifyRedirect('/++vh++https:localhost:443/++/foo',
'https://localhost/foo/index.html')
self.verifyRedirect('/foo/++vh++https:localhost:443/bar/++',
'https://localhost/bar/index.html')
def test_absolute_url(self):
self.addPage('/pt', u'<span tal:replace="context/@@absolute_url"/>')
self.verify('/pt', 'http://localhost')
self.verify('/++vh++/++/pt',
'http://localhost')
self.verify('/++vh++https:localhost:443/++/pt',
'https://localhost')
self.verify('/++vh++https:localhost:443/fake/folders/++/pt',
'https://localhost/fake/folders')
self.addPage('/foo/bar/pt',
u'<span tal:replace="context/@@absolute_url"/>')
self.verify('/foo/bar/pt', 'http://localhost/foo/bar')
self.verify('/foo/bar/++vh++/++/pt',
'http://localhost')
self.verify('/foo/bar/++vh++https:localhost:443/++/pt',
'https://localhost')
self.verify('/foo/++vh++https:localhost:443/fake/folders/++/bar/pt',
'https://localhost/fake/folders/bar')
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')
self.verify('/foo/++vh++https:localhost:443/++/bar/pt',
'https://localhost/bar/pt')
def test_resources(self):
browserResource('quux', Resource)
# Only register the checker once, so that multiple test runs pass.
if Resource not in _checkers:
defineChecker(Resource, NamesChecker(['__call__']))
self.addPage('/foo/bar/pt',
u'<span tal:replace="context/++resource++quux" />')
self.verify('/foo/bar/pt', 'http://localhost/@@/quux')
self.verify('/foo/++vh++https:localhost:443/fake/folders/++/bar/pt',
'https://localhost/fake/folders/@@/quux')
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.app #self.connection.root()['Application']
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 = MyPageTemplate()
page.pt_edit(content, 'text/html')
self.createObject(path, page)
def verify(self, path, content):
result = self.publish(path)
self.assertEquals(result.getStatus(), 200)
self.assertEquals(result.consumeBody(), content)
def verifyRedirect(self, path, location):
result = self.publish(path)
self.assertEquals(result.getStatus(), 302)
self.assertEquals(result.getHeader('Location'), location)
class DummyPublication:
def __init__(self, app):
self.app = app
def beforeTraversal(self, request):
"""Pre-traversal hook.
This is called *once* before any traversal has been done.
"""
def getApplication(self, request):
"""Returns the object where traversal should commence.
"""
return self.app
def callTraversalHooks(self, request, ob):
"""Invokes any traversal hooks associated with the object.
This is called before traversing each object. The ob argument
is the object that is about to be traversed.
"""
def traverseName(self, request, ob, name):
"""Traverses to the next object.
Name must be an ASCII string or Unicode object."""
if name == 'index.html':
from zope.component import queryMultiAdapter
view = queryMultiAdapter((ob, request), name=name)
if view is None:
from zope.publisher.interfaces import NotFound
raise NotFound(ob, name)
return view
else:
from zope.traversing.publicationtraverse \
import PublicationTraverserWithoutProxy
t = PublicationTraverserWithoutProxy()
return t.traverseName(request, ob, name)
def afterTraversal(self, request, ob):
"""Post-traversal hook.
This is called after all traversal.
"""
def callObject(self, request, ob):
"""Call the object, returning the result.
For GET/POST this means calling it, but for other methods
(including those of WebDAV and FTP) this might mean invoking
a method of an adapter.
"""
from zope.publisher.publish import mapply
return mapply(ob, request.getPositionalArguments(), request)
def afterCall(self, request, ob):
"""Post-callObject hook (if it was successful).
"""
def handleException(self, ob, request, exc_info, retry_allowed=1):
"""Handle an exception
Either:
- sets the body of the response, request.response, or
- raises a Retry exception, or
- throws another exception, which is a Bad Thing.
"""
import traceback
traceback.print_exception(*exc_info)
def endRequest(self, request, ob):
"""Do any end-of-request cleanup
"""
def getDefaultTraversal(self, request, ob):
if hasattr(ob, 'index'):
return ob, ()
else:
return ob, ('index.html',)
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestVirtualHosting))
return suite
if __name__ == '__main__':
unittest.main()
|