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_makeMaskedFrame(self): """ L{_makeFrame} can build masked frames. """ frame = b"\x81\x857\xfa!=\x7f\x9fMQX" buf = _makeFrame(b"Hello", CONTROLS.TEXT, True, mask=b"7\xfa!=") self.assertEqual(frame, buf)
def test_makeNonFinFrame(self): """ L{_makeFrame} can build fragmented frames. """ frame = b"\x01\x05Hello" buf = _makeFrame(b"Hello", CONTROLS.TEXT, False) self.assertEqual(frame, buf)
def test_makeHello(self): """ L{_makeFrame} makes valid HyBi-07 packets. """ frame = b"\x81\x05Hello" buf = _makeFrame(b"Hello", CONTROLS.TEXT, True) self.assertEqual(frame, buf)
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_makeHugeFrame(self): """ L{_makeFrame} prefixes the payload by the length on 8 bytes if the payload is more than 64 kB. """ frame = b"\x81\x7f\x00\x00\x00\x00\x00\x01\x86\xa0" + b"x" * 100000 buf = _makeFrame(b"x" * 100000, CONTROLS.TEXT, True) self.assertEqual(frame, buf)
def test_makeLargeFrame(self): """ L{_makeFrame} prefixes the payload by the length on 2 bytes if the payload is more than 125 bytes. """ frame = b"\x81\x7e\x00\xc8" + b"x" * 200 buf = _makeFrame(b"x" * 200, CONTROLS.TEXT, True) self.assertEqual(frame, buf)
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_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_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)