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())
ws.close()
success = 0
failed = 0
for case in range(1, count+1):
url = SERVER + '/runCase?case={}&agent={}'.format(case, AGENT)
status = websocket.STATUS_NORMAL
try:
ws = websocket.create_connection(url)
while True:
opcode, msg = ws.recv_data()
if opcode == websocket.ABNF.OPCODE_TEXT:
msg.decode("utf-8")
if opcode in (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY):
ws.send(msg, opcode)
success += 1
except UnicodeDecodeError:
# this case is ok.
success += 1
status = websocket.STATUS_PROTOCOL_ERROR
except websocket.WebSocketProtocolException:
status = websocket.STATUS_PROTOCOL_ERROR
except Exception as e:
failed += 1
status = websocket.STATUS_PROTOCOL_ERROR
# status = websocket.STATUS_PROTOCOL_ERROR
print(traceback.format_exc())
finally:
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)
ws = websocket.create_connection(url)

View File

@@ -105,22 +105,22 @@ class ABNF(object):
validate the ABNF frame.
"""
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:
raise WebSocketException("Invalid ping frame.")
raise WebSocketProtocolException("Invalid ping frame.")
if self.opcode == ABNF.OPCODE_CLOSE:
l = len(self.data)
if not l:
return
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:]):
raise WebSocketException("Invalid close frame.")
raise WebSocketProtocolException("Invalid close frame.")
code = 256*six.byte2int(self.data[0]) + six.byte2int(self.data[1])
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):
return code in VALID_CLOSE_STATUS or (3000 <= code <5000)

View File

@@ -698,12 +698,12 @@ class WebSocket(object):
if not frame:
# handle error:
# '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):
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):
raise WebSocketException("Illegal frame")
raise WebSocketProtocolException("Illegal frame")
if self._cont_data:
self._cont_data[1] += frame.data
@@ -720,7 +720,7 @@ class WebSocket(object):
self._cont_data = None
frame.data = data[1]
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]
elif frame.opcode == ABNF.OPCODE_CLOSE:
@@ -730,7 +730,7 @@ class WebSocket(object):
if len(frame.data) < 126:
self.pong(frame.data)
else:
raise WebSocketException("Protocol Error")
raise WebSocketProtocolException("Ping message is too long")
if control_frame:
return (frame.opcode, frame)
elif frame.opcode == ABNF.OPCODE_PONG:

View File

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

View File

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