test_20_websockets.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #***************************************************************************
  4. # _ _ ____ _
  5. # Project ___| | | | _ \| |
  6. # / __| | | | |_) | |
  7. # | (__| |_| | _ <| |___
  8. # \___|\___/|_| \_\_____|
  9. #
  10. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. #
  12. # This software is licensed as described in the file COPYING, which
  13. # you should have received as part of this distribution. The terms
  14. # are also available at https://curl.se/docs/copyright.html.
  15. #
  16. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. # copies of the Software, and permit persons to whom the Software is
  18. # furnished to do so, under the terms of the COPYING file.
  19. #
  20. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. # KIND, either express or implied.
  22. #
  23. # SPDX-License-Identifier: curl
  24. #
  25. ###########################################################################
  26. #
  27. import logging
  28. import os
  29. import shutil
  30. import subprocess
  31. import time
  32. from datetime import datetime, timedelta
  33. import pytest
  34. from testenv import Env, CurlClient, LocalClient
  35. log = logging.getLogger(__name__)
  36. @pytest.mark.skipif(condition=not Env.curl_has_protocol('ws'),
  37. reason='curl lacks ws protocol support')
  38. class TestWebsockets:
  39. def check_alive(self, env, timeout=5):
  40. curl = CurlClient(env=env)
  41. url = f'http://localhost:{env.ws_port}/'
  42. end = datetime.now() + timedelta(seconds=timeout)
  43. while datetime.now() < end:
  44. r = curl.http_download(urls=[url])
  45. if r.exit_code == 0:
  46. return True
  47. time.sleep(.1)
  48. return False
  49. def _mkpath(self, path):
  50. if not os.path.exists(path):
  51. return os.makedirs(path)
  52. def _rmrf(self, path):
  53. if os.path.exists(path):
  54. return shutil.rmtree(path)
  55. @pytest.fixture(autouse=True, scope='class')
  56. def ws_echo(self, env):
  57. run_dir = os.path.join(env.gen_dir, 'ws-echo-server')
  58. err_file = os.path.join(run_dir, 'stderr')
  59. self._rmrf(run_dir)
  60. self._mkpath(run_dir)
  61. with open(err_file, 'w') as cerr:
  62. cmd = os.path.join(env.project_dir,
  63. 'tests/http/testenv/ws_echo_server.py')
  64. args = [cmd, '--port', str(env.ws_port)]
  65. p = subprocess.Popen(args=args, cwd=run_dir, stderr=cerr,
  66. stdout=cerr)
  67. assert self.check_alive(env)
  68. yield
  69. p.terminate()
  70. def test_20_01_basic(self, env: Env, ws_echo, repeat):
  71. curl = CurlClient(env=env)
  72. url = f'http://localhost:{env.ws_port}/'
  73. r = curl.http_download(urls=[url])
  74. r.check_response(http_status=426)
  75. def test_20_02_pingpong_small(self, env: Env, ws_echo, repeat):
  76. payload = 125 * "x"
  77. client = LocalClient(env=env, name='ws-pingpong')
  78. if not client.exists():
  79. pytest.skip(f'example client not built: {client.name}')
  80. url = f'ws://localhost:{env.ws_port}/'
  81. r = client.run(args=[url, payload])
  82. r.check_exit_code(0)
  83. # the python websocket server does not like 'large' control frames
  84. def test_20_03_pingpong_too_large(self, env: Env, ws_echo, repeat):
  85. payload = 127 * "x"
  86. client = LocalClient(env=env, name='ws-pingpong')
  87. if not client.exists():
  88. pytest.skip(f'example client not built: {client.name}')
  89. url = f'ws://localhost:{env.ws_port}/'
  90. r = client.run(args=[url, payload])
  91. r.check_exit_code(56)
  92. # the python websocket server does not like 'large' control frames
  93. def test_20_04_data_small(self, env: Env, ws_echo, repeat):
  94. client = LocalClient(env=env, name='ws-data')
  95. if not client.exists():
  96. pytest.skip(f'example client not built: {client.name}')
  97. url = f'ws://localhost:{env.ws_port}/'
  98. r = client.run(args=[url, str(0), str(10)])
  99. r.check_exit_code(0)
  100. # the python websocket server does not like 'large' control frames
  101. def test_20_05_data_med(self, env: Env, ws_echo, repeat):
  102. client = LocalClient(env=env, name='ws-data')
  103. if not client.exists():
  104. pytest.skip(f'example client not built: {client.name}')
  105. url = f'ws://localhost:{env.ws_port}/'
  106. r = client.run(args=[url, str(120), str(130)])
  107. r.check_exit_code(0)
  108. # the python websocket server does not like 'large' control frames
  109. def test_20_06_data_large(self, env: Env, ws_echo, repeat):
  110. client = LocalClient(env=env, name='ws-data')
  111. if not client.exists():
  112. pytest.skip(f'example client not built: {client.name}')
  113. url = f'ws://localhost:{env.ws_port}/'
  114. r = client.run(args=[url, str(65535 - 5), str(65535 + 5)])
  115. r.check_exit_code(0)