Skip to content

Commit

Permalink
rich more
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Jun 10, 2018
1 parent 3825efc commit 87ab3bd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ omit =
exclude_lines =
pragma: no cover
raise NotImplementedError
uvicorn.run(app, host=host, port=port)
logger.info(f'listen : http://{host}:{port}')
_host = host or settings.LEMON_SERVER_HOST
_port = port or settings.LEMON_SERVER_PORT
serve(self.application, _host, _port)

4 changes: 1 addition & 3 deletions lemon/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ def serve(app: typing.Callable, host: str, port: typing.Union[int, str]) -> None
:param host: eg: "0.0.0.0"
:param port: eg: 9999
"""
logger.info('listen : http://{host}:{port}'.format(
host=host, port=port,
))
logger.info(f'listen : http://{host}:{port}')
uvicorn.run(app, host=host, port=port)
11 changes: 11 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ async def handle(ctx: Context):
ctx.body = {
'ack': 'yeah !',
}

assert ctx.status == 201
assert ctx.req.scheme == 'http'
assert ctx.req.protocol == 'http'
assert ctx.req.host == '127.0.0.1:9999'
Expand Down Expand Up @@ -220,3 +222,12 @@ async def handle(ctx: Context):
req = await self.get('/')
assert req.status == 200
assert req.json()['msg'] == 'xxx'

async def test_exception(self):
async def handle(ctx: Context):
raise Exception('err')

self.app.use(handle)

req = await self.get('/')
assert req.status == 500
40 changes: 40 additions & 0 deletions tests/test_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async def handle(ctx: Context):
}

self.app.use(handle)

# GET
req = await self.asgi_request(
self.app,
Expand Down Expand Up @@ -58,9 +59,23 @@ async def handle(ctx: Context):
'LEMON_CORS_ORIGIN_WHITELIST': [
'http://a.com',
],
'LEMON_CORS_ORIGIN_REGEX_WHITELIST': [
'http://b.com',
],
}, debug=True)
app.use(handle)

# EMPTY OPTIONS
req = await self.asgi_request(
app,
HTTP_METHODS.OPTIONS, '/',
headers=[
[b'origin', b'http://a.com'],
]
)
assert req.status_code == 200
assert req.json()['ack'] == 'ok'

req = await self.asgi_request(
app,
HTTP_METHODS.OPTIONS, '/',
Expand All @@ -75,6 +90,31 @@ async def handle(ctx: Context):
assert req.headers['access-control-allow-methods'] == 'GET,POST'
assert req.headers['access-control-allow-headers'] == 'X-PINGOTHER, Content-Type'

req = await self.asgi_request(
app,
HTTP_METHODS.OPTIONS, '/',
headers=[
[b'origin', b'http://b.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://b.com'
assert req.headers['access-control-allow-methods'] == 'GET,POST'
assert req.headers['access-control-allow-headers'] == 'X-PINGOTHER, Content-Type'

req = await self.asgi_request(
app,
HTTP_METHODS.OPTIONS, '/',
headers=[
[b'origin', b'http://c.com'],
[b'access-control-request-method', b'POST'],
[b'access-control-request-headers', b'X-PINGOTHER, Content-Type'],
]
)
assert req.status_code == 200

async def test_cors_config(self):
async def handle(ctx: Context):
ctx.body = {
Expand Down
1 change: 0 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import requests

from lemon.context import Context
from lemon.server import serve
from tests import BasicHttpTestCase


Expand Down

0 comments on commit 87ab3bd

Please sign in to comment.