def setUp(self): super(WebSocketsProtocolTest, self).setUp() self.receiver = SavingEchoReceiver() self.protocol = WebSocketsProtocol(self.receiver) self.factory = Factory.forProtocol(lambda: self.protocol) self.transport = StringTransportWithDisconnection() self.protocol.makeConnection(self.transport) self.transport.protocol = self.protocol
class TestWebSocketsProtocol(MAASTestCase): def setUp(self): super().setUp() self.receiver = SavingEchoReceiver() self.protocol = WebSocketsProtocol(self.receiver) self.factory = Factory.forProtocol(lambda: self.protocol) self.transport = StringTransportWithDisconnection() self.protocol.makeConnection(self.transport) self.transport.protocol = self.protocol def test_frameReceived(self): """ L{WebSocketsProtocol.dataReceived} translates bytes into frames, and then write it back encoded into frames. """ self.protocol.dataReceived( _makeFrame(b"Hello", CONTROLS.TEXT, True, mask=b"abcd") ) self.assertEqual(b"\x81\x05Hello", self.transport.value()) self.assertEqual( [(CONTROLS.TEXT, b"Hello", True)], self.receiver.received ) def test_ping(self): """ When a C{PING} frame is received, the frame is resent with a C{PONG}, and the application receiver is notified about it. """ self.protocol.dataReceived( _makeFrame(b"Hello", CONTROLS.PING, True, mask=b"abcd") ) self.assertEqual(b"\x8a\x05Hello", self.transport.value()) self.assertEqual( [(CONTROLS.PING, b"Hello", True)], self.receiver.received ) def test_close(self): """ When a C{CLOSE} frame is received, the protocol closes the connection and logs a message. """ with TwistedLoggerFixture() as logger: self.protocol.dataReceived( _makeFrame(b"", CONTROLS.CLOSE, True, mask=b"abcd") ) self.assertFalse(self.transport.connected) self.assertEqual( ["Closing connection: <STATUSES=NONE>"], logger.messages ) def test_invalidFrame(self): """ If an invalid frame is received, L{WebSocketsProtocol} closes the connection. """ self.protocol.dataReceived(b"\x72\x05") self.assertFalse(self.transport.connected)
def setUp(self): super(WebSocketsResourceTest, self).setUp() class SavingEchoFactory(Factory): def buildProtocol(oself, addr): return self.echoProtocol factory = SavingEchoFactory() self.echoProtocol = WebSocketsProtocol(SavingEchoReceiver()) self.resource = WebSocketsResource(lookupProtocolForFactory(factory))