Ejemplo n.º 1
0
    def __call__(self, tchan_request):
        request = _to_request(tchan_request)

        v = _Validator(request)
        try:
            v.validate()
        except BadRequestError as e:
            raise TChBadRequestError(
                'BadRequest: %s\n' % str(e)
            )

        answer = gen.Future()

        @fail_to(answer)
        def on_dispatch(future):
            if not future.exception():
                response = future.result()
                tchan_response = Response(
                    body=response.body,
                    headers=headers.encode(
                        request.encoding, response.headers),
                    transport=None,  # TODO transport?
                )
                answer.set_result(tchan_response)
                return

            _handle_request_exception(future.exception(), request)

        maybe_async(
            self.handler.handle, request
        ).add_done_callback(on_dispatch)

        return answer
Ejemplo n.º 2
0
 def _validate(self, request):
     v = _Validator(request)
     if request.ttl:
         v.parse_ttl(request.ttl)
     try:
         v.validate()
     except BadRequestError as e:
         m = 'BadRequest: %s\n' % str(e)
         self.write(m)
         self.set_status(400)
         return False
     return True
Ejemplo n.º 3
0
def test_validator(req, ttl, want_err, want_msg, scrub_ttl):
    if scrub_ttl:
        req.ttl = None

    v = _Validator(req)

    if ttl:
        v.parse_ttl(ttl)

    if want_err:
        with pytest.raises(want_err) as e:
            v.validate()
        assert str(e.value) == want_msg
        return

    v.validate()