class WebSocketsProtocolWrapperTest(MAASTestCase): """ Tests for L{WebSocketsProtocolWrapper}. """ def setUp(self): super(WebSocketsProtocolWrapperTest, self).setUp() self.accumulatingProtocol = AccumulatingProtocol() self.protocol = WebSocketsProtocolWrapper(self.accumulatingProtocol) self.transport = StringTransportWithDisconnection() self.protocol.makeConnection(self.transport) self.transport.protocol = self.protocol def test_dataReceived(self): """ L{WebSocketsProtocolWrapper.dataReceived} forwards frame content to the underlying protocol. """ self.protocol.dataReceived( _makeFrame(b"Hello", CONTROLS.TEXT, True, mask=b"abcd")) self.assertEqual(b"Hello", self.accumulatingProtocol.data) def test_controlFrames(self): """ L{WebSocketsProtocolWrapper} doesn't forward data from control frames to the underlying protocol. """ self.protocol.dataReceived( _makeFrame(b"Hello", CONTROLS.PING, True, mask=b"abcd")) self.protocol.dataReceived( _makeFrame(b"Hello", CONTROLS.PONG, True, mask=b"abcd")) self.protocol.dataReceived( _makeFrame(b"", CONTROLS.CLOSE, True, mask=b"abcd")) self.assertEqual(b"", self.accumulatingProtocol.data) def test_loseConnection(self): """ L{WebSocketsProtocolWrapper.loseConnection} sends a close frame and disconnects the transport. """ self.protocol.loseConnection() self.assertFalse(self.transport.connected) self.assertEqual(b"\x88\x02\x03\xe8", self.transport.value()) def test_write(self): """ L{WebSocketsProtocolWrapper.write} creates and writes a frame from the payload passed. """ self.accumulatingProtocol.transport.write(b"Hello") self.assertEqual(b"\x81\x05Hello", self.transport.value()) def test_writeSequence(self): """ L{WebSocketsProtocolWrapper.writeSequence} writes a frame for every chunk passed. """ self.accumulatingProtocol.transport.writeSequence([b"Hello", b"World"]) self.assertEqual(b"\x81\x05Hello\x81\x05World", self.transport.value()) def test_getHost(self): """ L{WebSocketsProtocolWrapper.getHost} returns the transport C{getHost}. """ self.assertEqual(self.transport.getHost(), self.accumulatingProtocol.transport.getHost()) def test_getPeer(self): """ L{WebSocketsProtocolWrapper.getPeer} returns the transport C{getPeer}. """ self.assertEqual(self.transport.getPeer(), self.accumulatingProtocol.transport.getPeer()) def test_connectionLost(self): """ L{WebSocketsProtocolWrapper.connectionLost} forwards the connection lost call to the underlying protocol. """ self.transport.loseConnection() self.assertTrue(self.accumulatingProtocol.closed)
class WebSocketsProtocolWrapperTest(TestCase): """ Tests for L{WebSocketsProtocolWrapper}. """ def setUp(self): self.accumulatingProtocol = AccumulatingProtocol() self.protocol = WebSocketsProtocolWrapper(self.accumulatingProtocol) self.transport = StringTransportWithDisconnection() self.protocol.makeConnection(self.transport) self.transport.protocol = self.protocol def test_dataReceived(self): """ L{WebSocketsProtocolWrapper.dataReceived} forwards frame content to the underlying protocol. """ self.protocol.dataReceived( _makeFrame("Hello", CONTROLS.TEXT, True, mask="abcd")) self.assertEqual("Hello", self.accumulatingProtocol.data) def test_controlFrames(self): """ L{WebSocketsProtocolWrapper} doesn't forward data from control frames to the underlying protocol. """ self.protocol.dataReceived( _makeFrame("Hello", CONTROLS.PING, True, mask="abcd")) self.protocol.dataReceived( _makeFrame("Hello", CONTROLS.PONG, True, mask="abcd")) self.protocol.dataReceived( _makeFrame("", CONTROLS.CLOSE, True, mask="abcd")) self.assertEqual("", self.accumulatingProtocol.data) def test_loseConnection(self): """ L{WebSocketsProtocolWrapper.loseConnection} sends a close frame and disconnects the transport. """ self.protocol.loseConnection() self.assertFalse(self.transport.connected) self.assertEqual("\x88\x02\x03\xe8", self.transport.value()) def test_write(self): """ L{WebSocketsProtocolWrapper.write} creates and writes a frame from the payload passed. """ self.accumulatingProtocol.transport.write("Hello") self.assertEqual("\x81\x05Hello", self.transport.value()) def test_writeSequence(self): """ L{WebSocketsProtocolWrapper.writeSequence} writes a frame for every chunk passed. """ self.accumulatingProtocol.transport.writeSequence(["Hello", "World"]) self.assertEqual("\x81\x05Hello\x81\x05World", self.transport.value()) def test_getHost(self): """ L{WebSocketsProtocolWrapper.getHost} returns the transport C{getHost}. """ self.assertEqual(self.transport.getHost(), self.accumulatingProtocol.transport.getHost()) def test_getPeer(self): """ L{WebSocketsProtocolWrapper.getPeer} returns the transport C{getPeer}. """ self.assertEqual(self.transport.getPeer(), self.accumulatingProtocol.transport.getPeer()) def test_connectionLost(self): """ L{WebSocketsProtocolWrapper.connectionLost} forwards the connection lost call to the underlying protocol. """ self.transport.loseConnection() self.assertTrue(self.accumulatingProtocol.closed)