Example #1
0
 def test_parser_eof(self):
     out = aiohttp.DataQueue()
     buf = aiohttp.ParserBuffer()
     p = websocket.WebSocketParser(out, buf)
     next(p)
     self.assertRaises(aiohttp.EofStream, p.throw, aiohttp.EofStream)
     self.assertEqual([], list(out._buffer))
Example #2
0
    def test_parser(self, m_parse_message):
        cur = 0

        def parse_message(buf):
            nonlocal cur
            yield
            if cur == 0:
                cur = 1
                return websocket.Message(websocket.OPCODE_TEXT, b'line1', b'')
            else:
                return websocket.Message(websocket.OPCODE_CLOSE, b'', b'')

        m_parse_message.side_effect = parse_message
        out = aiohttp.FlowControlDataQueue(unittest.mock.Mock())
        buf = aiohttp.ParserBuffer()
        p = websocket.WebSocketParser(out, buf)
        next(p)
        p.send(b'')
        self.assertRaises(StopIteration, p.send, b'')

        self.assertEqual(
            (websocket.OPCODE_TEXT, b'line1', b''), out._buffer[0])
        self.assertEqual(
            (websocket.OPCODE_CLOSE, b'', b''), out._buffer[1])
        self.assertTrue(out._eof)
Example #3
0
    def test_close_frame_invalid_2(self):
        self.buf.extend(self.build_close_frame(code=1))
        p = websocket.WebSocketParser(self.out, self.buf)
        with self.assertRaises(websocket.WebSocketError) as ctx:
            next(p)

        self.assertEqual(ctx.exception.code, websocket.CLOSE_PROTOCOL_ERROR)
Example #4
0
 def test_simple_text(self):
     self.buf.extend(self.build_frame(b'text', websocket.OPCODE_TEXT))
     p = websocket.WebSocketParser(self.out, self.buf)
     next(p)
     p.send(b'')
     res = self.out._buffer[0]
     self.assertEqual(res, ((websocket.OPCODE_TEXT, 'text', ''), 4))
Example #5
0
    def test_simple_text_unicode_err(self):
        self.buf.extend(
            self.build_frame(b'\xf4\x90\x80\x80', websocket.OPCODE_TEXT))
        p = websocket.WebSocketParser(self.out, self.buf)
        with self.assertRaises(websocket.WebSocketError) as ctx:
            next(p)

        self.assertEqual(ctx.exception.code, websocket.CLOSE_INVALID_TEXT)
Example #6
0
    def test_close_frame_unicode_err(self):
        self.buf.extend(
            self.build_close_frame(code=1000, message=b'\xf4\x90\x80\x80'))
        p = websocket.WebSocketParser(self.out, self.buf)
        with self.assertRaises(websocket.WebSocketError) as ctx:
            next(p)

        self.assertEqual(ctx.exception.code, websocket.CLOSE_INVALID_TEXT)
Example #7
0
    def test_unknown_frame(self, m_parse_frame):
        def parse_frame(buf):
            yield
            return (1, websocket.OPCODE_CONTINUATION, b'')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        self.assertRaises(websocket.WebSocketError, p.send, b'')
Example #8
0
    def test_simple_binary(self, m_parse_frame):
        def parse_frame(buf):
            yield
            return (1, websocket.OPCODE_BINARY, b'binary')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        res = self.out._buffer[0]
        self.assertEqual(res, ((websocket.OPCODE_BINARY, b'binary', ''), 6))
Example #9
0
    def test_pong_frame(self, m_parse_frame):
        def parse_frame(buf):
            yield
            return (1, websocket.OPCODE_PONG, b'data')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        res = self.out._buffer[0]
        self.assertEqual(res, (websocket.OPCODE_PONG, b'data', ''))
Example #10
0
    def test_close_frame(self, m_parse_frame):
        def parse_frame(buf):
            yield
            return (1, websocket.OPCODE_CLOSE, b'')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        res = self.out._buffer[0]
        self.assertEqual(res, ((websocket.OPCODE_CLOSE, 0, ''), 0))
Example #11
0
    def test_close_frame_info(self, m_parse_frame):
        def parse_frame(buf):
            yield
            return (1, websocket.OPCODE_CLOSE, b'0112345')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        res = self.out._buffer[0]
        self.assertEqual(res, Message(websocket.OPCODE_CLOSE, 12337, '12345'))
Example #12
0
    def test_close_frame_invalid(self, m_parse_frame):
        def parse_frame(buf):
            yield
            return (1, websocket.OPCODE_CLOSE, b'1')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        with self.assertRaises(websocket.WebSocketError) as ctx:
            next(p)

        self.assertEqual(ctx.exception.code, websocket.CLOSE_PROTOCOL_ERROR)
Example #13
0
    def test_continuation_with_close_bad_payload(self, m_parse_frame):
        frames = [(0, websocket.OPCODE_TEXT, b'line1'),
                  (0, websocket.OPCODE_CLOSE, b'1'),
                  (1, websocket.OPCODE_CONTINUATION, b'line2')]

        def parse_frame(buf, cont=False):
            yield
            return frames.pop(0)

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        with self.assertRaises(websocket.WebSocketError) as ctx:
            p.send(b'')

        self.assertEqual(ctx.exception.code, websocket.CLOSE_PROTOCOL_ERROR)
Example #14
0
    def test_continuation_err(self, m_parse_frame):
        cur = 0

        def parse_frame(buf, cont=False):
            nonlocal cur
            yield
            if cur == 0:
                cur = 1
                return (0, websocket.OPCODE_TEXT, b'line1')
            else:
                return (1, websocket.OPCODE_TEXT, b'line2')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        self.assertRaises(websocket.WebSocketError, p.send, b'')
Example #15
0
    def test_continuation(self, m_parse_frame):
        cur = 0

        def parse_frame(buf, cont=False):
            nonlocal cur
            yield
            if cur == 0:
                cur = 1
                return (0, websocket.OPCODE_TEXT, b'line1')
            else:
                return (1, websocket.OPCODE_CONTINUATION, b'line2')

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        p.send(b'')
        res = self.out._buffer[0]
        self.assertEqual(res, Message(websocket.OPCODE_TEXT, 'line1line2', ''))
Example #16
0
    def test_continuation_with_close_unicode_err(self, m_parse_frame):
        frames = [(0, websocket.OPCODE_TEXT, b'line1'),
                  (0, websocket.OPCODE_CLOSE,
                   self.build_close_frame(1000,
                                          b'\xf4\x90\x80\x80',
                                          noheader=True)),
                  (1, websocket.OPCODE_CONTINUATION, b'line2')]

        def parse_frame(buf, cont=False):
            yield
            return frames.pop(0)

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        with self.assertRaises(websocket.WebSocketError) as ctx:
            p.send(b'')

        self.assertEqual(ctx.exception.code, websocket.CLOSE_INVALID_TEXT)
Example #17
0
    def test_continuation_with_close_empty(self, m_parse_frame):
        frames = [
            (0, websocket.OPCODE_TEXT, b'line1'),
            (0, websocket.OPCODE_CLOSE, b''),
            (1, websocket.OPCODE_CONTINUATION, b'line2'),
        ]

        def parse_frame(buf, cont=False):
            yield
            return frames.pop(0)

        m_parse_frame.side_effect = parse_frame
        p = websocket.WebSocketParser(self.out, self.buf)
        next(p)
        p.send(b'')
        p.send(b'')
        p.send(b'')
        res = self.out._buffer[0]
        self.assertEqual(res, Message(websocket.OPCODE_CLOSE, 0, ''))
        res = self.out._buffer[1]
        self.assertEqual(res, Message(websocket.OPCODE_TEXT, 'line1line2', ''))
Example #18
0
def parser(buf, out):
    return websocket.WebSocketParser(out, buf)