1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/file_websocket_wsh.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +from mod_pywebsocket import msgutil 1.5 + 1.6 +import time 1.7 +import sys 1.8 +import struct 1.9 + 1.10 +# see the list of tests in test_websocket.html 1.11 + 1.12 +def web_socket_do_extra_handshake(request): 1.13 + # must set request.ws_protocol to the selected version from ws_requested_protocols 1.14 + for x in request.ws_requested_protocols: 1.15 + if x != "test-does-not-exist": 1.16 + request.ws_protocol = x 1.17 + break 1.18 + 1.19 + if request.ws_protocol == "test-2.1": 1.20 + time.sleep(3) 1.21 + elif request.ws_protocol == "test-9": 1.22 + time.sleep(3) 1.23 + elif request.ws_protocol == "test-10": 1.24 + time.sleep(3) 1.25 + elif request.ws_protocol == "test-19": 1.26 + raise ValueError('Aborting (test-19)') 1.27 + elif request.ws_protocol == "test-20" or request.ws_protocol == "test-17": 1.28 + time.sleep(3) 1.29 + elif request.ws_protocol == "test-22": 1.30 + # The timeout is 5 seconds 1.31 + time.sleep(13) 1.32 + elif request.ws_protocol == "test-41b": 1.33 + request.sts = "max-age=100" 1.34 + else: 1.35 + pass 1.36 + 1.37 +# Behave according to recommendation of RFC 6455, section # 5.5.1: 1.38 +# "When sending a Close frame in response, the endpoint typically echos the 1.39 +# status code it received." 1.40 +# - Without this, pywebsocket replies with 1000 to any close code. 1.41 +# 1.42 +# Note that this function is only called when the client initiates the close 1.43 +def web_socket_passive_closing_handshake(request): 1.44 + if request.ws_close_code == 1005: 1.45 + return None, None 1.46 + else: 1.47 + return request.ws_close_code, request.ws_close_reason 1.48 + 1.49 + 1.50 +def web_socket_transfer_data(request): 1.51 + if request.ws_protocol == "test-2.1" or request.ws_protocol == "test-2.2": 1.52 + msgutil.close_connection(request) 1.53 + elif request.ws_protocol == "test-6": 1.54 + resp = "wrong message" 1.55 + if msgutil.receive_message(request) == "1": 1.56 + resp = "2" 1.57 + msgutil.send_message(request, resp.decode('utf-8')) 1.58 + resp = "wrong message" 1.59 + if msgutil.receive_message(request) == "3": 1.60 + resp = "4" 1.61 + msgutil.send_message(request, resp.decode('utf-8')) 1.62 + resp = "wrong message" 1.63 + if msgutil.receive_message(request) == "5": 1.64 + resp = "あいうえお" 1.65 + msgutil.send_message(request, resp.decode('utf-8')) 1.66 + msgutil.close_connection(request) 1.67 + elif request.ws_protocol == "test-7": 1.68 + msgutil.send_message(request, "test-7 data") 1.69 + elif request.ws_protocol == "test-10": 1.70 + msgutil.close_connection(request) 1.71 + elif request.ws_protocol == "test-11": 1.72 + resp = "wrong message" 1.73 + if msgutil.receive_message(request) == "client data": 1.74 + resp = "server data" 1.75 + msgutil.send_message(request, resp.decode('utf-8')) 1.76 + elif request.ws_protocol == "test-12": 1.77 + msg = msgutil.receive_message(request) 1.78 + if msg == u'a\ufffdb': 1.79 + # converted unpaired surrogate in UTF-16 to UTF-8 OK 1.80 + msgutil.send_message(request, "SUCCESS") 1.81 + else: 1.82 + msgutil.send_message(request, "FAIL got '" + msg 1.83 + + "' instead of string with replacement char'") 1.84 + elif request.ws_protocol == "test-13": 1.85 + # first one binary message containing the byte 0x61 ('a') 1.86 + request.connection.write('\xff\x01\x61') 1.87 + # after a bad utf8 message 1.88 + request.connection.write('\x01\x61\xff') 1.89 + msgutil.close_connection(request) 1.90 + elif request.ws_protocol == "test-14": 1.91 + msgutil.close_connection(request) 1.92 + msgutil.send_message(request, "server data") 1.93 + elif request.ws_protocol == "test-15": 1.94 + # DISABLED: close_connection hasn't supported 2nd 'abort' argument for a 1.95 + # long time. Passing extra arg was causing exception, which conveniently 1.96 + # caused abort :) but as of pywebsocket v606 raising an exception here no 1.97 + # longer aborts, and there's no obvious way to close TCP connection w/o 1.98 + # sending websocket CLOSE. 1.99 + raise RuntimeError("test-15 should be disabled for now") 1.100 + #msgutil.close_connection(request, True) # OBSOLETE 2nd arg 1.101 + return 1.102 + elif request.ws_protocol == "test-17" or request.ws_protocol == "test-21": 1.103 + time.sleep(2) 1.104 + resp = "wrong message" 1.105 + if msgutil.receive_message(request) == "client data": 1.106 + resp = "server data" 1.107 + msgutil.send_message(request, resp.decode('utf-8')) 1.108 + time.sleep(2) 1.109 + msgutil.close_connection(request) 1.110 + elif request.ws_protocol == "test-20": 1.111 + msgutil.send_message(request, "server data") 1.112 + msgutil.close_connection(request) 1.113 + elif request.ws_protocol == "test-34": 1.114 + request.ws_stream.close_connection(1001, "going away now") 1.115 + elif request.ws_protocol == "test-35a": 1.116 + while not request.client_terminated: 1.117 + msgutil.receive_message(request) 1.118 + global test35code 1.119 + test35code = request.ws_close_code 1.120 + global test35reason 1.121 + test35reason = request.ws_close_reason 1.122 + elif request.ws_protocol == "test-35b": 1.123 + request.ws_stream.close_connection(test35code + 1, test35reason) 1.124 + elif request.ws_protocol == "test-37b": 1.125 + while not request.client_terminated: 1.126 + msgutil.receive_message(request) 1.127 + global test37code 1.128 + test37code = request.ws_close_code 1.129 + global test37reason 1.130 + test37reason = request.ws_close_reason 1.131 + elif request.ws_protocol == "test-37c": 1.132 + request.ws_stream.close_connection(test37code, test37reason) 1.133 + elif request.ws_protocol == "test-42": 1.134 + # Echo back 3 messages 1.135 + msgutil.send_message(request, 1.136 + msgutil.receive_message(request)) 1.137 + msgutil.send_message(request, 1.138 + msgutil.receive_message(request)) 1.139 + msgutil.send_message(request, 1.140 + msgutil.receive_message(request)) 1.141 + elif request.ws_protocol == "test-44": 1.142 + rcv = msgutil.receive_message(request) 1.143 + # check we received correct binary msg 1.144 + if len(rcv) == 3 \ 1.145 + and ord(rcv[0]) == 5 and ord(rcv[1]) == 0 and ord(rcv[2]) == 7: 1.146 + # reply with binary msg 0x04 1.147 + msgutil.send_message(request, struct.pack("cc", chr(0), chr(4)), True, True) 1.148 + else: 1.149 + msgutil.send_message(request, "incorrect binary msg received!") 1.150 + elif request.ws_protocol == "test-45": 1.151 + rcv = msgutil.receive_message(request) 1.152 + # check we received correct binary msg 1.153 + if rcv == "flob": 1.154 + # send back same blob as binary msg 1.155 + msgutil.send_message(request, rcv, True, True) 1.156 + else: 1.157 + msgutil.send_message(request, "incorrect binary msg received: '" + rcv + "'") 1.158 + elif request.ws_protocol == "test-46": 1.159 + msgutil.send_message(request, "client must drop this if close was called") 1.160 + 1.161 + while not request.client_terminated: 1.162 + msgutil.receive_message(request)