def test_json(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})

        data = {
            os.urandom(6).encode("hex"): os.urandom(6).encode("hex"),
            os.urandom(6).encode("hex"): os.urandom(6).encode("hex")
        }

        r = Response(deferred, twisted_response, request)
        map(r.dataReceived, json.dumps(data))
        r.connectionLost(responseDone)
        self.assertEqual(self.callbacks, set(["success"]))
        self.assertTrue(r._done)
        self.assertEqual(r.json(), data)
        return deferred
    def test_connection_done(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)
        r.connectionLost(connectionDone)
        self.assertEqual(self.callbacks, set(["failure"]))
        self.assertFalse(r._done)
        return deferred