Skip to content

Commit 9b5273c

Browse files
committed
Move CLIENT/SERVER_CONTEXT to utils.
Then we can reuse them for testing other implementations.
1 parent 87f58c7 commit 9b5273c

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

tests/sync/client.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import contextlib
2-
import ssl
32

43
from websockets.sync.client import *
54
from websockets.sync.server import WebSocketServer
65

7-
from ..utils import CERTIFICATE
8-
96

107
__all__ = [
11-
"CLIENT_CONTEXT",
128
"run_client",
139
"run_unix_client",
1410
]
1511

1612

17-
CLIENT_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
18-
CLIENT_CONTEXT.load_verify_locations(CERTIFICATE)
19-
20-
2113
@contextlib.contextmanager
2214
def run_client(wsuri_or_server, secure=None, resource_name="/", **kwargs):
2315
if isinstance(wsuri_or_server, str):

tests/sync/server.py

-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
import contextlib
2-
import ssl
32
import threading
43

54
from websockets.sync.server import *
65

7-
from ..utils import CERTIFICATE
8-
9-
10-
SERVER_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
11-
SERVER_CONTEXT.load_cert_chain(CERTIFICATE)
12-
13-
# Work around https://github.com/openssl/openssl/issues/7967
14-
15-
# This bug causes connect() to hang in tests for the client. Including this
16-
# workaround acknowledges that the issue could happen outside of the test suite.
17-
18-
# It shouldn't happen too often, or else OpenSSL 1.1.1 would be unusable. If it
19-
# happens, we can look for a library-level fix, but it won't be easy.
20-
21-
SERVER_CONTEXT.num_tickets = 0
22-
236

247
def crash(ws):
258
raise RuntimeError

tests/sync/test_client.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
from websockets.extensions.permessage_deflate import PerMessageDeflate
88
from websockets.sync.client import *
99

10-
from ..utils import MS, DeprecationTestCase, temp_unix_socket_path
11-
from .client import CLIENT_CONTEXT, run_client, run_unix_client
12-
from .server import SERVER_CONTEXT, do_nothing, run_server, run_unix_server
10+
from ..utils import (
11+
CLIENT_CONTEXT,
12+
MS,
13+
SERVER_CONTEXT,
14+
DeprecationTestCase,
15+
temp_unix_socket_path,
16+
)
17+
from .client import run_client, run_unix_client
18+
from .server import do_nothing, run_server, run_unix_server
1319

1420

1521
class ClientTests(unittest.TestCase):

tests/sync/test_server.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
from websockets.http11 import Request, Response
1515
from websockets.sync.server import *
1616

17-
from ..utils import MS, DeprecationTestCase, temp_unix_socket_path
18-
from .client import CLIENT_CONTEXT, run_client, run_unix_client
19-
from .server import (
17+
from ..utils import (
18+
CLIENT_CONTEXT,
19+
MS,
2020
SERVER_CONTEXT,
21+
DeprecationTestCase,
22+
temp_unix_socket_path,
23+
)
24+
from .client import run_client, run_unix_client
25+
from .server import (
2126
EvalShellMixin,
2227
crash,
2328
do_nothing,

tests/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import pathlib
55
import platform
6+
import ssl
67
import tempfile
78
import time
89
import unittest
@@ -17,6 +18,23 @@
1718

1819
CERTIFICATE = bytes(pathlib.Path(__file__).with_name("test_localhost.pem"))
1920

21+
CLIENT_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
22+
CLIENT_CONTEXT.load_verify_locations(CERTIFICATE)
23+
24+
25+
SERVER_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
26+
SERVER_CONTEXT.load_cert_chain(CERTIFICATE)
27+
28+
# Work around https://github.com/openssl/openssl/issues/7967
29+
30+
# This bug causes connect() to hang in tests for the client. Including this
31+
# workaround acknowledges that the issue could happen outside of the test suite.
32+
33+
# It shouldn't happen too often, or else OpenSSL 1.1.1 would be unusable. If it
34+
# happens, we can look for a library-level fix, but it won't be easy.
35+
36+
SERVER_CONTEXT.num_tickets = 0
37+
2038

2139
DATE = email.utils.formatdate(usegmt=True)
2240

0 commit comments

Comments
 (0)