Skip to content

Commit cd749e4

Browse files
authored
Cors preflight support (#139)
* add cors preflight support for options method * add tests for cors preflight feature * fix flake8 for test_pass_preflight_options_request
1 parent 0c7deb6 commit cd749e4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

aiohttp_jwt/middleware.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from functools import partial
44

55
import jwt
6-
from aiohttp import web
6+
from aiohttp import web, hdrs
77

88
from .utils import check_request, invoke
99

@@ -39,6 +39,9 @@ def JWTMiddleware(
3939

4040
@web.middleware
4141
async def jwt_middleware(request, handler):
42+
if request.method == hdrs.METH_OPTIONS:
43+
return await handler(request)
44+
4245
if check_request(request, whitelist):
4346
return await handler(request)
4447

tests/test_middleware.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,21 @@ async def handler(request):
244244
token.decode('utf-8')),
245245
})
246246
assert response.status == resp_status
247+
248+
249+
async def test_pass_preflight_options_request(
250+
create_app, aiohttp_client, fake_payload, token):
251+
async def handler(request):
252+
return web.json_response({})
253+
254+
views = (('/foo', handler),)
255+
client = await aiohttp_client(
256+
create_app(views=views),
257+
)
258+
response = await client.options('/foo')
259+
assert response.status == 200
260+
261+
response = await client.options('/foo', headers={
262+
'Authorization': 'Bearer {}'.format(token.decode('utf-8')),
263+
})
264+
assert response.status == 200

0 commit comments

Comments
 (0)