def _secondRequest(ign): request = SimpleRequest(site, "GET", '/sub/dummy') d2 = request.locateResource('/sub/dummy') d2.addCallback(lambda resource: self.getPage(request, resource)) d2.addCallback(_secondResponse) return d2
def test_scriptsExecute(self): """ Verify that CGI scripts within a CGIDirectory can actually be executed """ cgiBinDir = os.path.abspath(self.mktemp()) os.mkdir(cgiBinDir) root = twcgi.CGIDirectory(cgiBinDir) self.createScript(os.path.join(cgiBinDir, 'dummy')) cgiSubDir = os.path.join(cgiBinDir, 'sub') os.mkdir(cgiSubDir) self.createScript(os.path.join(cgiSubDir, 'dummy')) site = server.Site(root) request = SimpleRequest(site, "GET", "/dummy") d = request.locateResource('/dummy') def _firstResponse(res): self.failUnlessEqual(res, "cgi output%s" % os.linesep) def _firstRequest(resource): d1 = self.getPage(request, resource) d1.addCallback(_firstResponse) return d1 d.addCallback(_firstRequest) def _secondResponse(res): self.failUnlessEqual(res, "cgi output%s" % os.linesep) def _secondRequest(ign): request = SimpleRequest(site, "GET", '/sub/dummy') d2 = request.locateResource('/sub/dummy') d2.addCallback(lambda resource: self.getPage(request, resource)) d2.addCallback(_secondResponse) return d2 d.addCallback(_secondRequest) return d
def testOperatesOnStreamDirectly(self): def gotResponse(resp): self.assertEquals(resp.code, 200) self.assertEquals(resp.headers.getHeader('Content-Type'), http_headers.MimeType.fromString('text/plain')) stream = resp.stream resp.stream = None return defer.maybeDeferred(stream.read).addCallback( self.assertEquals, '42') req = SimpleRequest(None, 'GET', '/') d = self.doTestSCGI(req) d.addCallback(gotResponse) self.iterate(self.cxn) headers = parseSCGIHeaders(self.cxn.server.data) self.assertEquals(headers[0], ('CONTENT_LENGTH', '0')) self.failUnlessIn(('SCGI', '1'), headers) self.writeLines(self.cxn, [ 'Status: 200 OK', 'Content-Type: text/plain', 'Content-Length: 2', '', '42' ]) return d
def test_locateChild(self): resource = LeafResource() child, segments = (resource.locateChild( SimpleRequest(Site(resource), "GET", "/"), ("", "foo"), )) self.assertEquals(child, resource) self.assertEquals(segments, StopTraversal)
def test_renderHTTP(self): """ Test that if the renderHTTP method is ever called we authenticate the request and delegate rendering to the wrapper. """ self.protectedResource.responseText = "I hope you can see me." self.protectedResource.addSlash = True root = wrapper.HTTPAuthResource(self.protectedResource, [self.credFactory], self.portal, interfaces = (IHTTPUser,)) request = SimpleRequest(None, "GET", "/") request.prepath = [''] def _gotSecondResponse(response): self.assertEquals(response.code, 200) self.assertEquals(str(response.stream.read()), "I hope you can see me.") def _gotResponse(exception): response = exception.response self.assertEquals(response.code, 401) self.failUnless(response.headers.hasHeader('WWW-Authenticate')) self.assertEquals(response.headers.getHeader('WWW-Authenticate'), [('basic', {'realm': "test realm"})]) credentials = base64.encodestring('username:password') request.headers.setHeader('authorization', ['basic', credentials]) d = root.renderHTTP(request) d.addCallback(_gotSecondResponse) d = self.assertFailure(root.renderHTTP(request), http.HTTPError) d.addCallback(_gotResponse) return d
def test_renderHTTP(self): """ Test that if the renderHTTP method is ever called we authenticate the request and delegate rendering to the wrapper. """ self.protectedResource.responseText = "I hope you can see me." self.protectedResource.addSlash = True root = wrapper.HTTPAuthResource(self.protectedResource, [self.credFactory], self.portal, interfaces=(IHTTPUser, )) request = SimpleRequest(None, "GET", "/") request.prepath = [''] def _gotSecondResponse(response): self.assertEquals(response.code, 200) self.assertEquals(str(response.stream.read()), "I hope you can see me.") def _gotResponse(exception): response = exception.response self.assertEquals(response.code, 401) self.failUnless(response.headers.hasHeader('WWW-Authenticate')) self.assertEquals(response.headers.getHeader('WWW-Authenticate'), [('basic', { 'realm': "test realm" })]) credentials = base64.encodestring('username:password') request.headers.setHeader('authorization', ['basic', credentials]) d = root.renderHTTP(request) d.addCallback(_gotSecondResponse) d = self.assertFailure(root.renderHTTP(request), http.HTTPError) d.addCallback(_gotResponse) return d
def test_checkPreconditions_none(self): """ RenderMixin.checkPreconditions() checkPreconditions() returns None """ resource = TestResource() request = SimpleRequest(Site(resource), "SWEETHOOKUPS", "/") # Check that checkPreconditions without a raise doesn't barf self.assertEquals(resource.renderHTTP(request), responsecode.NO_CONTENT)
def testReadEmptyInput(self): """ Test that the CGI can successfully read from an empty input stream """ request = SimpleRequest(None, 'GET', '/cgi') resource = self.setUpResource(READINPUT_CGI) d = self.getPage(request, resource) d.addCallback(self._testReadEmptyInput_1) return d
def test_CGI(self): """ Test that the given DUMMY_CGI is executed and the expected output returned """ request = SimpleRequest(None, 'GET', '/cgi') resource = self.setUpResource(DUMMY_CGI) d = self.getPage(request, resource) d.addCallback(self._testCGI_1) return d
def test_readInput(self): """ Test that we can successfully read an input stream with data """ request = SimpleRequest(None, "POST", "/cgi", content="Here is your stdin") resource = self.setUpResource(READINPUT_CGI) d = self.getPage(request, resource) d.addCallback(self._testReadInput_1) return d
def test_checkPreconditions_raises(self): """ RenderMixin.checkPreconditions() Exception raised in checkPreconditions() """ resource = TestResource() request = SimpleRequest(Site(resource), "BLEARGH", "/") # Check that checkPreconditions raises as expected self.assertRaises(PreconditionError, resource.checkPreconditions, request) # Check that renderHTTP calls checkPreconditions self.assertRaises(PreconditionError, resource.renderHTTP, request)
def test_checkPreconditions_deferred(self): """ RenderMixin.checkPreconditions() checkPreconditions() returns a deferred """ resource = TestResource() request = SimpleRequest(Site(resource), "HOOKUPS", "/") # Check that checkPreconditions without a raise doesn't barf def checkResponse(response): self.assertEquals(response, responsecode.NO_CONTENT) d = resource.renderHTTP(request) d.addCallback(checkResponse)
def test_failsWithDifferentMethod(self): """ Test that the response fails if made for a different request method than it is being issued for. """ challenge = self.credentialFactory.getChallenge(clientAddress) clientResponse = authRequest1 % ( challenge['nonce'], self.getDigestResponse( challenge, "00000001"), challenge['opaque']) creds = self.credentialFactory.decode(clientResponse, SimpleRequest(None, 'POST', '/')) self.failIf(creds.checkPassword('password'))
def test_readAllInput(self): """ Test that we can all input can be read regardless of CONTENT_LENGTH """ request = SimpleRequest(None, "POST", "/cgi", content="Here is your stdin") resource = self.setUpResource(READALLINPUT_CGI) d = self.getPage(request, resource) d.addCallback(self._testReadAllInput_1) return d
def generateResponse(method): resource = TestResource() method = getattr(resource, "http_" + method) return method(SimpleRequest(Site(resource), method, "/"))
def _gotSecondResponse(response): self.assertEquals(response.code, 200) self.assertEquals(str(response.stream.read()), "I hope you can see me.") def _gotResponse(exception): response = exception.response self.assertEquals(response.code, 401) self.failUnless(response.headers.hasHeader('WWW-Authenticate')) self.assertEquals(response.headers.getHeader('WWW-Authenticate'), [('basic', { 'realm': "test realm" })]) credentials = base64.encodestring('username:password') request.headers.setHeader('authorization', ['basic', credentials]) d = root.renderHTTP(request) d.addCallback(_gotSecondResponse) d = self.assertFailure(root.renderHTTP(request), http.HTTPError) d.addCallback(_gotResponse) return d _trivial_GET = SimpleRequest(None, 'GET', '/')