test_app.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. #
  3. """
  4. websocket - WebSocket client library for Python
  5. Copyright (C) 2010 Hiroki Ohtani(liris)
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. import os
  19. import os.path
  20. import websocket as ws
  21. import sys
  22. sys.path[0:0] = [""]
  23. try:
  24. import ssl
  25. except ImportError:
  26. HAVE_SSL = False
  27. if sys.version_info[0] == 2 and sys.version_info[1] < 7:
  28. import unittest2 as unittest
  29. else:
  30. import unittest
  31. # Skip test to access the internet.
  32. TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1'
  33. TRACEABLE = True
  34. class WebSocketAppTest(unittest.TestCase):
  35. class NotSetYet(object):
  36. """ A marker class for signalling that a value hasn't been set yet.
  37. """
  38. def setUp(self):
  39. ws.enableTrace(TRACEABLE)
  40. WebSocketAppTest.keep_running_open = WebSocketAppTest.NotSetYet()
  41. WebSocketAppTest.keep_running_close = WebSocketAppTest.NotSetYet()
  42. WebSocketAppTest.get_mask_key_id = WebSocketAppTest.NotSetYet()
  43. def tearDown(self):
  44. WebSocketAppTest.keep_running_open = WebSocketAppTest.NotSetYet()
  45. WebSocketAppTest.keep_running_close = WebSocketAppTest.NotSetYet()
  46. WebSocketAppTest.get_mask_key_id = WebSocketAppTest.NotSetYet()
  47. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  48. def testKeepRunning(self):
  49. """ A WebSocketApp should keep running as long as its self.keep_running
  50. is not False (in the boolean context).
  51. """
  52. def on_open(self, *args, **kwargs):
  53. """ Set the keep_running flag for later inspection and immediately
  54. close the connection.
  55. """
  56. WebSocketAppTest.keep_running_open = self.keep_running
  57. self.close()
  58. def on_close(self, *args, **kwargs):
  59. """ Set the keep_running flag for the test to use.
  60. """
  61. WebSocketAppTest.keep_running_close = self.keep_running
  62. app = ws.WebSocketApp('ws://echo.websocket.org/', on_open=on_open, on_close=on_close)
  63. app.run_forever()
  64. # if numpy is installed, this assertion fail
  65. # self.assertFalse(isinstance(WebSocketAppTest.keep_running_open,
  66. # WebSocketAppTest.NotSetYet))
  67. # self.assertFalse(isinstance(WebSocketAppTest.keep_running_close,
  68. # WebSocketAppTest.NotSetYet))
  69. # self.assertEqual(True, WebSocketAppTest.keep_running_open)
  70. # self.assertEqual(False, WebSocketAppTest.keep_running_close)
  71. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  72. def testSockMaskKey(self):
  73. """ A WebSocketApp should forward the received mask_key function down
  74. to the actual socket.
  75. """
  76. def my_mask_key_func():
  77. pass
  78. def on_open(self, *args, **kwargs):
  79. """ Set the value so the test can use it later on and immediately
  80. close the connection.
  81. """
  82. WebSocketAppTest.get_mask_key_id = id(self.get_mask_key)
  83. self.close()
  84. app = ws.WebSocketApp('ws://echo.websocket.org/', on_open=on_open, get_mask_key=my_mask_key_func)
  85. app.run_forever()
  86. # if numpy is installed, this assertion fail
  87. # Note: We can't use 'is' for comparing the functions directly, need to use 'id'.
  88. # self.assertEqual(WebSocketAppTest.get_mask_key_id, id(my_mask_key_func))
  89. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  90. def testPingInterval(self):
  91. """ A WebSocketApp should ping regularly
  92. """
  93. def on_ping(app, msg):
  94. print("Got a ping!")
  95. app.close()
  96. def on_pong(app, msg):
  97. print("Got a pong! No need to respond")
  98. app.close()
  99. app = ws.WebSocketApp('wss://api-pub.bitfinex.com/ws/1', on_ping=on_ping, on_pong=on_pong)
  100. app.run_forever(ping_interval=2, ping_timeout=1) # , sslopt={"cert_reqs": ssl.CERT_NONE}
  101. self.assertRaises(ws.WebSocketException, app.run_forever, ping_interval=2, ping_timeout=3, sslopt={"cert_reqs": ssl.CERT_NONE})
  102. if __name__ == "__main__":
  103. unittest.main()