Skip to content

Commit

Permalink
Merge pull request #181 from flying-sheep/websocket-error-response
Browse files Browse the repository at this point in the history
Made websocket subprotocols conform to spec
  • Loading branch information
asvetlov committed Nov 28, 2014
2 parents ec67f8d + 8f881a7 commit b8548c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions aiohttp/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
client_log = logging.getLogger('aiohttp.client')
internal_log = logging.getLogger('aiohttp.internal')
server_log = logging.getLogger('aiohttp.server')
websocket_log = logging.getLogger('aiohttp.websocket')
7 changes: 4 additions & 3 deletions aiohttp/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hashlib
import struct
from aiohttp import errors
from aiohttp.log import websocket_log

# Frame opcodes defined in the spec.
OPCODE_CONTINUATION = 0x0
Expand Down Expand Up @@ -220,9 +221,9 @@ def do_handshake(method, headers, transport, protocols=()):
protocol = proto
break
else:
raise errors.HttpBadRequest(
'Client protocols {!r} don’t overlap server-known ones {!r}'
.format(protocols, req_protocols))
websocket_log.warning( # No overlap found: Return no protocol as per spec
'Client protocols %r don’t overlap server-known ones %r',
protocols, req_protocols)

# check supported version
version = headers.get('SEC-WEBSOCKET-VERSION')
Expand Down
17 changes: 13 additions & 4 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,21 @@ def test_handshake_protocol_agreement(self):

self.assertEqual(protocol, best_proto)

def test_handshake_protocol_unsupported(self):
@unittest.mock.patch('aiohttp.websocket.websocket_log.warning')
def test_handshake_protocol_unsupported(self, m_websocket_warn):
'''Tests if a protocol mismatch handshake warns and returns None'''
warn_called = False
def websocket_warn(msg, *fmts):
nonlocal warn_called
warn_called = True
m_websocket_warn.side_effect = websocket_warn

proto = 'chat'
self.headers.extend(self.gen_ws_headers('test')[0])

self.assertRaises(
errors.HttpBadRequest,
websocket.do_handshake,
_, _, _, _, protocol = websocket.do_handshake(
self.message.method, self.message.headers, self.transport,
protocols=[proto])

self.assertTrue(warn_called, 'protocol mismatch didn’t warn')
self.assertIsNone(protocol)

0 comments on commit b8548c7

Please sign in to comment.