def test_parseUnknownOpcode(self): """ L{_parseFrames} raises a L{_WSException} error when the error uses an unknown opcode. """ frame = [b"\x8f\x05"] error = self.assertRaises(_WSException, list, _parseFrames(frame)) self.assertEqual("Unknown opcode 15 in frame", str(error))
def test_parsePartialNoLength(self): """ Partial frames are stored for later decoding. """ frame = [b"\x81"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 0) self.assertEqual(frame, [b"\x81"])
def test_parseReservedFlag(self): """ L{_parseFrames} raises a L{_WSException} error when the header uses a reserved flag. """ frame = [b"\x72\x05"] error = self.assertRaises(_WSException, list, _parseFrames(frame)) self.assertEqual("Reserved flag in frame (114)", str(error))
def test_parsePartialTruncatedData(self): """ Partial frames with full headers and partial data are stored for later decoding. """ frame = [b"\x81\x05Hel"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 0) self.assertEqual(frame, [b"\x81\x05Hel"])
def test_parsePartialTruncatedLengthDouble(self): """ Partial frames are stored for later decoding, even if they are marked as being extra-long. """ frame = [b"\x81\xff"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 0) self.assertEqual(frame, [b"\x81\xff"])
def test_parsePartialTruncatedLengthInt(self): """ Partial frames are stored for later decoding, even if they are cut on length boundaries. """ frame = [b"\x81\xfe"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 0) self.assertEqual(frame, [b"\x81\xfe"])
def test_parseMaskedPartialText(self): """ L{_parseFrames} stops parsing if a masked frame isn't long enough to contain the length of the text. """ frame = [b"\x81\x827\xfa"] frames = list(_parseFrames(frame)) self.assertEqual(len(frames), 0) self.assertEqual(frame, [b"\x81\x827\xfa"])
def test_parseMaskedText(self): """ A sample masked frame of "Hello" from HyBi-10, 4.7. """ frame = [b"\x81\x857\xfa!=\x7f\x9fMQX"] frames = list(_parseFrames(frame)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.TEXT, b"Hello", True)) self.assertEqual(frame, [])
def test_parseUnmaskedHugeText(self): """ L{_parseFrames} handles frame with text longer than 64 kB. """ frame = [b"\x81\x7f\x00\x00\x00\x00\x00\x01\x86\xa0", b"x" * 100000] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.TEXT, b"x" * 100000, True)) self.assertEqual(frame, [])
def test_parseUnmaskedTextWithMaskNeeded(self): """ L{_parseFrames} raises L{_WSException} if the frame is not masked and C{needMask} is set to C{True}. """ frame = [b"\x81\x05Hello"] error = self.assertRaises(_WSException, list, _parseFrames(frame, needMask=True)) self.assertEqual("Received data not masked", str(error))
def test_parseUnmaskedLargeText(self): """ L{_parseFrames} handles frame with text longer than 125 bytes. """ frame = [b"\x81\x7e\x00\xc8", b"x" * 200] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.TEXT, b"x" * 200, True)) self.assertEqual(frame, [])
def test_parseUnmaskedText(self): """ A sample unmasked frame of "Hello" from HyBi-10, 4.7. """ frame = [b"\x81\x05Hello"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.TEXT, b"Hello", True)) self.assertEqual(frame, [])
def test_parsePong(self): """ Pong packets are decoded. From HyBi-10, 4.7. """ frame = [b"\x8a\x05Hello"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.PONG, b"Hello", True)) self.assertEqual(frame, [])
def test_parseCloseReason(self): """ A HyBi-07 close packet must have its first two bytes be a numeric error code, and may optionally include trailing text explaining why the connection was closed. """ frame = [b"\x88\x0b\x03\xe8No reason"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.CLOSE, (STATUSES.NORMAL, b"No reason"), True)) self.assertEqual(frame, [])
def test_parseCloseEmpty(self): """ A HyBi-07 close packet may have no body. In that case, it decodes with the generic error code 1000, and has no particular justification or error message. """ frame = [b"\x88\x00"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 1) self.assertEqual(frames[0], (CONTROLS.CLOSE, (STATUSES.NONE, b""), True)) self.assertEqual(frame, [])
def test_parseUnmaskedTextFragments(self): """ Fragmented masked packets are handled. From HyBi-10, 4.7. """ frame = [b"\x01\x03Hel\x80\x02lo"] frames = list(_parseFrames(frame, needMask=False)) self.assertEqual(len(frames), 2) self.assertEqual(frames[0], (CONTROLS.TEXT, b"Hel", False)) self.assertEqual(frames[1], (CONTROLS.CONTINUE, b"lo", True)) self.assertEqual(frame, [])