diff --git a/tests/sync/client.py b/tests/sync/client.py index bb4855c7..72eb5b8d 100644 --- a/tests/sync/client.py +++ b/tests/sync/client.py @@ -1,23 +1,15 @@ import contextlib -import ssl from websockets.sync.client import * from websockets.sync.server import WebSocketServer -from ..utils import CERTIFICATE - __all__ = [ - "CLIENT_CONTEXT", "run_client", "run_unix_client", ] -CLIENT_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) -CLIENT_CONTEXT.load_verify_locations(CERTIFICATE) - - @contextlib.contextmanager def run_client(wsuri_or_server, secure=None, resource_name="/", **kwargs): if isinstance(wsuri_or_server, str): diff --git a/tests/sync/server.py b/tests/sync/server.py index a9a77438..10ab789c 100644 --- a/tests/sync/server.py +++ b/tests/sync/server.py @@ -1,25 +1,8 @@ import contextlib -import ssl import threading from websockets.sync.server import * -from ..utils import CERTIFICATE - - -SERVER_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) -SERVER_CONTEXT.load_cert_chain(CERTIFICATE) - -# Work around https://github.com/openssl/openssl/issues/7967 - -# This bug causes connect() to hang in tests for the client. Including this -# workaround acknowledges that the issue could happen outside of the test suite. - -# It shouldn't happen too often, or else OpenSSL 1.1.1 would be unusable. If it -# happens, we can look for a library-level fix, but it won't be easy. - -SERVER_CONTEXT.num_tickets = 0 - def crash(ws): raise RuntimeError diff --git a/tests/sync/test_client.py b/tests/sync/test_client.py index c403b963..bebf68aa 100644 --- a/tests/sync/test_client.py +++ b/tests/sync/test_client.py @@ -7,9 +7,15 @@ from websockets.extensions.permessage_deflate import PerMessageDeflate from websockets.sync.client import * -from ..utils import MS, DeprecationTestCase, temp_unix_socket_path -from .client import CLIENT_CONTEXT, run_client, run_unix_client -from .server import SERVER_CONTEXT, do_nothing, run_server, run_unix_server +from ..utils import ( + CLIENT_CONTEXT, + MS, + SERVER_CONTEXT, + DeprecationTestCase, + temp_unix_socket_path, +) +from .client import run_client, run_unix_client +from .server import do_nothing, run_server, run_unix_server class ClientTests(unittest.TestCase): diff --git a/tests/sync/test_server.py b/tests/sync/test_server.py index f9f30baf..490a3f63 100644 --- a/tests/sync/test_server.py +++ b/tests/sync/test_server.py @@ -14,10 +14,15 @@ from websockets.http11 import Request, Response from websockets.sync.server import * -from ..utils import MS, DeprecationTestCase, temp_unix_socket_path -from .client import CLIENT_CONTEXT, run_client, run_unix_client -from .server import ( +from ..utils import ( + CLIENT_CONTEXT, + MS, SERVER_CONTEXT, + DeprecationTestCase, + temp_unix_socket_path, +) +from .client import run_client, run_unix_client +from .server import ( EvalShellMixin, crash, do_nothing, diff --git a/tests/utils.py b/tests/utils.py index 2937a2f1..bd3b61d7 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -3,6 +3,7 @@ import os import pathlib import platform +import ssl import tempfile import time import unittest @@ -17,6 +18,23 @@ CERTIFICATE = bytes(pathlib.Path(__file__).with_name("test_localhost.pem")) +CLIENT_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) +CLIENT_CONTEXT.load_verify_locations(CERTIFICATE) + + +SERVER_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) +SERVER_CONTEXT.load_cert_chain(CERTIFICATE) + +# Work around https://github.com/openssl/openssl/issues/7967 + +# This bug causes connect() to hang in tests for the client. Including this +# workaround acknowledges that the issue could happen outside of the test suite. + +# It shouldn't happen too often, or else OpenSSL 1.1.1 would be unusable. If it +# happens, we can look for a library-level fix, but it won't be easy. + +SERVER_CONTEXT.num_tickets = 0 + DATE = email.utils.formatdate(usegmt=True)