diff options
author | engn33r <engn33r@users.noreply.github.com> | 2021-03-29 19:42:27 -0700 |
---|---|---|
committer | engn33r <engn33r@users.noreply.github.com> | 2021-03-29 19:42:27 -0700 |
commit | ac49468ed5a014c1beccd746618453c72e2812bf (patch) | |
tree | 4184d963cd0584ea68335c643cffb29a30a77f9a | |
parent | c98736ecf194ecd9e7e2acb8b9a18bcfaf0ca156 (diff) | |
download | websocket-client-ac49468ed5a014c1beccd746618453c72e2812bf.tar.gz |
Improve _abnf.py test coverage
-rw-r--r-- | .github/workflows/codecoverage.yml | 2 | ||||
-rw-r--r-- | websocket/tests/test_abnf.py | 83 |
2 files changed, 84 insertions, 1 deletions
diff --git a/.github/workflows/codecoverage.yml b/.github/workflows/codecoverage.yml index 7335663..69d438a 100644 --- a/.github/workflows/codecoverage.yml +++ b/.github/workflows/codecoverage.yml @@ -14,7 +14,7 @@ jobs: python-version: "3.9" - name: Run test cases for coverage collection run: | - pip install six coverage pytest pytest-cov setuptools PySocks + pip install six coverage pytest pytest-cov setuptools PySocks numpy python -c "import setuptools; print('Setup tools version'); print(setuptools.__version__)" python setup.py install pytest -vrP --cov=websocket websocket/tests --cov-config=.coveragerc --cov-report=xml diff --git a/websocket/tests/test_abnf.py b/websocket/tests/test_abnf.py new file mode 100644 index 0000000..17eff53 --- /dev/null +++ b/websocket/tests/test_abnf.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +# +""" +websocket - WebSocket client library for Python + +Copyright (C) 2010 Hiroki Ohtani(liris) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +""" + +import os +import os.path +import socket +import websocket as ws +from websocket._abnf import * +import sys +sys.path[0:0] = [""] + +try: + import socks +except: + HAS_PYSOCKS = False + +if sys.version_info[0] == 2 and sys.version_info[1] < 7: + import unittest2 as unittest +else: + import unittest + + +class ABNFTest(unittest.TestCase): + + def testInit(self): + a = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING) + self.assertEqual(a.fin, 0) + self.assertEqual(a.rsv1, 0) + self.assertEqual(a.rsv2, 0) + self.assertEqual(a.rsv3, 0) + self.assertEqual(a.opcode, 9) + self.assertEqual(a.data, '') + a_bad = ABNF(0,1,0,0, opcode=77) + self.assertEqual(a_bad.rsv1, 1) + self.assertEqual(a_bad.opcode, 77) + + def testValidate(self): + a = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING) + self.assertRaises(ws.WebSocketProtocolException, a.validate) + a_bad = ABNF(0,1,0,0, opcode=77) + self.assertRaises(ws.WebSocketProtocolException, a_bad.validate) + a_close = ABNF(0,1,0,0, opcode=ABNF.OPCODE_CLOSE, data="abcdefgh1234567890abcdefgh1234567890abcdefgh1234567890abcdefgh1234567890") + self.assertRaises(ws.WebSocketProtocolException, a_close.validate) + + def testMask(self): + ab = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING) + bytes_val = bytes("aaaa", 'utf-8') + self.assertEqual(ab._get_masked(bytes_val), bytes_val) + + def testFrameBuffer(self): + fb = frame_buffer(0, True) + self.assertEqual(fb.recv, 0) + self.assertEqual(fb.skip_utf8_validation, True) + fb.clear + self.assertEqual(fb.header, None) + self.assertEqual(fb.length, None) + self.assertEqual(fb.mask, None) + self.assertEqual(fb.has_mask(), False) + + +if __name__ == "__main__": + unittest.main() + |