test_websocket.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. # -*- coding: utf-8 -*-
  2. #
  3. """
  4. """
  5. """
  6. websocket - WebSocket client library for Python
  7. Copyright (C) 2010 Hiroki Ohtani(liris)
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. """
  20. import sys
  21. sys.path[0:0] = [""]
  22. import os
  23. import os.path
  24. import socket
  25. import six
  26. # websocket-client
  27. import websocket as ws
  28. from websocket._handshake import _create_sec_websocket_key, \
  29. _validate as _validate_header
  30. from websocket._http import read_headers
  31. from websocket._utils import validate_utf8
  32. if six.PY3:
  33. from base64 import decodebytes as base64decode
  34. else:
  35. from base64 import decodestring as base64decode
  36. if sys.version_info[0] == 2 and sys.version_info[1] < 7:
  37. import unittest2 as unittest
  38. else:
  39. import unittest
  40. try:
  41. from ssl import SSLError
  42. except ImportError:
  43. # dummy class of SSLError for ssl none-support environment.
  44. class SSLError(Exception):
  45. pass
  46. # Skip test to access the internet.
  47. TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1'
  48. TRACEABLE = True
  49. def create_mask_key(_):
  50. return "abcd"
  51. class SockMock(object):
  52. def __init__(self):
  53. self.data = []
  54. self.sent = []
  55. def add_packet(self, data):
  56. self.data.append(data)
  57. def gettimeout(self):
  58. return None
  59. def recv(self, bufsize):
  60. if self.data:
  61. e = self.data.pop(0)
  62. if isinstance(e, Exception):
  63. raise e
  64. if len(e) > bufsize:
  65. self.data.insert(0, e[bufsize:])
  66. return e[:bufsize]
  67. def send(self, data):
  68. self.sent.append(data)
  69. return len(data)
  70. def close(self):
  71. pass
  72. class HeaderSockMock(SockMock):
  73. def __init__(self, fname):
  74. SockMock.__init__(self)
  75. import yatest.common
  76. path = yatest.common.source_path(os.path.join('contrib/python/websocket-client/py2/websocket/tests', fname))
  77. with open(path, "rb") as f:
  78. self.add_packet(f.read())
  79. class WebSocketTest(unittest.TestCase):
  80. def setUp(self):
  81. ws.enableTrace(TRACEABLE)
  82. def tearDown(self):
  83. pass
  84. def testDefaultTimeout(self):
  85. self.assertEqual(ws.getdefaulttimeout(), None)
  86. ws.setdefaulttimeout(10)
  87. self.assertEqual(ws.getdefaulttimeout(), 10)
  88. ws.setdefaulttimeout(None)
  89. def testWSKey(self):
  90. key = _create_sec_websocket_key()
  91. self.assertTrue(key != 24)
  92. self.assertTrue(six.u("¥n") not in key)
  93. def testNonce(self):
  94. """ WebSocket key should be a random 16-byte nonce.
  95. """
  96. key = _create_sec_websocket_key()
  97. nonce = base64decode(key.encode("utf-8"))
  98. self.assertEqual(16, len(nonce))
  99. def testWsUtils(self):
  100. key = "c6b8hTg4EeGb2gQMztV1/g=="
  101. required_header = {
  102. "upgrade": "websocket",
  103. "connection": "upgrade",
  104. "sec-websocket-accept": "Kxep+hNu9n51529fGidYu7a3wO0="}
  105. self.assertEqual(_validate_header(required_header, key, None), (True, None))
  106. header = required_header.copy()
  107. header["upgrade"] = "http"
  108. self.assertEqual(_validate_header(header, key, None), (False, None))
  109. del header["upgrade"]
  110. self.assertEqual(_validate_header(header, key, None), (False, None))
  111. header = required_header.copy()
  112. header["connection"] = "something"
  113. self.assertEqual(_validate_header(header, key, None), (False, None))
  114. del header["connection"]
  115. self.assertEqual(_validate_header(header, key, None), (False, None))
  116. header = required_header.copy()
  117. header["sec-websocket-accept"] = "something"
  118. self.assertEqual(_validate_header(header, key, None), (False, None))
  119. del header["sec-websocket-accept"]
  120. self.assertEqual(_validate_header(header, key, None), (False, None))
  121. header = required_header.copy()
  122. header["sec-websocket-protocol"] = "sub1"
  123. self.assertEqual(_validate_header(header, key, ["sub1", "sub2"]), (True, "sub1"))
  124. self.assertEqual(_validate_header(header, key, ["sub2", "sub3"]), (False, None))
  125. header = required_header.copy()
  126. header["sec-websocket-protocol"] = "sUb1"
  127. self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (True, "sub1"))
  128. header = required_header.copy()
  129. self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (False, None))
  130. def testReadHeader(self):
  131. status, header, status_message = read_headers(HeaderSockMock("data/header01.txt"))
  132. self.assertEqual(status, 101)
  133. self.assertEqual(header["connection"], "Upgrade")
  134. status, header, status_message = read_headers(HeaderSockMock("data/header03.txt"))
  135. self.assertEqual(status, 101)
  136. self.assertEqual(header["connection"], "Upgrade, Keep-Alive")
  137. HeaderSockMock("data/header02.txt")
  138. self.assertRaises(ws.WebSocketException, read_headers, HeaderSockMock("data/header02.txt"))
  139. def testSend(self):
  140. # TODO: add longer frame data
  141. sock = ws.WebSocket()
  142. sock.set_mask_key(create_mask_key)
  143. s = sock.sock = HeaderSockMock("data/header01.txt")
  144. sock.send("Hello")
  145. self.assertEqual(s.sent[0], six.b("\x81\x85abcd)\x07\x0f\x08\x0e"))
  146. sock.send("こんにちは")
  147. self.assertEqual(s.sent[1], six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc"))
  148. sock.send(u"こんにちは")
  149. self.assertEqual(s.sent[1], six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc"))
  150. # sock.send("x" * 5000)
  151. # self.assertEqual(s.sent[1], six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc"))
  152. self.assertEqual(sock.send_binary(b'1111111111101'), 19)
  153. def testRecv(self):
  154. # TODO: add longer frame data
  155. sock = ws.WebSocket()
  156. s = sock.sock = SockMock()
  157. something = six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc")
  158. s.add_packet(something)
  159. data = sock.recv()
  160. self.assertEqual(data, "こんにちは")
  161. s.add_packet(six.b("\x81\x85abcd)\x07\x0f\x08\x0e"))
  162. data = sock.recv()
  163. self.assertEqual(data, "Hello")
  164. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  165. def testIter(self):
  166. count = 2
  167. for _ in ws.create_connection('wss://stream.meetup.com/2/rsvps'):
  168. count -= 1
  169. if count == 0:
  170. break
  171. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  172. def testNext(self):
  173. sock = ws.create_connection('wss://stream.meetup.com/2/rsvps')
  174. self.assertEqual(str, type(next(sock)))
  175. def testInternalRecvStrict(self):
  176. sock = ws.WebSocket()
  177. s = sock.sock = SockMock()
  178. s.add_packet(six.b("foo"))
  179. s.add_packet(socket.timeout())
  180. s.add_packet(six.b("bar"))
  181. # s.add_packet(SSLError("The read operation timed out"))
  182. s.add_packet(six.b("baz"))
  183. with self.assertRaises(ws.WebSocketTimeoutException):
  184. sock.frame_buffer.recv_strict(9)
  185. # if six.PY2:
  186. # with self.assertRaises(ws.WebSocketTimeoutException):
  187. # data = sock._recv_strict(9)
  188. # else:
  189. # with self.assertRaises(SSLError):
  190. # data = sock._recv_strict(9)
  191. data = sock.frame_buffer.recv_strict(9)
  192. self.assertEqual(data, six.b("foobarbaz"))
  193. with self.assertRaises(ws.WebSocketConnectionClosedException):
  194. sock.frame_buffer.recv_strict(1)
  195. def testRecvTimeout(self):
  196. sock = ws.WebSocket()
  197. s = sock.sock = SockMock()
  198. s.add_packet(six.b("\x81"))
  199. s.add_packet(socket.timeout())
  200. s.add_packet(six.b("\x8dabcd\x29\x07\x0f\x08\x0e"))
  201. s.add_packet(socket.timeout())
  202. s.add_packet(six.b("\x4e\x43\x33\x0e\x10\x0f\x00\x40"))
  203. with self.assertRaises(ws.WebSocketTimeoutException):
  204. sock.recv()
  205. with self.assertRaises(ws.WebSocketTimeoutException):
  206. sock.recv()
  207. data = sock.recv()
  208. self.assertEqual(data, "Hello, World!")
  209. with self.assertRaises(ws.WebSocketConnectionClosedException):
  210. sock.recv()
  211. def testRecvWithSimpleFragmentation(self):
  212. sock = ws.WebSocket()
  213. s = sock.sock = SockMock()
  214. # OPCODE=TEXT, FIN=0, MSG="Brevity is "
  215. s.add_packet(six.b("\x01\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
  216. # OPCODE=CONT, FIN=1, MSG="the soul of wit"
  217. s.add_packet(six.b("\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17"))
  218. data = sock.recv()
  219. self.assertEqual(data, "Brevity is the soul of wit")
  220. with self.assertRaises(ws.WebSocketConnectionClosedException):
  221. sock.recv()
  222. def testRecvWithFireEventOfFragmentation(self):
  223. sock = ws.WebSocket(fire_cont_frame=True)
  224. s = sock.sock = SockMock()
  225. # OPCODE=TEXT, FIN=0, MSG="Brevity is "
  226. s.add_packet(six.b("\x01\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
  227. # OPCODE=CONT, FIN=0, MSG="Brevity is "
  228. s.add_packet(six.b("\x00\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
  229. # OPCODE=CONT, FIN=1, MSG="the soul of wit"
  230. s.add_packet(six.b("\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17"))
  231. _, data = sock.recv_data()
  232. self.assertEqual(data, six.b("Brevity is "))
  233. _, data = sock.recv_data()
  234. self.assertEqual(data, six.b("Brevity is "))
  235. _, data = sock.recv_data()
  236. self.assertEqual(data, six.b("the soul of wit"))
  237. # OPCODE=CONT, FIN=0, MSG="Brevity is "
  238. s.add_packet(six.b("\x80\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C"))
  239. with self.assertRaises(ws.WebSocketException):
  240. sock.recv_data()
  241. with self.assertRaises(ws.WebSocketConnectionClosedException):
  242. sock.recv()
  243. def testClose(self):
  244. sock = ws.WebSocket()
  245. sock.sock = SockMock()
  246. sock.connected = True
  247. sock.close()
  248. self.assertEqual(sock.connected, False)
  249. sock = ws.WebSocket()
  250. s = sock.sock = SockMock()
  251. sock.connected = True
  252. s.add_packet(six.b('\x88\x80\x17\x98p\x84'))
  253. sock.recv()
  254. self.assertEqual(sock.connected, False)
  255. def testRecvContFragmentation(self):
  256. sock = ws.WebSocket()
  257. s = sock.sock = SockMock()
  258. # OPCODE=CONT, FIN=1, MSG="the soul of wit"
  259. s.add_packet(six.b("\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17"))
  260. self.assertRaises(ws.WebSocketException, sock.recv)
  261. def testRecvWithProlongedFragmentation(self):
  262. sock = ws.WebSocket()
  263. s = sock.sock = SockMock()
  264. # OPCODE=TEXT, FIN=0, MSG="Once more unto the breach, "
  265. s.add_packet(six.b("\x01\x9babcd.\x0c\x00\x01A\x0f\x0c\x16\x04B\x16\n\x15"
  266. "\rC\x10\t\x07C\x06\x13\x07\x02\x07\tNC"))
  267. # OPCODE=CONT, FIN=0, MSG="dear friends, "
  268. s.add_packet(six.b("\x00\x8eabcd\x05\x07\x02\x16A\x04\x11\r\x04\x0c\x07"
  269. "\x17MB"))
  270. # OPCODE=CONT, FIN=1, MSG="once more"
  271. s.add_packet(six.b("\x80\x89abcd\x0e\x0c\x00\x01A\x0f\x0c\x16\x04"))
  272. data = sock.recv()
  273. self.assertEqual(
  274. data,
  275. "Once more unto the breach, dear friends, once more")
  276. with self.assertRaises(ws.WebSocketConnectionClosedException):
  277. sock.recv()
  278. def testRecvWithFragmentationAndControlFrame(self):
  279. sock = ws.WebSocket()
  280. sock.set_mask_key(create_mask_key)
  281. s = sock.sock = SockMock()
  282. # OPCODE=TEXT, FIN=0, MSG="Too much "
  283. s.add_packet(six.b("\x01\x89abcd5\r\x0cD\x0c\x17\x00\x0cA"))
  284. # OPCODE=PING, FIN=1, MSG="Please PONG this"
  285. s.add_packet(six.b("\x89\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17"))
  286. # OPCODE=CONT, FIN=1, MSG="of a good thing"
  287. s.add_packet(six.b("\x80\x8fabcd\x0e\x04C\x05A\x05\x0c\x0b\x05B\x17\x0c"
  288. "\x08\x0c\x04"))
  289. data = sock.recv()
  290. self.assertEqual(data, "Too much of a good thing")
  291. with self.assertRaises(ws.WebSocketConnectionClosedException):
  292. sock.recv()
  293. self.assertEqual(
  294. s.sent[0],
  295. six.b("\x8a\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17"))
  296. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  297. def testWebSocket(self):
  298. s = ws.create_connection("ws://echo.websocket.org/")
  299. self.assertNotEqual(s, None)
  300. s.send("Hello, World")
  301. result = s.recv()
  302. self.assertEqual(result, "Hello, World")
  303. s.send(u"こにゃにゃちは、世界")
  304. result = s.recv()
  305. self.assertEqual(result, "こにゃにゃちは、世界")
  306. self.assertRaises(ValueError, s.send_close, -1, "")
  307. s.close()
  308. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  309. def testPingPong(self):
  310. s = ws.create_connection("ws://echo.websocket.org/")
  311. self.assertNotEqual(s, None)
  312. s.ping("Hello")
  313. s.pong("Hi")
  314. s.close()
  315. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  316. def testSecureWebSocket(self):
  317. import ssl
  318. s = ws.create_connection("wss://api.bitfinex.com/ws/2")
  319. self.assertNotEqual(s, None)
  320. self.assertTrue(isinstance(s.sock, ssl.SSLSocket))
  321. self.assertEqual(s.getstatus(), 101)
  322. self.assertNotEqual(s.getheaders(), None)
  323. s.close()
  324. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  325. def testWebSocketWithCustomHeader(self):
  326. s = ws.create_connection("ws://echo.websocket.org/",
  327. headers={"User-Agent": "PythonWebsocketClient"})
  328. self.assertNotEqual(s, None)
  329. s.send("Hello, World")
  330. result = s.recv()
  331. self.assertEqual(result, "Hello, World")
  332. self.assertRaises(ValueError, s.close, -1, "")
  333. s.close()
  334. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  335. def testAfterClose(self):
  336. s = ws.create_connection("ws://echo.websocket.org/")
  337. self.assertNotEqual(s, None)
  338. s.close()
  339. self.assertRaises(ws.WebSocketConnectionClosedException, s.send, "Hello")
  340. self.assertRaises(ws.WebSocketConnectionClosedException, s.recv)
  341. class SockOptTest(unittest.TestCase):
  342. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  343. def testSockOpt(self):
  344. sockopt = ((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),)
  345. s = ws.create_connection("ws://echo.websocket.org", sockopt=sockopt)
  346. self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0)
  347. s.close()
  348. class UtilsTest(unittest.TestCase):
  349. def testUtf8Validator(self):
  350. state = validate_utf8(six.b('\xf0\x90\x80\x80'))
  351. self.assertEqual(state, True)
  352. state = validate_utf8(six.b('\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\xed\xa0\x80edited'))
  353. self.assertEqual(state, False)
  354. state = validate_utf8(six.b(''))
  355. self.assertEqual(state, True)
  356. if __name__ == "__main__":
  357. unittest.main()