test_errorhandler.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » nevow » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » nevow » test » test_errorhandler.py

from zope.interface import implements
from twisted.python import log
from twisted.internet import defer
from nevow import appserver,context,inevow,loaders,rend,tags


class Root(rend.Page):
    docFactory = loaders.stan(T.html[T.p['Root']])



class NotFoundHandler(object):
    implements(inevow.ICanHandleNotFound)
    html = 'NotFoundHandler'
    def renderHTTP_notFound(self, ctx):
        return self.html

class BrokenException(Exception):
    pass

class BadNotFoundHandler(object):
    implements(inevow.ICanHandleNotFound)
    html = 'NotFoundHandler'
    exceptionType = BrokenException
    exceptionMessage ='Error from BadNotFoundHandler'
    def __init__(self, exceptionType=None):
        if exceptionType is not None:
            self.exceptionType = exceptionType
    def renderHTTP_notFound(self, ctx):
        raise self.exceptionType(self.exceptionMessage)


def getResource(root, path):
    ctx = context.RequestContext(tag=testutil.FakeRequest(uri=path))
    return appserver.NevowSite(root).getPageContextForRequestContext(ctx).addCallback(
        lambda newctx: newctx.tag)

def renderResource(uri, notFoundHandler=None):
    """Render a resource at some uri and return the response code and html.
    """

    root = Root()
    if notFoundHandler is not None:
        root.remember(notFoundHandler, inevow.ICanHandleNotFound)
    site = appserver.NevowSite(root)
    ctx = context.SiteContext(tag=site)

    request = testutil.FakeRequest(uri=uri)
    ctx = context.RequestContext(parent=ctx, tag=request)

    def waitmore(newctx):
        return defer.maybeDeferred(newctx.tag.renderHTTP, newctx).addCallback(lambda html: (request.code, html))
    return site.getPageContextForRequestContext(ctx).addCallback(waitmore)


class Test404(testutil.TestCase):

    def test_standard404(self):
        """Test the standard 404 handler.
        """
        root = Root()
        def later(resource):
            self.failUnless(isinstance(resource, rend.FourOhFour))
            def morelater((code, html)):
                self.assertEquals(rend.FourOhFour.notFound, html)
                self.assertEquals(code, 404)
            return renderResource('/foo').addCallback(morelater)
        return getResource(root, '/foo').addCallback(later)

    def test_remembered404Handler(self):
        def later((code, html)):
            self.assertEquals(html, NotFoundHandler.html)
            self.assertEquals(code, 404)

        return renderResource('/foo', notFoundHandler=NotFoundHandler()).addCallback(later)

    def test_keyErroringNotFoundHandler(self):
        def later((code, html)):
            self.assertEquals(rend.FourOhFour.notFound, html)
            self.assertEquals(code, 404)
            fe = log.flushErrors(BrokenException)
            self.assertEquals(len(fe), 1)
        return renderResource('/foo', notFoundHandler=BadNotFoundHandler()).addCallback(later)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.