Exemplo n.º 1
0
    async def test_set_context(self):
        ctx = Context()
        ctx.body = {
            'a': 1,
        }

        ctx.req = await Request.from_asgi_interface({
            'channel': 'http.request',
            'server': ('127.0.0.1', 9999),
            'client': ('127.0.0.1', 58175),
            'scheme': 'http',
            'http_version': '0.0',
            'method': 'POST',
            'path': '/',
            'query_string': b'',
            'headers': [
                [b'content-type', b'application/x-www-form-urlencoded'],
                [b'cache-control', b'no-cache'],
                [b'postman-token', b'e279159d-6af2-45da-87ac-1a331f317a60'],
                [b'user-agent', b'PostmanRuntime/7.1.1'],
                [b'accept', b'*/*'],
                [b'host', b'127.0.0.1:9999'],
                [b'accept-encoding', b'gzip, deflate'],
                [b'content-length', b'11'],
                [b'connection', b'keep-alive'],
            ]
        }, {})

        assert ctx.body['a'] == 1
        assert ctx.res.body['a'] == 1
        assert id(ctx.res.body) == id(ctx.body)
        assert id(ctx.res.status) == id(ctx.status)
Exemplo n.º 2
0
 async def _wrapper(message, channels):
     """
     :param message: is an ASGI message.
     :param channels: is a dictionary of
      <unicode string>:<channel interface>.
     :return: asgi application
     """
     if message['channel'] == 'http.request':
         # init context
         ctx = Context()
         # prepare request
         ctx.req = await Request.from_asgi_interface(message=message,
                                                     channels=channels)
         try:
             await exec_middleware(ctx=ctx,
                                   middleware_list=self.middleware_list)
         except MiddlewareParamsError as e:
             await channels['reply'].send({
                 'status':
                 500,
                 'headers':
                 MIME_TYPES.APPLICATION_JSON,
                 'content':
                 json.dumps({
                     'lemon':
                     'Your application middleware '
                     'has wrong num of params',
                 }),
             })
         except Exception as e:
             logger.error(e)
             await channels['reply'].send({
                 'status':
                 500,
                 'headers':
                 MIME_TYPES.APPLICATION_JSON,
                 'content':
                 json.dumps({
                     'lemon': 'INTERNAL ERROR',
                 }),
             })
         else:
             await channels['reply'].send(ctx.res.message)
     # TODO: websocket support
     elif message['channel'] == 'websocket.connect':
         pass
     elif message['channel'] == 'websocket.receive':
         pass
     elif message['channel'] == 'websocket.disconnect':
         pass
Exemplo n.º 3
0
        async def _wrapper(message: dict, channels: dict) -> typing.Any:
            """
            :param message: is an ASGI message.
            :param channels: is a dictionary of
            """
            if message['channel'] == 'http.request':
                # init context
                ctx = Context()
                # prepare request
                ctx.req = await Request.from_asgi_interface(message=message,
                                                            channels=channels)
                middleware_chain = \
                    self.pre_process_middleware_list \
                    + self.middleware_list \
                    + self.post_process_middleware_list

                try:
                    await exec_middleware(ctx=ctx,
                                          middleware_list=middleware_chain)
                except MiddlewareParamsError as e:
                    return await channels['reply'].send({
                        'status':
                        500,
                        'headers':
                        MIME_TYPES.APPLICATION_JSON,
                        'content':
                        json.dumps({
                            'lemon':
                            'Your application middleware '
                            'has wrong num of params',
                        }).encode(),
                    })
                else:
                    return await channels['reply'].send(ctx.res.message)
            # TODO: websocket support
            elif message['channel'] == 'websocket.connect':
                return None
            elif message['channel'] == 'websocket.receive':
                return None
            elif message['channel'] == 'websocket.disconnect':
                return None