Tests: improved handshake for websocket tests.

This commit is contained in:
Andrey Zelenkov
2019-10-23 16:26:06 +03:00
parent f878b6eea5
commit 47436e9be5
2 changed files with 15 additions and 7 deletions

View File

@@ -8,7 +8,7 @@ from unit.applications.websockets import TestApplicationWebsocket
class TestJavaWebsockets(TestApplicationJava): class TestJavaWebsockets(TestApplicationJava):
prerequisites = {'modules': ['java']} prerequisites = {'modules': ['java']}
ws = TestApplicationWebsocket(True) ws = TestApplicationWebsocket()
def setUp(self): def setUp(self):
super().setUp() super().setUp()

View File

@@ -1,3 +1,4 @@
import re
import random import random
import base64 import base64
import struct import struct
@@ -32,11 +33,7 @@ class TestApplicationWebsocket(TestApplicationProto):
def upgrade(self): def upgrade(self):
key = self.key() key = self.key()
_, sock = self.get(
if self.preinit:
self.get()
resp, sock = self.get(
headers={ headers={
'Host': 'localhost', 'Host': 'localhost',
'Upgrade': 'websocket', 'Upgrade': 'websocket',
@@ -45,10 +42,21 @@ class TestApplicationWebsocket(TestApplicationProto):
'Sec-WebSocket-Protocol': 'chat', 'Sec-WebSocket-Protocol': 'chat',
'Sec-WebSocket-Version': 13, 'Sec-WebSocket-Version': 13,
}, },
read_timeout=1, no_recv=True,
start=True, start=True,
) )
resp = ''
while select.select([sock], [], [], 30)[0]:
resp += sock.recv(4096).decode()
if (
re.search('101 Switching Protocols', resp)
and resp[-4:] == '\r\n\r\n'
):
resp = self._resp_to_dict(resp)
break
return (resp, sock, key) return (resp, sock, key)
def apply_mask(self, data, mask): def apply_mask(self, data, mask):