self.assertEqual(ws.close_code, 1001)
self.assertEqual(ws.close_reason, "goodbye")
# The on_close callback is called no matter which side closed.
- yield self.close_future
+ code, reason = yield self.close_future
+ # The client echoed the close code it received to the server,
+ # so the server's close code (returned via close_future) is
+ # the same.
+ self.assertEqual(code, 1001)
@gen_test
def test_client_close_reason(self):
self.handler.close_code = struct.unpack('>H', data[:2])[0]
if len(data) > 2:
self.handler.close_reason = to_unicode(data[2:])
- self.close()
+ # Echo the received close code, if any (RFC 6455 section 5.5.1).
+ self.close(self.handler.close_code)
elif opcode == 0x9:
# Ping
self._write_frame(True, 0xA, data)
self.read_queue = collections.deque()
self.key = base64.b64encode(os.urandom(16))
self._on_message_callback = on_message_callback
+ self.close_code = self.close_reason = None
scheme, sep, rest = request.url.partition(':')
scheme = {'ws': 'http', 'wss': 'https'}[scheme]