11130Szelenkov@nginx.comimport time
21130Szelenkov@nginx.comimport struct
31130Szelenkov@nginx.comimport unittest
41130Szelenkov@nginx.comfrom unit.applications.lang.node import TestApplicationNode
51130Szelenkov@nginx.comfrom unit.applications.websockets import TestApplicationWebsocket
61130Szelenkov@nginx.com
71130Szelenkov@nginx.comclass TestNodeWebsockets(TestApplicationNode):
81130Szelenkov@nginx.com    prerequisites = ['node']
91130Szelenkov@nginx.com
101130Szelenkov@nginx.com    ws = TestApplicationWebsocket()
111130Szelenkov@nginx.com
121133Szelenkov@nginx.com    def setUp(self):
131133Szelenkov@nginx.com        super().setUp()
141133Szelenkov@nginx.com
15*1138Szelenkov@nginx.com        self.assertIn(
16*1138Szelenkov@nginx.com            'success',
17*1138Szelenkov@nginx.com            self.conf(
18*1138Szelenkov@nginx.com                {'http': {'websocket': {'keepalive_interval': 0}}}, 'settings'
19*1138Szelenkov@nginx.com            ),
20*1138Szelenkov@nginx.com            'clear keepalive_interval',
21*1138Szelenkov@nginx.com        )
22*1138Szelenkov@nginx.com
231133Szelenkov@nginx.com        self.skip_alerts.extend(
241133Szelenkov@nginx.com            [
251133Szelenkov@nginx.com                r'last message send failed',
261133Szelenkov@nginx.com                r'socket close\(\d+\) failed',
271133Szelenkov@nginx.com            ]
281133Szelenkov@nginx.com        )
291130Szelenkov@nginx.com
301130Szelenkov@nginx.com    def close_connection(self, sock):
311130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', 'empty sock')
321130Szelenkov@nginx.com
331130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CLOSE, self.ws.serialize_close())
341130Szelenkov@nginx.com
351130Szelenkov@nginx.com        self.check_close(sock)
361130Szelenkov@nginx.com
371130Szelenkov@nginx.com    def check_close(self, sock, code = 1000, no_close = False):
381130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
391130Szelenkov@nginx.com
401130Szelenkov@nginx.com        self.assertEqual(frame['fin'], True, 'close fin')
411130Szelenkov@nginx.com        self.assertEqual(frame['opcode'], self.ws.OP_CLOSE, 'close opcode')
421130Szelenkov@nginx.com        self.assertEqual(frame['code'], code, 'close code')
431130Szelenkov@nginx.com
441130Szelenkov@nginx.com        if not no_close:
451130Szelenkov@nginx.com            sock.close()
461130Szelenkov@nginx.com
471130Szelenkov@nginx.com    def check_frame(self, frame, fin, opcode, payload, decode=True):
481130Szelenkov@nginx.com        if opcode == self.ws.OP_BINARY or not decode:
491130Szelenkov@nginx.com            data = frame['data']
501130Szelenkov@nginx.com        else:
511130Szelenkov@nginx.com            data = frame['data'].decode('utf-8')
521130Szelenkov@nginx.com
531130Szelenkov@nginx.com        self.assertEqual(frame['fin'], fin, 'fin')
541130Szelenkov@nginx.com        self.assertEqual(frame['opcode'], opcode, 'opcode')
551130Szelenkov@nginx.com        self.assertEqual(data, payload, 'payload')
561130Szelenkov@nginx.com
571130Szelenkov@nginx.com    def test_node_websockets_handshake(self):
581130Szelenkov@nginx.com        self.load('websockets/mirror')
591130Szelenkov@nginx.com
601130Szelenkov@nginx.com        resp, sock, key = self.ws.upgrade()
611130Szelenkov@nginx.com        sock.close()
621130Szelenkov@nginx.com
631130Szelenkov@nginx.com        self.assertEqual(resp['status'], 101, 'status')
641130Szelenkov@nginx.com        self.assertEqual(
651130Szelenkov@nginx.com            resp['headers']['Upgrade'], 'websocket', 'upgrade'
661130Szelenkov@nginx.com        )
671130Szelenkov@nginx.com        self.assertEqual(
681130Szelenkov@nginx.com            resp['headers']['Connection'], 'Upgrade', 'connection'
691130Szelenkov@nginx.com        )
701130Szelenkov@nginx.com        self.assertEqual(
711130Szelenkov@nginx.com            resp['headers']['Sec-WebSocket-Accept'], self.ws.accept(key), 'key'
721130Szelenkov@nginx.com        )
731130Szelenkov@nginx.com
741130Szelenkov@nginx.com    def test_node_websockets_mirror(self):
751130Szelenkov@nginx.com        self.load('websockets/mirror')
761130Szelenkov@nginx.com
771130Szelenkov@nginx.com        message = 'blah'
781130Szelenkov@nginx.com
791130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
801130Szelenkov@nginx.com
811130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
821130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
831130Szelenkov@nginx.com
841130Szelenkov@nginx.com        self.assertEqual(
851130Szelenkov@nginx.com            message, frame['data'].decode('utf-8'), 'mirror'
861130Szelenkov@nginx.com        )
871130Szelenkov@nginx.com
881130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
891130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
901130Szelenkov@nginx.com
911130Szelenkov@nginx.com        self.assertEqual(
921130Szelenkov@nginx.com            message, frame['data'].decode('utf-8'), 'mirror 2'
931130Szelenkov@nginx.com        )
941130Szelenkov@nginx.com
951130Szelenkov@nginx.com        sock.close()
961130Szelenkov@nginx.com
971130Szelenkov@nginx.com    def test_node_websockets_no_mask(self):
981130Szelenkov@nginx.com        self.load('websockets/mirror')
991130Szelenkov@nginx.com
1001130Szelenkov@nginx.com        message = 'blah'
1011130Szelenkov@nginx.com
1021130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1031130Szelenkov@nginx.com
1041130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message, mask=False)
1051130Szelenkov@nginx.com
1061130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1071130Szelenkov@nginx.com
1081130Szelenkov@nginx.com        self.assertEqual(frame['opcode'], self.ws.OP_CLOSE, 'no mask opcode')
1091130Szelenkov@nginx.com        self.assertEqual(frame['code'], 1002, 'no mask close code')
1101130Szelenkov@nginx.com
1111130Szelenkov@nginx.com        sock.close()
1121130Szelenkov@nginx.com
1131130Szelenkov@nginx.com    def test_node_websockets_fragmentation(self):
1141130Szelenkov@nginx.com        self.load('websockets/mirror')
1151130Szelenkov@nginx.com
1161130Szelenkov@nginx.com        message = 'blah'
1171130Szelenkov@nginx.com
1181130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1191130Szelenkov@nginx.com
1201130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message, fin=False)
1211130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, ' ', fin=False)
1221130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, message)
1231130Szelenkov@nginx.com
1241130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1251130Szelenkov@nginx.com
1261130Szelenkov@nginx.com        self.assertEqual(
1271130Szelenkov@nginx.com            message + ' ' + message,
1281130Szelenkov@nginx.com            frame['data'].decode('utf-8'),
1291130Szelenkov@nginx.com            'mirror framing',
1301130Szelenkov@nginx.com        )
1311130Szelenkov@nginx.com
1321130Szelenkov@nginx.com        sock.close()
1331130Szelenkov@nginx.com
1341130Szelenkov@nginx.com    def test_node_websockets_frame_fragmentation_invalid(self):
1351130Szelenkov@nginx.com        self.load('websockets/mirror')
1361130Szelenkov@nginx.com
1371130Szelenkov@nginx.com        message = 'blah'
1381130Szelenkov@nginx.com
1391130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1401130Szelenkov@nginx.com
1411130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, message, fin=False)
1421130Szelenkov@nginx.com
1431130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1441130Szelenkov@nginx.com
1451130Szelenkov@nginx.com        frame.pop('data')
1461130Szelenkov@nginx.com        self.assertDictEqual(
1471130Szelenkov@nginx.com            frame,
1481130Szelenkov@nginx.com            {
1491130Szelenkov@nginx.com                'fin': True,
1501130Szelenkov@nginx.com                'rsv1': False,
1511130Szelenkov@nginx.com                'rsv2': False,
1521130Szelenkov@nginx.com                'rsv3': False,
1531130Szelenkov@nginx.com                'opcode': self.ws.OP_CLOSE,
1541130Szelenkov@nginx.com                'mask': 0,
1551130Szelenkov@nginx.com                'code': 1002,
1561130Szelenkov@nginx.com                'reason': 'Fragmented control frame',
1571130Szelenkov@nginx.com            },
1581130Szelenkov@nginx.com            'close frame',
1591130Szelenkov@nginx.com        )
1601130Szelenkov@nginx.com
1611130Szelenkov@nginx.com        sock.close()
1621130Szelenkov@nginx.com
1631130Szelenkov@nginx.com    def test_node_websockets_partial_send(self):
1641130Szelenkov@nginx.com        self.load('websockets/mirror')
1651130Szelenkov@nginx.com
1661130Szelenkov@nginx.com        message = 'blah'
1671130Szelenkov@nginx.com
1681130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1691130Szelenkov@nginx.com
1701130Szelenkov@nginx.com        frame = self.ws.frame_to_send(self.ws.OP_TEXT, message)
1711130Szelenkov@nginx.com        sock.sendall(frame[:1])
1721130Szelenkov@nginx.com        sock.sendall(frame[1:2])
1731130Szelenkov@nginx.com        sock.sendall(frame[2:3])
1741130Szelenkov@nginx.com        sock.sendall(frame[3:])
1751130Szelenkov@nginx.com
1761130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1771130Szelenkov@nginx.com
1781130Szelenkov@nginx.com        self.assertEqual(
1791130Szelenkov@nginx.com            message,
1801130Szelenkov@nginx.com            frame['data'].decode('utf-8'),
1811130Szelenkov@nginx.com            'partial send',
1821130Szelenkov@nginx.com        )
1831130Szelenkov@nginx.com
1841130Szelenkov@nginx.com        sock.close()
1851130Szelenkov@nginx.com
1861130Szelenkov@nginx.com    def test_node_websockets_large(self):
1871130Szelenkov@nginx.com        self.load('websockets/mirror_fragmentation')
1881130Szelenkov@nginx.com
1891130Szelenkov@nginx.com        message = '0123456789' * 3000
1901130Szelenkov@nginx.com
1911130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
1921130Szelenkov@nginx.com
1931130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
1941130Szelenkov@nginx.com
1951130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1961130Szelenkov@nginx.com        data = frame['data'].decode('utf-8')
1971130Szelenkov@nginx.com
1981130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
1991130Szelenkov@nginx.com        data += frame['data'].decode('utf-8')
2001130Szelenkov@nginx.com
2011130Szelenkov@nginx.com        self.assertEqual(message, data, 'large')
2021130Szelenkov@nginx.com
2031130Szelenkov@nginx.com        sock.close()
2041130Szelenkov@nginx.com
2051130Szelenkov@nginx.com    def test_node_websockets_frame_invalid_opcode(self):
2061130Szelenkov@nginx.com        self.load('websockets/mirror')
2071130Szelenkov@nginx.com
2081130Szelenkov@nginx.com        message = 'blah'
2091130Szelenkov@nginx.com
2101130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
2111130Szelenkov@nginx.com
2121130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message, fin=False)
2131130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, message)
2141130Szelenkov@nginx.com
2151130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
2161130Szelenkov@nginx.com
2171130Szelenkov@nginx.com        frame.pop('data')
2181130Szelenkov@nginx.com        frame.pop('reason')
2191130Szelenkov@nginx.com        self.assertDictEqual(
2201130Szelenkov@nginx.com            frame,
2211130Szelenkov@nginx.com            {
2221130Szelenkov@nginx.com                'fin': True,
2231130Szelenkov@nginx.com                'rsv1': False,
2241130Szelenkov@nginx.com                'rsv2': False,
2251130Szelenkov@nginx.com                'rsv3': False,
2261130Szelenkov@nginx.com                'opcode': self.ws.OP_CLOSE,
2271130Szelenkov@nginx.com                'mask': 0,
2281130Szelenkov@nginx.com                'code': 1002,
2291130Szelenkov@nginx.com            },
2301130Szelenkov@nginx.com            'close frame',
2311130Szelenkov@nginx.com        )
2321130Szelenkov@nginx.com
2331130Szelenkov@nginx.com        sock.close()
2341130Szelenkov@nginx.com
2351130Szelenkov@nginx.com    def test_node_websockets_frame_invalid_opcode_2(self):
2361130Szelenkov@nginx.com        self.load('websockets/mirror')
2371130Szelenkov@nginx.com
2381130Szelenkov@nginx.com        message = 'blah'
2391130Szelenkov@nginx.com
2401130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
2411130Szelenkov@nginx.com
2421130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, message)
2431130Szelenkov@nginx.com
2441130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
2451130Szelenkov@nginx.com
2461130Szelenkov@nginx.com        frame.pop('data')
2471130Szelenkov@nginx.com        self.assertDictEqual(
2481130Szelenkov@nginx.com            frame,
2491130Szelenkov@nginx.com            {
2501130Szelenkov@nginx.com                'fin': True,
2511130Szelenkov@nginx.com                'rsv1': False,
2521130Szelenkov@nginx.com                'rsv2': False,
2531130Szelenkov@nginx.com                'rsv3': False,
2541130Szelenkov@nginx.com                'opcode': self.ws.OP_CLOSE,
2551130Szelenkov@nginx.com                'mask': 0,
2561130Szelenkov@nginx.com                'code': 1002,
2571130Szelenkov@nginx.com                'reason': 'Unrecognized opcode 0',
2581130Szelenkov@nginx.com            },
2591130Szelenkov@nginx.com            'close frame',
2601130Szelenkov@nginx.com        )
2611130Szelenkov@nginx.com
2621130Szelenkov@nginx.com        sock.close()
2631130Szelenkov@nginx.com
2641130Szelenkov@nginx.com    def test_node_websockets_two_clients(self):
2651130Szelenkov@nginx.com        self.load('websockets/mirror')
2661130Szelenkov@nginx.com
2671130Szelenkov@nginx.com        message1 = 'blah1'
2681130Szelenkov@nginx.com        message2 = 'blah2'
2691130Szelenkov@nginx.com
2701130Szelenkov@nginx.com        _, sock1, _ = self.ws.upgrade()
2711130Szelenkov@nginx.com        _, sock2, _ = self.ws.upgrade()
2721130Szelenkov@nginx.com
2731130Szelenkov@nginx.com        self.ws.frame_write(sock1, self.ws.OP_TEXT, message1)
2741130Szelenkov@nginx.com        self.ws.frame_write(sock2, self.ws.OP_TEXT, message2)
2751130Szelenkov@nginx.com
2761130Szelenkov@nginx.com        frame1 = self.ws.frame_read(sock1)
2771130Szelenkov@nginx.com        frame2 = self.ws.frame_read(sock2)
2781130Szelenkov@nginx.com
2791130Szelenkov@nginx.com        self.assertEqual(
2801130Szelenkov@nginx.com            message1, frame1['data'].decode('utf-8'), 'client 1'
2811130Szelenkov@nginx.com        )
2821130Szelenkov@nginx.com        self.assertEqual(
2831130Szelenkov@nginx.com            message2, frame2['data'].decode('utf-8'), 'client 2'
2841130Szelenkov@nginx.com        )
2851130Szelenkov@nginx.com
2861130Szelenkov@nginx.com        sock1.close()
2871130Szelenkov@nginx.com        sock2.close()
2881130Szelenkov@nginx.com
2891130Szelenkov@nginx.com    @unittest.skip('not yet')
2901130Szelenkov@nginx.com    def test_node_websockets_handshake_upgrade_absent(self): # FAIL https://tools.ietf.org/html/rfc6455#section-4.2.1
2911130Szelenkov@nginx.com        self.load('websockets/mirror')
2921130Szelenkov@nginx.com
2931130Szelenkov@nginx.com        key = self.ws.key()
2941130Szelenkov@nginx.com        resp = self.get(headers={
2951130Szelenkov@nginx.com            'Host': 'localhost',
2961130Szelenkov@nginx.com            'Connection': 'Upgrade',
2971130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
2981130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
2991130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13,
3001130Szelenkov@nginx.com        }, read_timeout=1)
3011130Szelenkov@nginx.com
3021130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'upgrade absent')
3031130Szelenkov@nginx.com
3041130Szelenkov@nginx.com    def test_node_websockets_handshake_case_insensitive(self):
3051130Szelenkov@nginx.com        self.load('websockets/mirror')
3061130Szelenkov@nginx.com
3071130Szelenkov@nginx.com        key = self.ws.key()
3081130Szelenkov@nginx.com        resp = self.get(headers={
3091130Szelenkov@nginx.com            'Host': 'localhost',
3101130Szelenkov@nginx.com            'Upgrade': 'WEBSOCKET',
3111130Szelenkov@nginx.com            'Connection': 'UPGRADE',
3121130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
3131130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
3141130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13,
3151130Szelenkov@nginx.com        }, read_timeout=1)
3161130Szelenkov@nginx.com
3171130Szelenkov@nginx.com        self.assertEqual(resp['status'], 101, 'status')
3181130Szelenkov@nginx.com
3191130Szelenkov@nginx.com    @unittest.skip('not yet')
3201130Szelenkov@nginx.com    def test_node_websockets_handshake_connection_absent(self): # FAIL
3211130Szelenkov@nginx.com        self.load('websockets/mirror')
3221130Szelenkov@nginx.com
3231130Szelenkov@nginx.com        key = self.ws.key()
3241130Szelenkov@nginx.com        resp = self.get(headers={
3251130Szelenkov@nginx.com            'Host': 'localhost',
3261130Szelenkov@nginx.com            'Upgrade': 'websocket',
3271130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
3281130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
3291130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13,
3301130Szelenkov@nginx.com        }, read_timeout=1)
3311130Szelenkov@nginx.com
3321130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'status')
3331130Szelenkov@nginx.com
3341130Szelenkov@nginx.com    def test_node_websockets_handshake_version_absent(self):
3351130Szelenkov@nginx.com        self.load('websockets/mirror')
3361130Szelenkov@nginx.com
3371130Szelenkov@nginx.com        key = self.ws.key()
3381130Szelenkov@nginx.com        resp = self.get(headers={
3391130Szelenkov@nginx.com            'Host': 'localhost',
3401130Szelenkov@nginx.com            'Upgrade': 'websocket',
3411130Szelenkov@nginx.com            'Connection': 'Upgrade',
3421130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
3431130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat'
3441130Szelenkov@nginx.com        }, read_timeout=1)
3451130Szelenkov@nginx.com
3461130Szelenkov@nginx.com        self.assertEqual(resp['status'], 426, 'status')
3471130Szelenkov@nginx.com
3481130Szelenkov@nginx.com    @unittest.skip('not yet')
3491130Szelenkov@nginx.com    def test_node_websockets_handshake_key_invalid(self):
3501130Szelenkov@nginx.com        self.load('websockets/mirror')
3511130Szelenkov@nginx.com
3521130Szelenkov@nginx.com        resp = self.get(headers={
3531130Szelenkov@nginx.com            'Host': 'localhost',
3541130Szelenkov@nginx.com            'Upgrade': 'websocket',
3551130Szelenkov@nginx.com            'Connection': 'Upgrade',
3561130Szelenkov@nginx.com            'Sec-WebSocket-Key': '!',
3571130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
3581130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13
3591130Szelenkov@nginx.com        }, read_timeout=1)
3601130Szelenkov@nginx.com
3611130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'key length')
3621130Szelenkov@nginx.com
3631130Szelenkov@nginx.com        key = self.ws.key()
3641130Szelenkov@nginx.com        resp = self.get(headers={
3651130Szelenkov@nginx.com            'Host': 'localhost',
3661130Szelenkov@nginx.com            'Upgrade': 'websocket',
3671130Szelenkov@nginx.com            'Connection': 'Upgrade',
3681130Szelenkov@nginx.com            'Sec-WebSocket-Key': [key, key],
3691130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
3701130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13
3711130Szelenkov@nginx.com        }, read_timeout=1)
3721130Szelenkov@nginx.com
3731130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'key double') # FAIL https://tools.ietf.org/html/rfc6455#section-11.3.1
3741130Szelenkov@nginx.com
3751130Szelenkov@nginx.com    def test_node_websockets_handshake_method_invalid(self):
3761130Szelenkov@nginx.com        self.load('websockets/mirror')
3771130Szelenkov@nginx.com
3781130Szelenkov@nginx.com        key = self.ws.key()
3791130Szelenkov@nginx.com        resp = self.post(headers={
3801130Szelenkov@nginx.com            'Host': 'localhost',
3811130Szelenkov@nginx.com            'Upgrade': 'websocket',
3821130Szelenkov@nginx.com            'Connection': 'Upgrade',
3831130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
3841130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
3851130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13
3861130Szelenkov@nginx.com        }, read_timeout=1)
3871130Szelenkov@nginx.com
3881130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'status')
3891130Szelenkov@nginx.com
3901130Szelenkov@nginx.com    def test_node_websockets_handshake_http_10(self):
3911130Szelenkov@nginx.com        self.load('websockets/mirror')
3921130Szelenkov@nginx.com
3931130Szelenkov@nginx.com        key = self.ws.key()
3941130Szelenkov@nginx.com        resp = self.get(headers={
3951130Szelenkov@nginx.com            'Host': 'localhost',
3961130Szelenkov@nginx.com            'Upgrade': 'websocket',
3971130Szelenkov@nginx.com            'Connection': 'Upgrade',
3981130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
3991130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
4001130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13
4011130Szelenkov@nginx.com        }, http_10=True, read_timeout=1)
4021130Szelenkov@nginx.com
4031130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'status')
4041130Szelenkov@nginx.com
4051130Szelenkov@nginx.com    def test_node_websockets_handshake_uri_invalid(self):
4061130Szelenkov@nginx.com        self.load('websockets/mirror')
4071130Szelenkov@nginx.com
4081130Szelenkov@nginx.com        key = self.ws.key()
4091130Szelenkov@nginx.com        resp = self.get(headers={
4101130Szelenkov@nginx.com            'Host': 'localhost',
4111130Szelenkov@nginx.com            'Upgrade': 'websocket',
4121130Szelenkov@nginx.com            'Connection': 'Upgrade',
4131130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
4141130Szelenkov@nginx.com            'Sec-WebSocket-Protocol': 'chat',
4151130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13
4161130Szelenkov@nginx.com        }, url='!', read_timeout=1)
4171130Szelenkov@nginx.com
4181130Szelenkov@nginx.com        self.assertEqual(resp['status'], 400, 'status')
4191130Szelenkov@nginx.com
4201130Szelenkov@nginx.com    def test_node_websockets_protocol_absent(self):
4211130Szelenkov@nginx.com        self.load('websockets/mirror')
4221130Szelenkov@nginx.com
4231130Szelenkov@nginx.com        key = self.ws.key()
4241130Szelenkov@nginx.com        resp = self.get(headers={
4251130Szelenkov@nginx.com            'Host': 'localhost',
4261130Szelenkov@nginx.com            'Upgrade': 'websocket',
4271130Szelenkov@nginx.com            'Connection': 'Upgrade',
4281130Szelenkov@nginx.com            'Sec-WebSocket-Key': key,
4291130Szelenkov@nginx.com            'Sec-WebSocket-Version': 13
4301130Szelenkov@nginx.com        }, read_timeout=1)
4311130Szelenkov@nginx.com
4321130Szelenkov@nginx.com        self.assertEqual(resp['status'], 101, 'status')
4331130Szelenkov@nginx.com        self.assertEqual(
4341130Szelenkov@nginx.com            resp['headers']['Upgrade'], 'websocket', 'upgrade'
4351130Szelenkov@nginx.com        )
4361130Szelenkov@nginx.com        self.assertEqual(
4371130Szelenkov@nginx.com            resp['headers']['Connection'], 'Upgrade', 'connection'
4381130Szelenkov@nginx.com        )
4391130Szelenkov@nginx.com        self.assertEqual(
4401130Szelenkov@nginx.com            resp['headers']['Sec-WebSocket-Accept'], self.ws.accept(key), 'key'
4411130Szelenkov@nginx.com        )
4421130Szelenkov@nginx.com
4431130Szelenkov@nginx.com    # autobahn-testsuite
4441130Szelenkov@nginx.com
4451130Szelenkov@nginx.com    # Some following tests fail because of Unit does not support UTF-8
4461130Szelenkov@nginx.com    # validation for websocket frames.  It should be implemented
4471130Szelenkov@nginx.com    # by application, if necessary.
4481130Szelenkov@nginx.com
4491133Szelenkov@nginx.com    @unittest.skip('not yet')
4501130Szelenkov@nginx.com    def test_node_websockets_1_1_1__1_1_8(self):
4511130Szelenkov@nginx.com        self.load('websockets/mirror')
4521130Szelenkov@nginx.com
4531130Szelenkov@nginx.com        opcode = self.ws.OP_TEXT
4541130Szelenkov@nginx.com
4551130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4561130Szelenkov@nginx.com
4571130Szelenkov@nginx.com        def check_length(length, chopsize=None):
4581130Szelenkov@nginx.com            payload = '*' * length
4591130Szelenkov@nginx.com
4601130Szelenkov@nginx.com            self.ws.frame_write(sock, opcode, payload, chopsize=chopsize)
4611130Szelenkov@nginx.com
4621130Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
4631130Szelenkov@nginx.com            self.check_frame(frame, True, opcode, payload)
4641130Szelenkov@nginx.com
4651130Szelenkov@nginx.com        check_length(0)                      # 1_1_1
4661130Szelenkov@nginx.com        check_length(125)                    # 1_1_2
4671130Szelenkov@nginx.com        check_length(126)                    # 1_1_3
4681130Szelenkov@nginx.com        check_length(127)                    # 1_1_4
4691130Szelenkov@nginx.com        check_length(128)                    # 1_1_5
4701130Szelenkov@nginx.com        check_length(65535)                  # 1_1_6
4711130Szelenkov@nginx.com        check_length(65536)                  # 1_1_7
4721130Szelenkov@nginx.com        check_length(65536, chopsize = 997)  # 1_1_8
4731130Szelenkov@nginx.com
4741130Szelenkov@nginx.com        self.close_connection(sock)
4751130Szelenkov@nginx.com
4761133Szelenkov@nginx.com    @unittest.skip('not yet')
4771130Szelenkov@nginx.com    def test_node_websockets_1_2_1__1_2_8(self):
4781130Szelenkov@nginx.com        self.load('websockets/mirror')
4791130Szelenkov@nginx.com
4801130Szelenkov@nginx.com        opcode = self.ws.OP_BINARY
4811130Szelenkov@nginx.com
4821130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
4831130Szelenkov@nginx.com
4841130Szelenkov@nginx.com        def check_length(length, chopsize=None):
4851130Szelenkov@nginx.com            payload = b'\xfe' * length
4861130Szelenkov@nginx.com
4871130Szelenkov@nginx.com            self.ws.frame_write(sock, opcode, payload, chopsize=chopsize)
4881130Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
4891130Szelenkov@nginx.com
4901130Szelenkov@nginx.com            self.check_frame(frame, True, opcode, payload)
4911130Szelenkov@nginx.com
4921130Szelenkov@nginx.com        check_length(0)                      # 1_2_1
4931130Szelenkov@nginx.com        check_length(125)                    # 1_2_2
4941130Szelenkov@nginx.com        check_length(126)                    # 1_2_3
4951130Szelenkov@nginx.com        check_length(127)                    # 1_2_4
4961130Szelenkov@nginx.com        check_length(128)                    # 1_2_5
4971130Szelenkov@nginx.com        check_length(65535)                  # 1_2_6
4981130Szelenkov@nginx.com        check_length(65536)                  # 1_2_7
4991130Szelenkov@nginx.com        check_length(65536, chopsize = 997)  # 1_2_8
5001130Szelenkov@nginx.com
5011130Szelenkov@nginx.com        self.close_connection(sock)
5021130Szelenkov@nginx.com
5031130Szelenkov@nginx.com    def test_node_websockets_2_1__2_6(self):
5041130Szelenkov@nginx.com        self.load('websockets/mirror')
5051130Szelenkov@nginx.com
5061130Szelenkov@nginx.com        op_ping = self.ws.OP_PING
5071130Szelenkov@nginx.com        op_pong = self.ws.OP_PONG
5081130Szelenkov@nginx.com
5091130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5101130Szelenkov@nginx.com
5111130Szelenkov@nginx.com        def check_ping(payload, chopsize=None, decode=True):
5121130Szelenkov@nginx.com            self.ws.frame_write(sock, op_ping, payload, chopsize=chopsize)
5131130Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
5141130Szelenkov@nginx.com
5151130Szelenkov@nginx.com            self.check_frame(frame, True, op_pong, payload, decode=decode)
5161130Szelenkov@nginx.com
5171130Szelenkov@nginx.com        check_ping('')                                                 # 2_1
5181130Szelenkov@nginx.com        check_ping('Hello, world!')                                    # 2_2
5191130Szelenkov@nginx.com        check_ping(b'\x00\xff\xfe\xfd\xfc\xfb\x00\xff', decode=False)  # 2_3
5201130Szelenkov@nginx.com        check_ping(b'\xfe' * 125, decode=False)                        # 2_4
5211130Szelenkov@nginx.com        check_ping(b'\xfe' * 125, chopsize=1, decode=False)            # 2_6
5221130Szelenkov@nginx.com
5231130Szelenkov@nginx.com        self.close_connection(sock)
5241130Szelenkov@nginx.com
5251130Szelenkov@nginx.com        # 2_5
5261130Szelenkov@nginx.com
5271130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5281130Szelenkov@nginx.com
5291130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, b'\xfe' * 126)
5301130Szelenkov@nginx.com        self.check_close(sock, 1002)
5311130Szelenkov@nginx.com
5321130Szelenkov@nginx.com    def test_node_websockets_2_7__2_9(self):
5331130Szelenkov@nginx.com        self.load('websockets/mirror')
5341130Szelenkov@nginx.com
5351130Szelenkov@nginx.com        # 2_7
5361130Szelenkov@nginx.com
5371130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5381130Szelenkov@nginx.com
5391130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, '')
5401130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', '2_7')
5411130Szelenkov@nginx.com
5421130Szelenkov@nginx.com        # 2_8
5431130Szelenkov@nginx.com
5441130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
5451130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', '2_8')
5461130Szelenkov@nginx.com
5471130Szelenkov@nginx.com        # 2_9
5481130Szelenkov@nginx.com
5491130Szelenkov@nginx.com        payload = 'ping payload'
5501130Szelenkov@nginx.com
5511130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, 'unsolicited pong payload')
5521130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, payload)
5531130Szelenkov@nginx.com
5541130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
5551130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, payload)
5561130Szelenkov@nginx.com
5571130Szelenkov@nginx.com        self.close_connection(sock)
5581130Szelenkov@nginx.com
5591130Szelenkov@nginx.com    def test_node_websockets_2_10__2_11(self):
5601130Szelenkov@nginx.com        self.load('websockets/mirror')
5611130Szelenkov@nginx.com
5621130Szelenkov@nginx.com        # 2_10
5631130Szelenkov@nginx.com
5641130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5651130Szelenkov@nginx.com
5661130Szelenkov@nginx.com        for i in range(0, 10):
5671130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_PING, 'payload-%d' % i)
5681130Szelenkov@nginx.com
5691130Szelenkov@nginx.com        for i in range(0, 10):
5701130Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
5711130Szelenkov@nginx.com            self.check_frame(frame, True, self.ws.OP_PONG, 'payload-%d' % i)
5721130Szelenkov@nginx.com
5731130Szelenkov@nginx.com        # 2_11
5741130Szelenkov@nginx.com
5751130Szelenkov@nginx.com        for i in range(0, 10):
5761130Szelenkov@nginx.com            opcode = self.ws.OP_PING
5771130Szelenkov@nginx.com            self.ws.frame_write(sock, opcode, 'payload-%d' % i, chopsize=1)
5781130Szelenkov@nginx.com
5791130Szelenkov@nginx.com        for i in range(0, 10):
5801130Szelenkov@nginx.com            frame = self.ws.frame_read(sock)
5811130Szelenkov@nginx.com            self.check_frame(frame, True, self.ws.OP_PONG, 'payload-%d' % i)
5821130Szelenkov@nginx.com
5831130Szelenkov@nginx.com        self.close_connection(sock)
5841130Szelenkov@nginx.com
5851130Szelenkov@nginx.com    @unittest.skip('not yet')
5861130Szelenkov@nginx.com    def test_node_websockets_3_1__3_7(self):
5871130Szelenkov@nginx.com        self.load('websockets/mirror')
5881130Szelenkov@nginx.com
5891130Szelenkov@nginx.com        payload = 'Hello, world!'
5901130Szelenkov@nginx.com
5911130Szelenkov@nginx.com        # 3_1
5921130Szelenkov@nginx.com
5931130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
5941130Szelenkov@nginx.com
5951130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, rsv1=True)
5961130Szelenkov@nginx.com        self.check_close(sock, 1002)
5971130Szelenkov@nginx.com
5981130Szelenkov@nginx.com        # 3_2
5991130Szelenkov@nginx.com
6001130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6011130Szelenkov@nginx.com
6021130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
6031130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, rsv2=True)
6041130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6051130Szelenkov@nginx.com
6061130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6071130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6081130Szelenkov@nginx.com
6091130Szelenkov@nginx.com        self.check_close(sock, 1002, no_close = True)
6101130Szelenkov@nginx.com
6111130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', 'empty 3_2')
6121130Szelenkov@nginx.com        sock.close()
6131130Szelenkov@nginx.com
6141130Szelenkov@nginx.com        # 3_3
6151130Szelenkov@nginx.com
6161130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6171130Szelenkov@nginx.com
6181130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
6191130Szelenkov@nginx.com
6201130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6211130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6221130Szelenkov@nginx.com
6231130Szelenkov@nginx.com        self.ws.frame_write(
6241130Szelenkov@nginx.com            sock,
6251130Szelenkov@nginx.com            self.ws.OP_TEXT,
6261130Szelenkov@nginx.com            payload,
6271130Szelenkov@nginx.com            rsv1=True,
6281130Szelenkov@nginx.com            rsv2=True,
6291130Szelenkov@nginx.com        )
6301130Szelenkov@nginx.com
6311130Szelenkov@nginx.com        self.check_close(sock, 1002, no_close = True)
6321130Szelenkov@nginx.com
6331130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', 'empty 3_3')
6341130Szelenkov@nginx.com        sock.close()
6351130Szelenkov@nginx.com
6361130Szelenkov@nginx.com        # 3_4
6371130Szelenkov@nginx.com
6381130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6391130Szelenkov@nginx.com
6401130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
6411130Szelenkov@nginx.com        self.ws.frame_write(
6421130Szelenkov@nginx.com            sock,
6431130Szelenkov@nginx.com            self.ws.OP_TEXT,
6441130Szelenkov@nginx.com            payload,
6451130Szelenkov@nginx.com            rsv3=True,
6461130Szelenkov@nginx.com            chopsize=1
6471130Szelenkov@nginx.com        )
6481130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
6491130Szelenkov@nginx.com
6501130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
6511130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
6521130Szelenkov@nginx.com
6531130Szelenkov@nginx.com        self.check_close(sock, 1002, no_close = True)
6541130Szelenkov@nginx.com
6551130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', 'empty 3_4')
6561130Szelenkov@nginx.com        sock.close()
6571130Szelenkov@nginx.com
6581130Szelenkov@nginx.com        # 3_5
6591130Szelenkov@nginx.com
6601130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6611130Szelenkov@nginx.com
6621130Szelenkov@nginx.com        self.ws.frame_write(
6631130Szelenkov@nginx.com            sock,
6641130Szelenkov@nginx.com            self.ws.OP_BINARY,
6651130Szelenkov@nginx.com            b'\x00\xff\xfe\xfd\xfc\xfb\x00\xff',
6661130Szelenkov@nginx.com            rsv1=True,
6671130Szelenkov@nginx.com            rsv3=True,
6681130Szelenkov@nginx.com        )
6691130Szelenkov@nginx.com
6701130Szelenkov@nginx.com        self.check_close(sock, 1002)
6711130Szelenkov@nginx.com
6721130Szelenkov@nginx.com        # 3_6
6731130Szelenkov@nginx.com
6741130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6751130Szelenkov@nginx.com
6761130Szelenkov@nginx.com        self.ws.frame_write(
6771130Szelenkov@nginx.com            sock,
6781130Szelenkov@nginx.com            self.ws.OP_PING,
6791130Szelenkov@nginx.com            payload,
6801130Szelenkov@nginx.com            rsv2=True,
6811130Szelenkov@nginx.com            rsv3=True,
6821130Szelenkov@nginx.com        )
6831130Szelenkov@nginx.com
6841130Szelenkov@nginx.com        self.check_close(sock, 1002)
6851130Szelenkov@nginx.com
6861130Szelenkov@nginx.com        # 3_7
6871130Szelenkov@nginx.com
6881130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
6891130Szelenkov@nginx.com
6901130Szelenkov@nginx.com        self.ws.frame_write(
6911130Szelenkov@nginx.com            sock,
6921130Szelenkov@nginx.com            self.ws.OP_CLOSE,
6931130Szelenkov@nginx.com            payload,
6941130Szelenkov@nginx.com            rsv1=True,
6951130Szelenkov@nginx.com            rsv2=True,
6961130Szelenkov@nginx.com            rsv3=True,
6971130Szelenkov@nginx.com        )
6981130Szelenkov@nginx.com
6991130Szelenkov@nginx.com        self.check_close(sock, 1002)
7001130Szelenkov@nginx.com
7011130Szelenkov@nginx.com    def test_node_websockets_4_1_1__4_2_5(self):
7021130Szelenkov@nginx.com        self.load('websockets/mirror')
7031130Szelenkov@nginx.com
7041130Szelenkov@nginx.com        payload = 'Hello, world!'
7051130Szelenkov@nginx.com
7061130Szelenkov@nginx.com        # 4_1_1
7071130Szelenkov@nginx.com
7081130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7091130Szelenkov@nginx.com
7101130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x03, '')
7111130Szelenkov@nginx.com        self.check_close(sock, 1002)
7121130Szelenkov@nginx.com
7131130Szelenkov@nginx.com        # 4_1_2
7141130Szelenkov@nginx.com
7151130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7161130Szelenkov@nginx.com
7171130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x04, 'reserved opcode payload')
7181130Szelenkov@nginx.com        self.check_close(sock, 1002)
7191130Szelenkov@nginx.com
7201130Szelenkov@nginx.com        # 4_1_3
7211130Szelenkov@nginx.com
7221130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7231130Szelenkov@nginx.com
7241130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
7251130Szelenkov@nginx.com
7261130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7271130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
7281130Szelenkov@nginx.com
7291130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x05, '')
7301130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
7311130Szelenkov@nginx.com
7321130Szelenkov@nginx.com        self.check_close(sock, 1002)
7331130Szelenkov@nginx.com
7341130Szelenkov@nginx.com        # 4_1_4
7351130Szelenkov@nginx.com
7361130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7371130Szelenkov@nginx.com
7381130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
7391130Szelenkov@nginx.com
7401130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7411130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
7421130Szelenkov@nginx.com
7431130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x06, payload)
7441130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
7451130Szelenkov@nginx.com
7461130Szelenkov@nginx.com        self.check_close(sock, 1002)
7471130Szelenkov@nginx.com
7481130Szelenkov@nginx.com        # 4_1_5
7491130Szelenkov@nginx.com
7501130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7511130Szelenkov@nginx.com
7521130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
7531130Szelenkov@nginx.com
7541130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7551130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
7561130Szelenkov@nginx.com
7571130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x07, payload, chopsize=1)
7581130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
7591130Szelenkov@nginx.com
7601130Szelenkov@nginx.com        self.check_close(sock, 1002)
7611130Szelenkov@nginx.com
7621130Szelenkov@nginx.com        # 4_2_1
7631130Szelenkov@nginx.com
7641130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7651130Szelenkov@nginx.com
7661130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0B, '')
7671130Szelenkov@nginx.com        self.check_close(sock, 1002)
7681130Szelenkov@nginx.com
7691130Szelenkov@nginx.com        # 4_2_2
7701130Szelenkov@nginx.com
7711130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7721130Szelenkov@nginx.com
7731130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0C, 'reserved opcode payload')
7741130Szelenkov@nginx.com        self.check_close(sock, 1002)
7751130Szelenkov@nginx.com
7761130Szelenkov@nginx.com        # 4_2_3
7771130Szelenkov@nginx.com
7781130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7791130Szelenkov@nginx.com
7801130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
7811130Szelenkov@nginx.com
7821130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7831130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
7841130Szelenkov@nginx.com
7851130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0D, '')
7861130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
7871130Szelenkov@nginx.com
7881130Szelenkov@nginx.com        self.check_close(sock, 1002)
7891130Szelenkov@nginx.com
7901130Szelenkov@nginx.com        # 4_2_4
7911130Szelenkov@nginx.com
7921130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
7931130Szelenkov@nginx.com
7941130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload)
7951130Szelenkov@nginx.com
7961130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
7971130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
7981130Szelenkov@nginx.com
7991130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0E, payload)
8001130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
8011130Szelenkov@nginx.com
8021130Szelenkov@nginx.com        self.check_close(sock, 1002)
8031130Szelenkov@nginx.com
8041130Szelenkov@nginx.com        # 4_2_5
8051130Szelenkov@nginx.com
8061130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8071130Szelenkov@nginx.com
8081130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, payload, chopsize=1)
8091130Szelenkov@nginx.com
8101130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
8111130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, payload)
8121130Szelenkov@nginx.com
8131130Szelenkov@nginx.com        self.ws.frame_write(sock, 0x0F, payload, chopsize=1)
8141130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, '')
8151130Szelenkov@nginx.com
8161130Szelenkov@nginx.com        self.check_close(sock, 1002)
8171130Szelenkov@nginx.com
8181133Szelenkov@nginx.com    @unittest.skip('not yet')
8191130Szelenkov@nginx.com    def test_node_websockets_5_1__5_20(self):
8201130Szelenkov@nginx.com        self.load('websockets/mirror')
8211130Szelenkov@nginx.com
8221130Szelenkov@nginx.com        # 5_1
8231130Szelenkov@nginx.com
8241130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8251130Szelenkov@nginx.com
8261130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, 'fragment1', fin=False)
8271130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
8281130Szelenkov@nginx.com        self.check_close(sock, 1002)
8291130Szelenkov@nginx.com
8301130Szelenkov@nginx.com        # 5_2
8311130Szelenkov@nginx.com
8321130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8331130Szelenkov@nginx.com
8341130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PONG, 'fragment1', fin=False)
8351130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
8361130Szelenkov@nginx.com        self.check_close(sock, 1002)
8371130Szelenkov@nginx.com
8381130Szelenkov@nginx.com        # 5_3
8391130Szelenkov@nginx.com
8401130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
8411130Szelenkov@nginx.com
8421130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
8431130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
8441130Szelenkov@nginx.com
8451130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
8461130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
8471130Szelenkov@nginx.com
8481130Szelenkov@nginx.com        # 5_4
8491130Szelenkov@nginx.com
8501130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
8511130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', '5_4')
8521130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
8531130Szelenkov@nginx.com
8541130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
8551130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
8561130Szelenkov@nginx.com
8571130Szelenkov@nginx.com        # 5_5
8581130Szelenkov@nginx.com
8591130Szelenkov@nginx.com        self.ws.frame_write(
8601130Szelenkov@nginx.com            sock,
8611130Szelenkov@nginx.com            self.ws.OP_TEXT,
8621130Szelenkov@nginx.com            'fragment1',
8631130Szelenkov@nginx.com            fin=False,
8641130Szelenkov@nginx.com            chopsize=1,
8651130Szelenkov@nginx.com        )
8661130Szelenkov@nginx.com        self.ws.frame_write(
8671130Szelenkov@nginx.com            sock,
8681130Szelenkov@nginx.com            self.ws.OP_CONT,
8691130Szelenkov@nginx.com            'fragment2',
8701130Szelenkov@nginx.com            fin=True,
8711130Szelenkov@nginx.com            chopsize=1,
8721130Szelenkov@nginx.com        )
8731130Szelenkov@nginx.com
8741130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
8751130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
8761130Szelenkov@nginx.com
8771130Szelenkov@nginx.com        # 5_6
8781130Szelenkov@nginx.com
8791130Szelenkov@nginx.com        ping_payload = 'ping payload'
8801130Szelenkov@nginx.com
8811130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
8821130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, ping_payload)
8831130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
8841130Szelenkov@nginx.com
8851130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
8861130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
8871130Szelenkov@nginx.com
8881130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
8891130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
8901130Szelenkov@nginx.com
8911130Szelenkov@nginx.com        # 5_7
8921130Szelenkov@nginx.com
8931130Szelenkov@nginx.com        ping_payload = 'ping payload'
8941130Szelenkov@nginx.com
8951130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
8961130Szelenkov@nginx.com        self.assertEqual(self.recvall(sock, read_timeout=1), b'', '5_7')
8971130Szelenkov@nginx.com
8981130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, ping_payload)
8991130Szelenkov@nginx.com
9001130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9011130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
9021130Szelenkov@nginx.com
9031130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
9041130Szelenkov@nginx.com
9051130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9061130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
9071130Szelenkov@nginx.com
9081130Szelenkov@nginx.com        # 5_8
9091130Szelenkov@nginx.com
9101130Szelenkov@nginx.com        ping_payload = 'ping payload'
9111130Szelenkov@nginx.com
9121130Szelenkov@nginx.com        self.ws.frame_write(
9131130Szelenkov@nginx.com            sock,
9141130Szelenkov@nginx.com            self.ws.OP_TEXT,
9151130Szelenkov@nginx.com            'fragment1',
9161130Szelenkov@nginx.com            fin=False,
9171130Szelenkov@nginx.com            chopsize=1,
9181130Szelenkov@nginx.com        )
9191130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_PING, ping_payload, chopsize=1)
9201130Szelenkov@nginx.com        self.ws.frame_write(
9211130Szelenkov@nginx.com            sock,
9221130Szelenkov@nginx.com            self.ws.OP_CONT,
9231130Szelenkov@nginx.com            'fragment2',
9241130Szelenkov@nginx.com            fin=True,
9251130Szelenkov@nginx.com            chopsize=1,
9261130Szelenkov@nginx.com        )
9271130Szelenkov@nginx.com
9281130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9291130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_PONG, ping_payload)
9301130Szelenkov@nginx.com
9311130Szelenkov@nginx.com        frame = self.ws.frame_read(sock)
9321130Szelenkov@nginx.com        self.check_frame(frame, True, self.ws.OP_TEXT, 'fragment1fragment2')
9331130Szelenkov@nginx.com
9341130Szelenkov@nginx.com        # 5_9
9351130Szelenkov@nginx.com
9361130Szelenkov@nginx.com        self.ws.frame_write(
9371130Szelenkov@nginx.com            sock,
9381130Szelenkov@nginx.com            self.ws.OP_CONT,
9391130Szelenkov@nginx.com            'non-continuation payload',
9401130Szelenkov@nginx.com            fin=True,
9411130Szelenkov@nginx.com        )
9421130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
9431130Szelenkov@nginx.com        self.check_close(sock, 1002)
9441130Szelenkov@nginx.com
9451130Szelenkov@nginx.com        # 5_10
9461130Szelenkov@nginx.com
9471130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9481130Szelenkov@nginx.com
9491130Szelenkov@nginx.com        self.ws.frame_write(
9501130Szelenkov@nginx.com            sock,
9511130Szelenkov@nginx.com            self.ws.OP_CONT,
9521130Szelenkov@nginx.com            'non-continuation payload',
9531130Szelenkov@nginx.com            fin=True,
9541130Szelenkov@nginx.com        )
9551130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
9561130Szelenkov@nginx.com        self.check_close(sock, 1002)
9571130Szelenkov@nginx.com
9581130Szelenkov@nginx.com        # 5_11
9591130Szelenkov@nginx.com
9601130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9611130Szelenkov@nginx.com
9621130Szelenkov@nginx.com        self.ws.frame_write(
9631130Szelenkov@nginx.com            sock,
9641130Szelenkov@nginx.com            self.ws.OP_CONT,
9651130Szelenkov@nginx.com            'non-continuation payload',
9661130Szelenkov@nginx.com            fin=True,
9671130Szelenkov@nginx.com            chopsize=1,
9681130Szelenkov@nginx.com        )
9691130Szelenkov@nginx.com        self.ws.frame_write(
9701130Szelenkov@nginx.com            sock,
9711130Szelenkov@nginx.com            self.ws.OP_TEXT,
9721130Szelenkov@nginx.com            'Hello, world!',
9731130Szelenkov@nginx.com            fin=True,
9741130Szelenkov@nginx.com            chopsize=1,
9751130Szelenkov@nginx.com        )
9761130Szelenkov@nginx.com        self.check_close(sock, 1002)
9771130Szelenkov@nginx.com
9781130Szelenkov@nginx.com        # 5_12
9791130Szelenkov@nginx.com
9801130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9811130Szelenkov@nginx.com
9821130Szelenkov@nginx.com        self.ws.frame_write(
9831130Szelenkov@nginx.com            sock,
9841130Szelenkov@nginx.com            self.ws.OP_CONT,
9851130Szelenkov@nginx.com            'non-continuation payload',
9861130Szelenkov@nginx.com            fin=False,
9871130Szelenkov@nginx.com        )
9881130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
9891130Szelenkov@nginx.com        self.check_close(sock, 1002)
9901130Szelenkov@nginx.com
9911130Szelenkov@nginx.com        # 5_13
9921130Szelenkov@nginx.com
9931130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
9941130Szelenkov@nginx.com
9951130Szelenkov@nginx.com        self.ws.frame_write(
9961130Szelenkov@nginx.com            sock,
9971130Szelenkov@nginx.com            self.ws.OP_CONT,
9981130Szelenkov@nginx.com            'non-continuation payload',
9991130Szelenkov@nginx.com            fin=False,
10001130Szelenkov@nginx.com        )
10011130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'Hello, world!', fin=True)
10021130Szelenkov@nginx.com        self.check_close(sock, 1002)
10031130Szelenkov@nginx.com
10041130Szelenkov@nginx.com        # 5_14
10051130Szelenkov@nginx.com
10061130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
10071130Szelenkov@nginx.com
10081130Szelenkov@nginx.com        self.ws.frame_write(
10091130Szelenkov@nginx.com            sock,
10101130Szelenkov@nginx.com            self.ws.OP_CONT,
10111130Szelenkov@nginx.com            'non-continuation payload',
10121130Szelenkov@nginx.com            fin=False,
10131130Szelenkov@nginx.com            chopsize=1,
10141130Szelenkov@nginx.com        )
10151130Szelenkov@nginx.com        self.ws.frame_write(
10161130Szelenkov@nginx.com            sock,
10171130Szelenkov@nginx.com            self.ws.OP_TEXT,
10181130Szelenkov@nginx.com            'Hello, world!',
10191130Szelenkov@nginx.com            fin=True,
10201130Szelenkov@nginx.com            chopsize=1,
10211130Szelenkov@nginx.com        )
10221130Szelenkov@nginx.com        self.check_close(sock, 1002)
10231130Szelenkov@nginx.com
10241130Szelenkov@nginx.com        # 5_15
10251130Szelenkov@nginx.com
10261130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
10271130Szelenkov@nginx.com
10281130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
10291130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=True)
10301130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=False)
10311130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment4', fin=True)
10321130Szelenkov@nginx.com        self.check_close(sock, 1002)
10331130Szelenkov@nginx.com
10341130Szelenkov@nginx.com        # 5_16
10351130Szelenkov@nginx.com
10361130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
10371130Szelenkov@nginx.com
10381130Szelenkov@nginx.com        for i in range(0, 2):
10391130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment1', fin=False)
10401130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment2', fin=False)
10411130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=True)
10421130Szelenkov@nginx.com        self.check_close(sock, 1002)
10431130Szelenkov@nginx.com
10441130Szelenkov@nginx.com        # 5_17
10451130Szelenkov@nginx.com
10461130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
10471130Szelenkov@nginx.com
10481130Szelenkov@nginx.com        for i in range(0, 2):
10491130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment1', fin=True)
10501130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment2', fin=False)
10511130Szelenkov@nginx.com            self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment3', fin=True)
10521130Szelenkov@nginx.com        self.check_close(sock, 1002)
10531130Szelenkov@nginx.com
10541130Szelenkov@nginx.com        # 5_18
10551130Szelenkov@nginx.com
10561130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
10571130Szelenkov@nginx.com
10581130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
10591130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment2')
10601130Szelenkov@nginx.com        self.check_close(sock, 1002)
10611130Szelenkov@nginx.com
10621130Szelenkov@nginx.com        # 5_19
10631130Szelenkov@nginx.com
10641130Szelenkov@nginx.com        _, sock, _ = self.ws.upgrade()
10651130Szelenkov@nginx.com
10661130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_TEXT, 'fragment1', fin=False)
10671130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.OP_CONT, 'fragment2', fin=False)
10681130Szelenkov@nginx.com        self.ws.frame_write(sock, self.ws.