示例#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)
示例#2
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)
示例#3
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
示例#4
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)
示例#5
0
    def test_pullException(self):
        class Failer:
            def read(self):
                raise RuntimeError

        return stream.readStream(
            Failer(),
            lambda _: None).addErrback(lambda _: _.trap(RuntimeError))
示例#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)
示例#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)
示例#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
示例#9
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)
示例#10
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)
示例#11
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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#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)
示例#18
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)
示例#19
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)
示例#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)
示例#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)
示例#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)
示例#23
0
def noDataFromStream(stream):
    def gotData(data):
        if data:
            raise ValueError("Stream contains unexpected data.")
    return readStream(stream, gotData)
示例#24
0
def noDataFromStream(stream):
    def gotData(data):
        if data:
            raise ValueError("Stream contains unexpected data.")

    return readStream(stream, gotData)
示例#25
0
 def test_processingException(self):
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(s, lambda x: 1 / 0).addErrback(
         lambda _: _.trap(ZeroDivisionError))
示例#26
0
 def test_pullException(self):
     class Failer:
         def read(self):
             raise RuntimeError
     return stream.readStream(Failer(), lambda _: None).addErrback(
         lambda _: _.trap(RuntimeError))
示例#27
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"]))
示例#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"]))
示例#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))