test_abnf.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 websocket as ws
  20. from websocket._abnf import *
  21. import sys
  22. sys.path[0:0] = [""]
  23. if sys.version_info[0] == 2 and sys.version_info[1] < 7:
  24. import unittest2 as unittest
  25. else:
  26. import unittest
  27. class ABNFTest(unittest.TestCase):
  28. def testInit(self):
  29. a = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING)
  30. self.assertEqual(a.fin, 0)
  31. self.assertEqual(a.rsv1, 0)
  32. self.assertEqual(a.rsv2, 0)
  33. self.assertEqual(a.rsv3, 0)
  34. self.assertEqual(a.opcode, 9)
  35. self.assertEqual(a.data, '')
  36. a_bad = ABNF(0,1,0,0, opcode=77)
  37. self.assertEqual(a_bad.rsv1, 1)
  38. self.assertEqual(a_bad.opcode, 77)
  39. def testValidate(self):
  40. a = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING)
  41. self.assertRaises(ws.WebSocketProtocolException, a.validate)
  42. a_bad = ABNF(0,1,0,0, opcode=77)
  43. self.assertRaises(ws.WebSocketProtocolException, a_bad.validate)
  44. a_close = ABNF(0,1,0,0, opcode=ABNF.OPCODE_CLOSE, data="abcdefgh1234567890abcdefgh1234567890abcdefgh1234567890abcdefgh1234567890")
  45. self.assertRaises(ws.WebSocketProtocolException, a_close.validate)
  46. # This caused an error in the Python 2.7 Github Actions build
  47. # Uncomment test case when Python 2 support no longer wanted
  48. # def testMask(self):
  49. # ab = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING)
  50. # bytes_val = bytes("aaaa", 'utf-8')
  51. # self.assertEqual(ab._get_masked(bytes_val), bytes_val)
  52. def testFrameBuffer(self):
  53. fb = frame_buffer(0, True)
  54. self.assertEqual(fb.recv, 0)
  55. self.assertEqual(fb.skip_utf8_validation, True)
  56. fb.clear
  57. self.assertEqual(fb.header, None)
  58. self.assertEqual(fb.length, None)
  59. self.assertEqual(fb.mask, None)
  60. self.assertEqual(fb.has_mask(), False)
  61. if __name__ == "__main__":
  62. unittest.main()