lundi 4 février 2019

Tornado: correct way to test parent classes

I have a base handler that that other classes inherit from. When testing against the basic RequestHandler All testing is fine. However, when wrapping against BaseHandler I get tornado.simple_httpclient.HTTPStreamClosedError: Stream closed

Tornado 5.1.1

class BaseHandler(RequestHandler):
    def initialize(self):
        self.logger = self.application.logger
        self.redis = self.application.redis

    def data_received(self, chunk):
        pass

    def write_error(self, status_code, **kwargs):
        self.set_header('Content-Type', 'application/json')

        traceback_exception = (
            traceback
            .format_exception(*kwargs['exc_info'])
        )
        result = {
            'error': {
                'code': status_code,
                'message': self._reason,
                'traceback': traceback_exception,
            }
        }
        logger.error(result, request_body=self.request.arguments)
        self.finish(json.dumps(result))

    @property
    def input_cls(self):
        raise RuntimeError("input_cls must be implemented.")

    @property
    def output_cls(self):
        raise RuntimeError("output_cls must be set.")

    def on_finish(self):
        logger.info(tornado_log(self))
        super().on_finish()

a simple wrapper on top of that like below will return an error.

class API(BaseHandler):
    route = r'/api'
    async def get(self):
        self.write('OK')

the test case is set up like so. nothing fancy in App() just a few extra bits that do not cause any problems

class TestBaseHandler(AsyncHTTPTestCase):

    def get_app(self):
        return App()

    def test_BaseHandler(self):
        response = self.fetch('/api', raise_error=True)
        self.assertEqual(response.code, 200)

substituting BaseHandler for tornados RequestHandler will return a result. Googling the error says the client closed the connection and server throws that error. So something must be wrong with my test setup of wrapper handler classes but I cant seem to find any resources addressing that problem

Aucun commentaire:

Enregistrer un commentaire