[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/HTTP/tests - testPublisher.py:1.1.2.1

Jim Fulton jim@zope.com
Sat, 16 Mar 2002 09:44:02 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Publisher/HTTP/tests
In directory cvs.zope.org:/tmp/cvs-serv24057/python/Zope/Publisher/HTTP/tests

Added Files:
      Tag: Zope-3x-branch
	testPublisher.py 
Log Message:
Checking in partial publisher refactoring on the
Zope3-publisher-refactor-branch branch to facilitate collaboration
with Stephan.



=== Added File Zope3/lib/python/Zope/Publisher/HTTP/tests/testPublisher.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors.  All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 1.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.
##############################################################################
"""

$Id: testPublisher.py,v 1.1.2.1 2002/03/16 14:44:01 jim Exp $
"""

import unittest

from Zope.Publisher.Publish import publish
from Zope.Publisher.BaseRequest import BaseRequest
from Zope.Publisher.DefaultPublication import DefaultPublication
from Zope.Publisher.Exceptions import Retry, Unauthorized, NotFound, DebugError
from Zope.Publisher.IPublication import IPublication

from Interface.Verify import verifyClass
from Interface.Implements import instancesOfObjectImplements

from StringIO import StringIO

class TestPublication(DefaultPublication):
    # Override handleException to reraise for testing purposes
    def handleException(self, request, exc_info, retry_allowed=1):
        raise exc_info[0], exc_info[1], exc_info[2]

class PublisherTests(unittest.TestCase):
    def setUp(self):
        class AppRoot:
            " "

        class Folder:
            " "

        class Item:
            " "
            def __call__(self):
                return "item"

        class NoDocstringItem:
            def __call__(self):
                return "Yo! No docstring!"

        self.app = AppRoot()
        self.app.folder = Folder()
        self.app.folder.item = Item()

        self.app._item = Item()
        self.app.noDocString = NoDocstringItem()

    def _createRequest(self, path):
        publication = TestPublication(self.app)
        path = path.split('/')
        request = BaseRequest(path, StringIO(''), StringIO())
        request.setPublication(publication)      
        return request

    def _publisherResults(self, path):
        request = self._createRequest(path)
        publish(request)
        return request.getResponse().outstream.getvalue()

    def testImplementsIPublication(self):
        self.failUnless(IPublication.isImplementedBy(
                            DefaultPublication(self.app)))

    def testInterfacesVerify(self):
        for interface in instancesOfObjectImplements(DefaultPublication):
            verifyClass(interface, DefaultPublication)

    def testTraversalToItem(self):
        res = self._publisherResults('/folder/item')
        self.failUnlessEqual(res, 'item')
        res = self._publisherResults('/folder/item/')
        self.failUnlessEqual(res, 'item')
        res = self._publisherResults('/folder/./item')
        self.failUnlessEqual(res, 'item')
        res = self._publisherResults('/folder/../folder/item/../item')
        self.failUnlessEqual(res, 'item')
        res = self._publisherResults('folder/item')
        self.failUnlessEqual(res, 'item')

    def testUnderscoreUnauthorizedException(self):
        self.assertRaises(Unauthorized, self._publisherResults, '/_item')

    def testNotFoundException(self):
        self.assertRaises(NotFound, self._publisherResults, '/foo')

    def testDebugError(self):
        self.assertRaises(DebugError, self._publisherResults, '/noDocString')

def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(PublisherTests)

if __name__=='__main__':
    unittest.TextTestRunner().run( test_suite() )