def sendback(self, result, request): """ Send back a result to the browser :param request: the HTTP request :type request: :class:`~twisted.web.server.Request` :param result: the result for send back to the browser :type result: dict """ self.prepare_headers(request, result.code, result.headers) try: if type(result.subject) is not str: d = asyncjson.AsyncJSON(result.subject).begin(request) d.addCallback(lambda ignored: request.finish()) return d else: request.write(result.subject) request.finish() except Exception as error: log.err(error) request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.write(error) request.finish() return
def test_asyncjson(self): consumer = Spy(Request) ajson = ProxySpy(asyncjson.AsyncJSON({'id': 1, 'name': 'Test'})) r = yield ajson.begin(consumer) for p in r: pass assert_that(consumer.registerProducer, called().times(1)) assert_that(consumer.write, called()) assert_that(consumer.unregisterProducer, called().times(1)) assert_that(ajson.begin, called()) self.flushLoggedErrors()