Ejemplo n.º 1
0
    def test_data_early(self):
        deferred = Deferred()
        deferred.addCallback(lambda _: self.callbacks.add("success"))
        deferred.addErrback(lambda _: self.callbacks.add("failure"))
        twisted_headers = Headers({"Content-Type": ["application/json"]})
        twisted_response = TWResponse(
            ("HTTP", 1, 1), 200, "OK", twisted_headers, None)
        request = Request(
            method="GET", url="/",
            kwargs={
                "headers": {
                    "Content-Type": "application/json"}, "data": None})

        r = Response(deferred, twisted_response, request)

        with self.assertRaises(RuntimeError):
            r.data()
Ejemplo n.º 2
0
    def test_data(self):
        deferred = Deferred()
        deferred.addCallback(lambda _: self.callbacks.add("success"))
        deferred.addErrback(lambda _: self.callbacks.add("failure"))

        twisted_headers = Headers({"Content-Type": ["application/json"]})
        twisted_response = TWResponse(
            ("HTTP", 1, 1), 200, "OK", twisted_headers, None)
        request = Request(
            method="GET", url="/",
            kwargs={
                "headers": {
                    "Content-Type": "application/json"}, "data": None})
        data1 = os.urandom(6).encode("hex")
        data2 = os.urandom(6).encode("hex")
        data = data1 + data2
        r = Response(deferred, twisted_response, request)
        r.dataReceived(data1)
        r.dataReceived(data2)
        r.connectionLost(responseDone)
        self.assertEqual(self.callbacks, set(["success"]))
        self.assertTrue(r._done)
        self.assertEqual(r.data(), data)
        return deferred