Exemple #1
0
 def test_pullFailure(self):
     l = []
     s = TestStreamer(['abcd', defer.fail(RuntimeError()), 'ijkl'])
     def test(result):
         result.trap(RuntimeError)
         self.assertEquals(l, ["abcd"])
     return stream.readStream(s, l.append).addErrback(test)
 def test_pullFailure(self):
     l = []
     s = TestStreamer(['abcd', defer.fail(RuntimeError()), 'ijkl'])
     def test(result):
         result.trap(RuntimeError)
         self.assertEquals(l, ["abcd"])
     return stream.readStream(s, l.append).addErrback(test)
 def assertListing(response):
     data = []
     d = readStream(response.stream, lambda s: data.append(str(s)))
     d.addCallback(lambda _: self.failIf(
         'dir2/' not in "".join(data), "'dir2' expected in listing: %r"
         % (data, )))
     return d
 def test_errouput(self):
     p = self.runCode("import sys\nfor i in range(100): sys.stderr.write('x' * 1000)")
     l = []
     d = stream.readStream(p.errStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), ("x" * 1000) * 100)
     p.run()
     return d.addCallback(verify)
Exemple #5
0
    def test_pullException(self):
        class Failer:
            def read(self):
                raise RuntimeError

        return stream.readStream(
            Failer(),
            lambda _: None).addErrback(lambda _: _.trap(RuntimeError))
Exemple #6
0
 def test_errouput(self):
     p = self.runCode("import sys\nfor i in range(100): sys.stderr.write('x' * 1000)")
     l = []
     d = stream.readStream(p.errStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), ("x" * 1000) * 100)
     p.run()
     return d.addCallback(verify)
Exemple #7
0
 def test_output(self):
     p = self.runCode("import sys\nfor i in range(100): sys.stdout.write('x' * 1000)")
     l = []
     d = stream.readStream(p.outStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), ("x" * 1000) * 100)
     d2 = p.run()
     return d.addCallback(verify).addCallback(lambda _: d2)
Exemple #8
0
 def assertListing(response):
     data = []
     d = readStream(response.stream, lambda s: data.append(str(s)))
     d.addCallback(lambda _: self.failIf(
         'dir2/' not in "".join(data),
         "'dir2' expected in listing: %r" % (data,)
     ))
     return d
 def test_output(self):
     p = self.runCode("import sys\nfor i in range(100): sys.stdout.write('x' * 1000)")
     l = []
     d = stream.readStream(p.outStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), ("x" * 1000) * 100)
     d2 = p.run()
     return d.addCallback(verify).addCallback(lambda _: d2)
 def test_input(self):
     p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())",
                      "hello world")
     l = []
     d = stream.readStream(p.outStream, l.append)
     d2 = p.run()
     def verify(_):
         self.assertEquals("".join(l), "hello world")
         return d2
     return d.addCallback(verify)
 def test_processclosedinput(self):
     p = self.runCode("import sys; sys.stdout.write(sys.stdin.read(3));" +
                      "sys.stdin.close(); sys.stdout.write('def')",
                      "abc123")
     l = []
     d = stream.readStream(p.outStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), "abcdef")
     d2 = p.run()
     return d.addCallback(verify).addCallback(lambda _: d2)
Exemple #12
0
 def test_processclosedinput(self):
     p = self.runCode("import sys; sys.stdout.write(sys.stdin.read(3));" +
                      "sys.stdin.close(); sys.stdout.write('def')",
                      "abc123")
     l = []
     d = stream.readStream(p.outStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), "abcdef")
     d2 = p.run()
     return d.addCallback(verify).addCallback(lambda _: d2)
Exemple #13
0
 def test_input(self):
     p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())",
                      "hello world")
     l = []
     d = stream.readStream(p.outStream, l.append)
     d2 = p.run()
     def verify(_):
         self.assertEquals("".join(l), "hello world")
         return d2
     return d.addCallback(verify)
Exemple #14
0
def allDataFromStream(stream, filter=None):
    data = []
    def gotAllData(_):
        if not data:
            return None
        result = "".join([str(x) for x in data])
        if filter is None:
            return result
        else:
            return filter(result)
    return readStream(stream, data.append).addCallback(gotAllData)
Exemple #15
0
def allDataFromStream(stream, filter=None):
    data = []
    def gotAllData(_):
        if not data:
            return None
        result = "".join([str(x) for x in data])
        if filter is None:
            return result
        else:
            return filter(result)
    return readStream(stream, data.append).addCallback(gotAllData)
Exemple #16
0
    def doRequest(self, txn):

        # Generate an HTTP client request
        try:
            if "xpod" not in txn.logItems:
                txn.logItems["xpod"] = 0
            txn.logItems["xpod"] += 1

            response = (yield self._processRequest())

            if accountingEnabledForCategory("xPod"):
                self.loggedResponse = yield self.logResponse(response)
                emitAccounting("xPod", "",
                               self.loggedRequest + "\n" + self.loggedResponse,
                               "POST")

            if response.code == responsecode.OK:
                if self.writeStream is None:
                    data = (yield allDataFromStream(response.stream))
                    data = json.loads(data)
                else:
                    yield readStream(response.stream, self.writeStream.write)
                    content_type = response.headers.getHeader("content-type")
                    if content_type is None:
                        content_type = MimeType("application", "octet-stream")
                    content_disposition = response.headers.getHeader(
                        "content-disposition")
                    if content_disposition is None or "filename" not in content_disposition.params:
                        filename = ""
                    else:
                        filename = content_disposition.params["filename"]
                    self.writeStream.resetDetails(content_type, filename)
                    yield self.writeStream.loseConnection()
                    data = {
                        "result": "ok",
                        "content-type": content_type,
                        "name": filename,
                    }
            elif response.code == responsecode.BAD_REQUEST:
                data = (yield allDataFromStream(response.stream))
                data = json.loads(data)
            else:
                raise ValueError(
                    "Incorrect cross-pod response status code: {}".format(
                        response.code))

        except Exception as e:
            # Request failed
            log.error("Could not do cross-pod request : {request} {ex}",
                      request=self,
                      ex=e)
            raise ValueError("Failed cross-pod request: {}".format(e))

        returnValue(data)
Exemple #17
0
 def test_inputerror(self):
     p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())",
                      TestStreamer(["hello", defer.fail(ZeroDivisionError())]))
     l = []
     d = stream.readStream(p.outStream, l.append)
     d2 = p.run()
     def verify(_):
         self.assertEquals("".join(l), "hello")
         return d2
     def cbVerified(ignored):
         excs = self.flushLoggedErrors(ZeroDivisionError)
         self.assertEqual(len(excs), 1)
     return d.addCallback(verify).addCallback(cbVerified)
 def test_inputerror(self):
     p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())",
                      TestStreamer(["hello", defer.fail(ZeroDivisionError())]))
     l = []
     d = stream.readStream(p.outStream, l.append)
     d2 = p.run()
     def verify(_):
         self.assertEquals("".join(l), "hello")
         return d2
     def cbVerified(ignored):
         excs = self.flushLoggedErrors(ZeroDivisionError)
         self.assertEqual(len(excs), 1)
     return d.addCallback(verify).addCallback(cbVerified)
def readIntoFile(stream, outFile, maxlen):
    """Read the stream into a file, but not if it's longer than maxlen.
    Returns Deferred which will be triggered on finish.
    """
    curlen = [0]
    def done(_):
        return _
    def write(data):
        curlen[0] += len(data)
        if curlen[0] > maxlen:
            raise MimeFormatError("Maximum length of %d bytes exceeded." %
                                  maxlen)
        
        outFile.write(data)
    return readStream(stream, write).addBoth(done)
Exemple #20
0
 def test_render(self):
     """
     Test that the result from Data.render is acceptable, including the
     response code, the content-type header, and the actual response body
     itself.
     """
     response = iweb.IResponse(self.data.render(None))
     self.assertEqual(response.code, 200)
     self.assert_(response.headers.hasHeader("content-type"))
     self.assertEqual(response.headers.getHeader("content-type"),
                      http_headers.MimeType("text", "plain"))
     def checkStream(data):
         self.assertEquals(str(data), self.text)
     return stream.readStream(iweb.IResponse(self.data.render(None)).stream,
                              checkStream)
Exemple #21
0
def readIntoFile(stream, outFile, maxlen):
    """Read the stream into a file, but not if it's longer than maxlen.
    Returns Deferred which will be triggered on finish.
    """
    curlen = [0]
    def done(_):
        return _
    def write(data):
        curlen[0] += len(data)
        if curlen[0] > maxlen:
            raise MimeFormatError("Maximum length of %d bytes exceeded." %
                                  maxlen)
        
        outFile.write(data)
    return readStream(stream, write).addBoth(done)
Exemple #22
0
    def doRequest(self, txn):

        # Generate an HTTP client request
        try:
            if "xpod" not in txn.logItems:
                txn.logItems["xpod"] = 0
            txn.logItems["xpod"] += 1

            response = (yield self._processRequest())

            if accountingEnabledForCategory("xPod"):
                self.loggedResponse = yield self.logResponse(response)
                emitAccounting("xPod", "", self.loggedRequest + "\n" + self.loggedResponse, "POST")

            if response.code == responsecode.OK:
                if self.writeStream is None:
                    data = (yield allDataFromStream(response.stream))
                    data = json.loads(data)
                else:
                    yield readStream(response.stream, self.writeStream.write)
                    content_type = response.headers.getHeader("content-type")
                    if content_type is None:
                        content_type = MimeType("application", "octet-stream")
                    content_disposition = response.headers.getHeader("content-disposition")
                    if content_disposition is None or "filename" not in content_disposition.params:
                        filename = ""
                    else:
                        filename = content_disposition.params["filename"]
                    self.writeStream.resetDetails(content_type, filename)
                    yield self.writeStream.loseConnection()
                    data = {
                        "result": "ok",
                        "content-type": content_type,
                        "name": filename,
                    }
            elif response.code == responsecode.BAD_REQUEST:
                data = (yield allDataFromStream(response.stream))
                data = json.loads(data)
            else:
                raise ValueError("Incorrect cross-pod response status code: {}".format(response.code))

        except Exception as e:
            # Request failed
            log.error("Could not do cross-pod request : {request} {ex}", request=self, ex=e)
            raise ValueError("Failed cross-pod request: {}".format(e))

        returnValue(data)
Exemple #23
0
def noDataFromStream(stream):
    def gotData(data):
        if data:
            raise ValueError("Stream contains unexpected data.")
    return readStream(stream, gotData)
Exemple #24
0
def noDataFromStream(stream):
    def gotData(data):
        if data:
            raise ValueError("Stream contains unexpected data.")

    return readStream(stream, gotData)
 def test_processingException(self):
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(s, lambda x: 1 / 0).addErrback(
         lambda _: _.trap(ZeroDivisionError))
 def test_pullException(self):
     class Failer:
         def read(self):
             raise RuntimeError
     return stream.readStream(Failer(), lambda _: None).addErrback(
         lambda _: _.trap(RuntimeError))
 def test_pull(self):
     l = []
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(s, l.append).addCallback(
         lambda _: self.assertEquals(l, ["abcd", "efgh", "ijkl"]))
Exemple #28
0
 def test_pull(self):
     l = []
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(s, l.append).addCallback(
         lambda _: self.assertEquals(l, ["abcd", "efgh", "ijkl"]))
Exemple #29
0
 def test_processingException(self):
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(
         s, lambda x: 1 / 0).addErrback(lambda _: _.trap(ZeroDivisionError))