<s> """<STR_LIT>""" <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . web . error import UnsupportedMethod <EOL> from twisted . web . resource import ( <EOL> NOT_FOUND , FORBIDDEN , Resource , ErrorPage , NoResource , ForbiddenResource , <EOL> getChildForRequest ) <EOL> from twisted . web . http_headers import Headers <EOL> from twisted . web . test . requesthelper import DummyRequest <EOL> class ErrorPageTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> errorPage = ErrorPage <EOL> noResource = NoResource <EOL> forbiddenResource = ForbiddenResource <EOL> def test_getChild ( self ) : <EOL> """<STR_LIT>""" <EOL> page = self . errorPage ( <NUM_LIT> , "<STR_LIT:foo>" , "<STR_LIT:bar>" ) <EOL> self . assertIdentical ( page . getChild ( b"<STR_LIT:name>" , object ( ) ) , page ) <EOL> def _pageRenderingTest ( self , page , code , brief , detail ) : <EOL> request = DummyRequest ( [ b'<STR_LIT>' ] ) <EOL> template = ( <EOL> u"<STR_LIT:\n>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> expected = template % ( code , brief , brief , detail ) <EOL> self . assertEqual ( <EOL> page . render ( request ) , expected . encode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( request . responseCode , code ) <EOL> self . assertEqual ( <EOL> request . responseHeaders , <EOL> Headers ( { b'<STR_LIT>' : [ b'<STR_LIT>' ] } ) ) <EOL> def test_errorPageRendering ( self ) : <EOL> """<STR_LIT>""" <EOL> code = <NUM_LIT> <EOL> brief = "<STR_LIT>" <EOL> detail = "<STR_LIT>" <EOL> page = self . errorPage ( code , brief , detail ) <EOL> self . _pageRenderingTest ( page , code , brief , detail ) <EOL> def test_noResourceRendering ( self ) : <EOL> """<STR_LIT>""" <EOL> detail = "<STR_LIT>" <EOL> page = self . noResource ( detail ) <EOL> self . _pageRenderingTest ( page , NOT_FOUND , "<STR_LIT>" , detail ) <EOL> def test_forbiddenResourceRendering ( self ) : <EOL> """<STR_LIT>""" <EOL> detail = "<STR_LIT>" <EOL> page = self . forbiddenResource ( detail ) <EOL> self . _pageRenderingTest ( page , FORBIDDEN , "<STR_LIT>" , detail ) <EOL> class DynamicChild ( Resource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , request ) : <EOL> Resource . __init__ ( self ) <EOL> self . path = path <EOL> self . request = request <EOL> class DynamicChildren ( Resource ) : <EOL> """<STR_LIT>""" <EOL> def getChild ( self , path , request ) : <EOL> return DynamicChild ( path , request ) <EOL> class BytesReturnedRenderable ( Resource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response ) : <EOL> """<STR_LIT>""" <EOL> Resource . __init__ ( self ) <EOL> self . _response = response <EOL> def render_GET ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return self . _response <EOL> class ImplicitAllowedMethods ( Resource ) : <EOL> """<STR_LIT>""" <EOL> def render_GET ( self , request ) : <EOL> pass <EOL> def render_PUT ( self , request ) : <EOL> pass <EOL> class ResourceTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_staticChildren ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = Resource ( ) <EOL> child = Resource ( ) <EOL> sibling = Resource ( ) <EOL> resource . putChild ( b"<STR_LIT:foo>" , child ) <EOL> resource . putChild ( b"<STR_LIT:bar>" , sibling ) <EOL> self . assertIdentical ( <EOL> child , resource . getChildWithDefault ( b"<STR_LIT:foo>" , DummyRequest ( [ ] ) ) ) <EOL> def test_dynamicChildren ( self ) : <EOL> """<STR_LIT>""" <EOL> path = b"<STR_LIT:foo>" <EOL> request = DummyRequest ( [ ] ) <EOL> resource = DynamicChildren ( ) <EOL> child = resource . getChildWithDefault ( path , request ) <EOL> self . assertIsInstance ( child , DynamicChild ) <EOL> self . assertEqual ( child . path , path ) <EOL> self . assertIdentical ( child . request , request ) <EOL> def test_defaultHEAD ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = b"<STR_LIT>" <EOL> request = DummyRequest ( [ ] ) <EOL> request . method = b'<STR_LIT>' <EOL> resource = BytesReturnedRenderable ( expected ) <EOL> self . assertEqual ( expected , resource . render ( request ) ) <EOL> def test_explicitAllowedMethods ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = [ b'<STR_LIT:GET>' , b'<STR_LIT>' , b'<STR_LIT>' ] <EOL> resource = Resource ( ) <EOL> resource . allowedMethods = expected <EOL> request = DummyRequest ( [ ] ) <EOL> request . method = b'<STR_LIT>' <EOL> exc = self . assertRaises ( UnsupportedMethod , resource . render , request ) <EOL> self . assertEqual ( set ( expected ) , set ( exc . allowedMethods ) ) <EOL> def test_implicitAllowedMethods ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = set ( [ b'<STR_LIT:GET>' , b'<STR_LIT>' , b'<STR_LIT>' ] ) <EOL> resource = ImplicitAllowedMethods ( ) <EOL> request = DummyRequest ( [ ] ) <EOL> request . method = b'<STR_LIT>' <EOL> exc = self . assertRaises ( UnsupportedMethod , resource . render , request ) <EOL> self . assertEqual ( expected , set ( exc . allowedMethods ) ) <EOL> class GetChildForRequestTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_exhaustedPostPath ( self ) : <EOL> """<STR_LIT>""" <EOL> request = DummyRequest ( [ ] ) <EOL> resource = Resource ( ) <EOL> result = getChildForRequest ( resource , request ) <EOL> self . assertIdentical ( resource , result ) <EOL> def test_leafResource ( self ) : <EOL> """<STR_LIT>""" <EOL> request = DummyRequest ( [ b"<STR_LIT:foo>" , b"<STR_LIT:bar>" ] ) <EOL> resource = Resource ( ) <EOL> resource . isLeaf = True <EOL> result = getChildForRequest ( resource , request ) <EOL> self . assertIdentical ( resource , result ) <EOL> def test_postPathToPrePath ( self ) : <EOL> """<STR_LIT>""" <EOL> request = DummyRequest ( [ b"<STR_LIT:foo>" , b"<STR_LIT:bar>" ] ) <EOL> root = Resource ( ) <EOL> child = Resource ( ) <EOL> child . isLeaf = True <EOL> root . putChild ( b"<STR_LIT:foo>" , child ) <EOL> self . assertIdentical ( child , getChildForRequest ( root , request ) ) <EOL> self . assertEqual ( request . prepath , [ b"<STR_LIT:foo>" ] ) <EOL> self . assertEqual ( request . postpath , [ b"<STR_LIT:bar>" ] ) </s>
