2
from zope.interface import implements
3
from twisted.python import log
4
from twisted.internet import defer
5
from nevow import appserver, context, inevow, loaders, rend, tags as T, testutil
9
docFactory = loaders.stan(T.html[T.p['Root']])
13
class NotFoundHandler(object):
14
implements(inevow.ICanHandleNotFound)
15
html = 'NotFoundHandler'
16
def renderHTTP_notFound(self, ctx):
19
class BrokenException(Exception):
22
class BadNotFoundHandler(object):
23
implements(inevow.ICanHandleNotFound)
24
html = 'NotFoundHandler'
25
exceptionType = BrokenException
26
exceptionMessage ='Error from BadNotFoundHandler'
27
def __init__(self, exceptionType=None):
28
if exceptionType is not None:
29
self.exceptionType = exceptionType
30
def renderHTTP_notFound(self, ctx):
31
raise self.exceptionType(self.exceptionMessage)
34
def getResource(root, path):
35
ctx = context.RequestContext(tag=testutil.FakeRequest(uri=path))
36
return appserver.NevowSite(root).getPageContextForRequestContext(ctx).addCallback(
37
lambda newctx: newctx.tag)
39
def renderResource(uri, notFoundHandler=None):
40
"""Render a resource at some uri and return the response code and html.
44
if notFoundHandler is not None:
45
root.remember(notFoundHandler, inevow.ICanHandleNotFound)
46
site = appserver.NevowSite(root)
47
ctx = context.SiteContext(tag=site)
49
request = testutil.FakeRequest(uri=uri)
50
ctx = context.RequestContext(parent=ctx, tag=request)
53
return defer.maybeDeferred(newctx.tag.renderHTTP, newctx).addCallback(lambda html: (request.code, html))
54
return site.getPageContextForRequestContext(ctx).addCallback(waitmore)
57
class Test404(testutil.TestCase):
59
def test_standard404(self):
60
"""Test the standard 404 handler.
64
self.failUnless(isinstance(resource, rend.FourOhFour))
65
def morelater((code, html)):
66
self.assertEquals(rend.FourOhFour.notFound, html)
67
self.assertEquals(code, 404)
68
return renderResource('/foo').addCallback(morelater)
69
return getResource(root, '/foo').addCallback(later)
71
def test_remembered404Handler(self):
72
def later((code, html)):
73
self.assertEquals(html, NotFoundHandler.html)
74
self.assertEquals(code, 404)
76
return renderResource('/foo', notFoundHandler=NotFoundHandler()).addCallback(later)
78
def test_keyErroringNotFoundHandler(self):
79
def later((code, html)):
80
self.assertEquals(rend.FourOhFour.notFound, html)
81
self.assertEquals(code, 404)
82
fe = log.flushErrors(BrokenException)
83
self.assertEquals(len(fe), 1)
84
return renderResource('/foo', notFoundHandler=BadNotFoundHandler()).addCallback(later)