Skip to content

Commit

Permalink
Move CLIENT/SERVER_CONTEXT to utils.
Browse files Browse the repository at this point in the history
Then we can reuse them for testing other implementations.
  • Loading branch information
aaugustin committed Feb 11, 2024
1 parent 87f58c7 commit 9b5273c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
8 changes: 0 additions & 8 deletions tests/sync/client.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
17 changes: 0 additions & 17 deletions tests/sync/server.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 9 additions & 3 deletions tests/sync/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 8 additions & 3 deletions tests/sync/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import pathlib
import platform
import ssl
import tempfile
import time
import unittest
Expand All @@ -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)

Expand Down

0 comments on commit 9b5273c

Please sign in to comment.