def _makeDummyRequest(self, postpath, path, site): request = DummyRequest(postpath) if path is not None: request.path = path if site is not None: request.channel.site = site return request
def test_rootURLNotRedirectedWithLeafRoot(self): req = DummyRequest(['']) req.uri = '/' r = Leaf() site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, Leaf), res)
def test_rootURLNotRedirectedWithNonLeafRoot(self): # the '' is necessary for this test, not for the above req = DummyRequest(['']) req.uri = '/' r = NonLeafWithIndexChild() site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, Leaf), res)
def test_404urlWithSlashCrud(self): req = DummyRequest(['hello', '', '', '']) req.uri = '/hello///' r = NonLeaf() hello = Leaf() r.putChild('hello', hello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, HelpfulNoResource), res)
def test_404forBadPath(self): req = DummyRequest(['hello']) req.uri = '/hello' r = NonLeaf() nothello = Leaf() r.putChild('nothello', nothello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, HelpfulNoResource), res)
def test_notRedirectedBecauseResourceIsNotBetter2(self): # For non-leaf req = DummyRequest(['hello']) req.uri = '/hello' r = NonLeaf() hello = NonLeafPlainResource() r.putChild('hello', hello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, NonLeafPlainResource), res)
def test_normalRequestNotRedirected(self): req = DummyRequest(['hello', '']) req.uri = '/hello/' r = NonLeaf() hello = Leaf() r.putChild('hello', hello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, Leaf), res)
def test_normalRequestToNonLeafNonLeafNotRedirected(self): # ugh. need to do integration testing and send real requests req = DummyRequest(['hello', '', '']) req.uri = '/hello/' r = NonLeaf() hello = NonLeafWithNonLeafIndexChild() r.putChild('hello', hello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, Leaf), res)
def test_redirectedToPathPlusSlash3(self): # For non-leaf -> non-leaf req = DummyRequest(['hello']) req.uri = '/hello' r = NonLeaf() hello = NonLeafWithNonLeafIndexChild() r.putChild('hello', hello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, RedirectingResource), res) self.assertEqual("/hello/", res._location)
def test_httpsRequest(self): clock = Clock() rco = ResponseCacheOptions( cacheTime=3600, httpCachePublic=False, httpsCachePublic=True) request = DummyRequest([]) request.isSecure = lambda: True setCachingHeadersOnRequest(request, rco, getTime=lambda: clock.seconds()) self.assertEqual({ 'Cache-Control': ['max-age=3600, public'], 'Date': ['Thu, 01 Jan 1970 00:00:00 GMT'], 'Expires': ['Thu, 01 Jan 1970 01:00:00 GMT']}, dict(request.responseHeaders.getAllRawHeaders()))
def makeRequestForPath(site, path): """ @param site: a L{server.Site}. @param path: a C{str} URL-encoded path that starts with C{"/"}. @return: a request that requests C{path}. """ # Unquote URL with the same function that twisted.web.server uses. postpath = unquote(path).split('/') postpath.pop(0) dummyRequest = DummyRequest(postpath) dummyRequest.path = path dummyRequest.channel.site = site return dummyRequest
def test_getChildCalledForNonexistentChild(self): req = DummyRequest(['']) r = DynamicBetterResource() uri = 'hello' child = r.getChildWithDefault(uri, req) self.assertIdentical(child, r.childResource) self.assertEqual(r.path, uri) self.assertIdentical(r.request, req)
def test_noCache(self): """ If C{cacheTime} is 0, appropriate headers are set. """ clock = Clock() # Even though these are both public=True, it correctly sets ", private". rco = ResponseCacheOptions( cacheTime=0, httpCachePublic=True, httpsCachePublic=True) request = DummyRequest([]) setCachingHeadersOnRequest(request, rco, getTime=lambda: clock.seconds()) self.assertEqual({ 'Cache-Control': ['max-age=0, private'], # A Date header is set even in this case. 'Date': ['Thu, 01 Jan 1970 00:00:00 GMT'], 'Expires': ['-1']}, dict(request.responseHeaders.getAllRawHeaders()))
def test_requestAlreadyHasHeaders(self): """ If the request passed to L{setCachingHeadersOnRequest} already has headers, existing Date/Expires/Cache-Control headers are replaced, and irrelevant ones are kept. """ clock = Clock() rco = ResponseCacheOptions( cacheTime=3600, httpCachePublic=False, httpsCachePublic=True) request = DummyRequest([]) request.responseHeaders.setRawHeaders('cache-control', ['X', 'Y']) request.responseHeaders.setRawHeaders('date', ['whenever']) request.responseHeaders.setRawHeaders('expires', ['sometime']) request.responseHeaders.setRawHeaders('extra', ['one', 'two']) setCachingHeadersOnRequest(request, rco, getTime=lambda: clock.seconds()) self.assertEqual({ 'Cache-Control': ['max-age=3600, private'], 'Date': ['Thu, 01 Jan 1970 00:00:00 GMT'], 'Expires': ['Thu, 01 Jan 1970 01:00:00 GMT'], 'Extra': ['one', 'two']}, dict(request.responseHeaders.getAllRawHeaders()))
def test_404forStrangeResourceBecauseNoIndex(self): req = DummyRequest(['hello']) req.uri = '/hello' r = NonLeaf() hello = NonLeafWithChildChild() r.putChild('hello', hello) site = self._makeSite(r) res = site.getResourceFor(req) self.assertTrue(isinstance(res, HelpfulNoResource), res) # Sanity check that child is accessible req2 = DummyRequest(['hello', 'child']) req2.uri = '/hello/child' res2 = site.getResourceFor(req2) self.assertTrue(isinstance(res2, RedirectingResource), res2) self.assertEqual("/hello/child/", res2._location)