refs #117 . change exception type.

This commit is contained in:
liris
2014-10-15 15:15:01 +09:00
parent 158663b84f
commit 63c2f5a91a
5 changed files with 26 additions and 22 deletions

View File

@@ -17,31 +17,29 @@ ws = websocket.create_connection(SERVER + "/getCaseCount")
count = json.loads(ws.recv()) count = json.loads(ws.recv())
ws.close() ws.close()
success = 0
failed = 0
for case in range(1, count+1): for case in range(1, count+1):
url = SERVER + '/runCase?case={}&agent={}'.format(case, AGENT) url = SERVER + '/runCase?case={}&agent={}'.format(case, AGENT)
status = websocket.STATUS_NORMAL status = websocket.STATUS_NORMAL
try: try:
ws = websocket.create_connection(url) ws = websocket.create_connection(url)
opcode, msg = ws.recv_data() while True:
if opcode == websocket.ABNF.OPCODE_TEXT: opcode, msg = ws.recv_data()
msg.decode("utf-8") if opcode == websocket.ABNF.OPCODE_TEXT:
if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY): msg.decode("utf-8")
ws.send(msg, opcode) if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
success += 1 ws.send(msg, opcode)
except UnicodeDecodeError: except UnicodeDecodeError:
# this case is ok. # this case is ok.
success += 1 status = websocket.STATUS_PROTOCOL_ERROR
except websocket.WebSocketProtocolException:
status = websocket.STATUS_PROTOCOL_ERROR status = websocket.STATUS_PROTOCOL_ERROR
except Exception as e: except Exception as e:
failed += 1 # status = websocket.STATUS_PROTOCOL_ERROR
status = websocket.STATUS_PROTOCOL_ERROR
print(traceback.format_exc()) print(traceback.format_exc())
finally: finally:
ws.close(status) ws.close(status)
print("Ran {} test cases. success: {}, faield: {}".format(case, success, failed)) print("Ran {} test cases.".format(case))
url = SERVER + '/updateReports?agent={}'.format(AGENT) url = SERVER + '/updateReports?agent={}'.format(AGENT)
ws = websocket.create_connection(url) ws = websocket.create_connection(url)

View File

@@ -105,22 +105,22 @@ class ABNF(object):
validate the ABNF frame. validate the ABNF frame.
""" """
if self.rsv1 or self.rsv2 or self.rsv3: if self.rsv1 or self.rsv2 or self.rsv3:
raise NotImplementedError("rsv is not implemented, yet") raise WebSocketProtocolException("rsv is not implemented, yet")
if self.opcode == ABNF.OPCODE_PING and not self.fin: if self.opcode == ABNF.OPCODE_PING and not self.fin:
raise WebSocketException("Invalid ping frame.") raise WebSocketProtocolException("Invalid ping frame.")
if self.opcode == ABNF.OPCODE_CLOSE: if self.opcode == ABNF.OPCODE_CLOSE:
l = len(self.data) l = len(self.data)
if not l: if not l:
return return
if l == 1 or l >= 126: if l == 1 or l >= 126:
raise WebSocketException("Invalid close frame.") raise WebSocketProtocolException("Invalid close frame.")
if l > 2 and not validate_utf8(self.data[2:]): if l > 2 and not validate_utf8(self.data[2:]):
raise WebSocketException("Invalid close frame.") raise WebSocketProtocolException("Invalid close frame.")
code = 256*six.byte2int(self.data[0]) + six.byte2int(self.data[1]) code = 256*six.byte2int(self.data[0]) + six.byte2int(self.data[1])
if not self._is_valid_close_status(code): if not self._is_valid_close_status(code):
raise WebSocketException("Invalid close opcode.") raise WebSocketProtocolException("Invalid close opcode.")
def _is_valid_close_status(self, code): def _is_valid_close_status(self, code):
return code in VALID_CLOSE_STATUS or (3000 <= code <5000) return code in VALID_CLOSE_STATUS or (3000 <= code <5000)

View File

@@ -698,12 +698,12 @@ class WebSocket(object):
if not frame: if not frame:
# handle error: # handle error:
# 'NoneType' object has no attribute 'opcode' # 'NoneType' object has no attribute 'opcode'
raise WebSocketException("Not a valid frame %s" % frame) raise WebSocketProtocolException("Not a valid frame %s" % frame)
elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT): elif frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY, ABNF.OPCODE_CONT):
if not self._recving_frames and frame.opcode == ABNF.OPCODE_CONT: if not self._recving_frames and frame.opcode == ABNF.OPCODE_CONT:
raise WebSocketException("Illegal frame") raise WebSocketProtocolException("Illegal frame")
if self._recving_frames and frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY): if self._recving_frames and frame.opcode in (ABNF.OPCODE_TEXT, ABNF.OPCODE_BINARY):
raise WebSocketException("Illegal frame") raise WebSocketProtocolException("Illegal frame")
if self._cont_data: if self._cont_data:
self._cont_data[1] += frame.data self._cont_data[1] += frame.data
@@ -720,7 +720,7 @@ class WebSocket(object):
self._cont_data = None self._cont_data = None
frame.data = data[1] frame.data = data[1]
if not self.fire_cont_frame and data[0] == ABNF.OPCODE_TEXT and not validate_utf8(frame.data): if not self.fire_cont_frame and data[0] == ABNF.OPCODE_TEXT and not validate_utf8(frame.data):
raise WebSocketException("cannot decode: " + repr(frame.data)) raise WebSocketProtocolException("cannot decode: " + repr(frame.data))
return [data[0], frame] return [data[0], frame]
elif frame.opcode == ABNF.OPCODE_CLOSE: elif frame.opcode == ABNF.OPCODE_CLOSE:
@@ -730,7 +730,7 @@ class WebSocket(object):
if len(frame.data) < 126: if len(frame.data) < 126:
self.pong(frame.data) self.pong(frame.data)
else: else:
raise WebSocketException("Protocol Error") raise WebSocketProtocolException("Ping message is too long")
if control_frame: if control_frame:
return (frame.opcode, frame) return (frame.opcode, frame)
elif frame.opcode == ABNF.OPCODE_PONG: elif frame.opcode == ABNF.OPCODE_PONG:

View File

@@ -30,6 +30,11 @@ class WebSocketException(Exception):
""" """
pass pass
class WebSocketProtocolException(WebSocketException):
"""
If the webscoket protocol is invalid, this exception will be raised.
"""
pass
class WebSocketConnectionClosedException(WebSocketException): class WebSocketConnectionClosedException(WebSocketException):
""" """

View File

@@ -30,6 +30,7 @@ from websocket._utils import validate_utf8
# Skip test to access the internet. # Skip test to access the internet.
TEST_WITH_INTERNET = False TEST_WITH_INTERNET = False
TEST_WITH_INTERNET = True
# Skip Secure WebSocket test. # Skip Secure WebSocket test.
TEST_SECURE_WS = False TEST_SECURE_WS = False