Ejemplo n.º 1
0
    async def test_cors_preflight_request(self):
        async def handle(ctx: Context):
            ctx.body = {
                'ack': 'ok',
            }

        app = Lemon(config={
            'LEMON_CORS_ENABLE': True,
            'LEMON_CORS_ALLOW_METHODS': ['GET', 'POST'],
            'LEMON_CORS_ALLOW_CREDENTIALS': True,
            'LEMON_CORS_ORIGIN_WHITELIST': [
                'http://a.com',
            ],
        }, debug=True)
        app.use(handle)

        req = await self.asgi_request(
            app,
            HTTP_METHODS.OPTIONS, '/',
            headers=[
                [b'origin', b'http://a.com'],
                [b'access-control-request-method', b'POST'],
                [b'access-control-request-headers', b'X-PINGOTHER, Content-Type'],
            ]
        )
        assert req.status_code == 204
        assert req.headers['access-control-allow-origin'] == 'http://a.com'
        assert req.headers['access-control-allow-methods'] == 'GET,POST'
        assert req.headers['access-control-allow-headers'] == 'X-PINGOTHER, Content-Type'
Ejemplo n.º 2
0
    async def test_cors_not_allowed_request(self):
        async def handle(ctx: Context):
            ctx.body = {
                'ack': 'ok',
            }

        app = Lemon(config={
            'LEMON_CORS_ENABLE': True,
            'LEMON_CORS_ALLOW_METHODS': ['GET', 'POST'],
            'LEMON_CORS_ORIGIN_WHITELIST': [
                'http://a.com',
            ],
            'LEMON_CORS_MAX_AGE': 8640,
        }, debug=True)
        app.use(handle)
        req = await self.asgi_request(
            app,
            HTTP_METHODS.OPTIONS, '/',
            headers=[
                [b'origin', b'https://b.com'],
                [b'access-control-request-method', b'POST'],
                [b'access-control-request-headers', b'X-PINGOTHER, Content-Type'],
            ]
        )
        assert 'access-control-allow-origin' not in req.headers
Ejemplo n.º 3
0
    async def test_cors_config(self):
        async def handle(ctx: Context):
            ctx.body = {
                'ack': 'ok',
            }

        app = Lemon(config={
            'LEMON_CORS_ENABLE': True,
            'LEMON_CORS_ALLOW_METHODS': ['GET', 'POST'],
            'LEMON_CORS_ALLOW_HEADERS': ['allow_header'],
            'LEMON_CORS_EXPOSE_HEADERS': ['test_header'],
            'LEMON_CORS_ALLOW_CREDENTIALS': True,
            'LEMON_CORS_ORIGIN_WHITELIST': [
                'http://a.com',
            ],
            'LEMON_CORS_ORIGIN_REGEX_WHITELIST': [
                r'^(https?://)?(\w+\.)?b\.com$',
            ],
            'LEMON_CORS_MAX_AGE': 8640,
        }, debug=True)
        app.use(handle)

        # preflight
        req = await self.asgi_request(
            app,
            HTTP_METHODS.OPTIONS, '/',
            headers=[
                [b'origin', b'http://a.com'],
                [b'access-control-request-method', b'POST'],
                [b'access-control-request-headers', b'X-PINGOTHER, Content-Type'],
            ]
        )
        assert req.headers['access-control-allow-origin'] == 'http://a.com'
        assert req.headers['access-control-allow-methods'] == 'GET,POST'
        assert req.headers['access-control-allow-headers'] == 'allow_header'
        assert req.headers['access-control-allow-credentials'] == 'true'
        assert req.headers['access-control-max-age'] == '8640'

        req = await self.asgi_request(
            app,
            HTTP_METHODS.POST, '/',
            headers=[
                [b'origin', b'http://a.com'],
                [b'x-pingother', b'xxx'],
            ]
        )
        assert req.headers['access-control-allow-origin'] == 'http://a.com'
        assert req.headers['access-control-allow-credentials'] == 'true'
        assert req.headers['access-control-expose-headers'] == 'test_header'
Ejemplo n.º 4
0
    def setup_method(self, method):
        self.app = Lemon(config={
            'LEMON_CORS_ENABLE': True,
        }, debug=True)

        self.message = {
            'channel':
            'http.request',
            'server': (self.address, self.port),
            'client': ('127.0.0.1', 55555),
            'scheme':
            'http',
            'http_version':
            '1.1',
            'headers': [
                [b'user-agent', b'lemon-pytest'],
                [b'accept', b'*/*'],
                [b'host', '{0}:{1}'.format(self.address, self.port).encode()],
                [b'connection', b'keep-alive'],
            ],
        }

        self.channels = {'reply': MockReplyChannel()}
Ejemplo n.º 5
0
from lemon.app import Lemon
from lemon.context import Context


async def middleware(ctx: Context, nxt):
    ctx.body = {
        'msg': 'hello world'
    }
    await nxt()


async def handle(ctx: Context):
    ctx.body['ack'] = 'yeah !'


app = Lemon(debug=True)

app.use(middleware)

app.use(handle)

app.listen()
Ejemplo n.º 6
0
from lemon.app import Lemon
from lemon.context import Context
from lemon.router import SimpleRouter


async def middleware(ctx: Context, nxt):
    ctx.body = {'msg': 'hello lemon'}
    await nxt()


async def handler1(ctx: Context):
    ctx.body['ack'] = 'yeah !'


async def handler2(ctx: Context):
    ctx.body['ack'] = 'yeah !'
    ctx.body['data'] = ctx.req.json


app = Lemon(debug=True)

router = SimpleRouter()
router.get('/handler1', middleware, handler1)
router.post('/handler2', middleware, handler2)

app.use(router.routes())

app.listen()
Ejemplo n.º 7
0
import time

from lemon.app import Lemon
from lemon.context import Context


async def handler(ctx: Context):
    server_recv = int(time.time() * 1000)
    # do something
    server_resp = int(time.time() * 1000)

    ctx.body = {
        'server_recv': server_recv,
        'server_resp': server_resp,
    }


app = Lemon()

app.use(handler)

app.listen(port=9999)
Ejemplo n.º 8
0
from lemon.app import Lemon
from lemon.context import Context


async def handle(ctx: Context):
    ctx.body = {
        'ok': True,
    }


app = Lemon(
    config={
        'LEMON_CORS_ENABLE': True,
    },
    debug=True,
)

app.use(handle)

app.listen()
Ejemplo n.º 9
0
from lemon.app import Lemon
from lemon.context import Context


async def err_middleware(ctx: Context, nxt):
    ctx.body = {
        'msg': 'err_middleware'
    }
    try:
        await nxt()
    except Exception:
        ctx.body = {
            'msg': 'error handled'
        }


async def middleware_exception(ctx: Context):
    raise Exception('error')


app = Lemon(debug=True)

app.use(err_middleware)

app.use(middleware_exception)

app.listen()
Ejemplo n.º 10
0
    content = data['content']

    ret = zh_checker.correct(content=content)
    ret = [r.to_json() for r in ret]
    ctx.body = {
        'data': ret,
    }


app = Lemon(
    config={
        'LEMON_CORS_ENABLE':
        True,
        'LEMON_CORS_ALLOW_CREDENTIALS':
        True,
        'LEMON_CORS_ORIGIN_WHITELIST': [
            'https://zh.sh.mk',
            'http://127.0.0.1:3000',
        ],
    },
    debug=True,
)

router = Router()
router.get('/health', health)
router.post('/api/v1/zhchecker/check', checker)
router.post('/api/v1/zhchecker/correct', correct)

app.use(router.routes())

app.listen(host='0.0.0.0')
Ejemplo n.º 11
0
 def create_http_server(self, handlers: list):
     client = HttpClient()
     app = Lemon(debug=False)
     app.use(*handlers)
     client.create_server(app.listen, port=client.port)
     return client