Skip to content

Commit

Permalink
Fix error handling for ASGI WebSocket errors (Fixes #210)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 31, 2021
1 parent e16bbca commit e8e4ba5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion engineio/async_drivers/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def read(self, length=None):

async def make_response(status, headers, payload, environ):
headers = [(h[0].encode('utf-8'), h[1].encode('utf-8')) for h in headers]
if 'HTTP_SEC_WEBSOCKET_VERSION' in environ:
if environ['asgi.scope']['type'] == 'websocket':
if status.startswith('200 '):
await environ['asgi.send']({'type': 'websocket.accept',
'headers': headers})
Expand Down
6 changes: 3 additions & 3 deletions tests/asyncio/test_async_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def test_translate_unknown_request(self):
assert environ == {}

def test_make_response(self):
environ = {'asgi.send': AsyncMock()}
environ = {'asgi.send': AsyncMock(), 'asgi.scope': {'type': 'http'}}
_run(
async_asgi.make_response(
'202 ACCEPTED', [('foo', 'bar')], b'payload', environ
Expand All @@ -451,7 +451,7 @@ def test_make_response(self):
def test_make_response_websocket_accept(self):
environ = {
'asgi.send': AsyncMock(),
'HTTP_SEC_WEBSOCKET_VERSION': 'foo',
'asgi.scope': {'type': 'websocket'},
}
_run(
async_asgi.make_response(
Expand All @@ -465,7 +465,7 @@ def test_make_response_websocket_accept(self):
def test_make_response_websocket_reject(self):
environ = {
'asgi.send': AsyncMock(),
'HTTP_SEC_WEBSOCKET_VERSION': 'foo',
'asgi.scope': {'type': 'websocket'},
}
_run(
async_asgi.make_response(
Expand Down

0 comments on commit e8e4ba5

Please sign in to comment.