full coverage of websocket

This commit is contained in:
Brian Warner 2016-05-20 11:08:10 -07:00
parent 399efb374c
commit 0a14901e94
2 changed files with 21 additions and 6 deletions

View File

@ -130,7 +130,7 @@ class WebSocketRendezvous(websocket.WebSocketServerProtocol):
if mtype == "close": if mtype == "close":
return self.handle_close(msg, server_rx) return self.handle_close(msg, server_rx)
raise Error("Unknown type") raise Error("unknown type")
except Error as e: except Error as e:
self.send("error", error=e._explain, orig=msg) self.send("error", error=e._explain, orig=msg)

View File

@ -349,6 +349,10 @@ class WSClient(websocket.WebSocketClientProtocol):
payload = json.dumps(kwargs).encode("utf-8") payload = json.dumps(kwargs).encode("utf-8")
self.sendMessage(payload, False) self.sendMessage(payload, False)
def send_notype(self, **kwargs):
payload = json.dumps(kwargs).encode("utf-8")
self.sendMessage(payload, False)
@inlineCallbacks @inlineCallbacks
def sync(self): def sync(self):
ping = next(self.ping_counter) ping = next(self.ping_counter)
@ -486,6 +490,21 @@ class WebSocketAPI(ServerBase, unittest.TestCase):
self.assertEqual(err[u"type"], u"error") self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"already bound") self.assertEqual(err[u"error"], u"already bound")
c1.send_notype(other="misc") # missing 'type'
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"missing 'type'")
c1.send("___unknown") # unknown type
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"unknown type")
c1.send("ping") # missing 'ping'
err = yield c1.next_non_ack()
self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"ping requires 'ping'")
@inlineCallbacks @inlineCallbacks
def test_list(self): def test_list(self):
c1 = yield self.make_client() c1 = yield self.make_client()
@ -704,12 +723,8 @@ class WebSocketAPI(ServerBase, unittest.TestCase):
m = yield c1.next_non_ack() m = yield c1.next_non_ack()
self.assertEqual(m[u"type"], u"closed") self.assertEqual(m[u"type"], u"closed")
return c1.send(u"close", mood=u"mood") # already closed
print("doing last close")
c1.send(u"close", mood=u"mood") # already closed # XXX not getting through
print("did last close")
err = yield c1.next_non_ack() err = yield c1.next_non_ack()
print("done")
self.assertEqual(err[u"type"], u"error") self.assertEqual(err[u"type"], u"error")
self.assertEqual(err[u"error"], u"must open mailbox before closing") self.assertEqual(err[u"error"], u"must open mailbox before closing")