Skip to content

Commit 516999d

Browse files
committed
configure pytest strict
1 parent a28a7a3 commit 516999d

File tree

6 files changed

+46
-31
lines changed

6 files changed

+46
-31
lines changed

pytest_httpbin/plugin.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66

77
@pytest.fixture(scope="session")
88
def httpbin(request):
9-
server = serve.Server(application=httpbin_app)
10-
server.start()
11-
request.addfinalizer(server.stop)
12-
return server
9+
with serve.Server(application=httpbin_app) as server:
10+
yield server
1311

1412

1513
@pytest.fixture(scope="session")
1614
def httpbin_secure(request):
17-
server = serve.SecureServer(application=httpbin_app)
18-
server.start()
19-
request.addfinalizer(server.stop)
20-
return server
15+
with serve.SecureServer(application=httpbin_app) as server:
16+
yield server
2117

2218

2319
@pytest.fixture(scope="session", params=["http", "https"])

pytest_httpbin/serve.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ def finish_request(self, request, client_address):
6060
"""
6161
request.settimeout(1.0)
6262
try:
63-
ssock = ssl.wrap_socket(
64-
request,
65-
keyfile=os.path.join(CERT_DIR, "key.pem"),
66-
certfile=os.path.join(CERT_DIR, "cert.pem"),
67-
server_side=True,
68-
suppress_ragged_eofs=False,
63+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
64+
context.load_cert_chain(
65+
os.path.join(CERT_DIR, "cert.pem"),
66+
os.path.join(CERT_DIR, "key.pem"),
6967
)
70-
self.base_environ["HTTPS"] = "yes"
71-
self.RequestHandlerClass(ssock, client_address, self)
68+
ssock = context.wrap_socket(
69+
request, server_side=True, suppress_ragged_eofs=False
70+
)
71+
try:
72+
self.base_environ["HTTPS"] = "yes"
73+
self.RequestHandlerClass(ssock, client_address, self)
74+
finally:
75+
ssock.close()
7276
except Exception as e:
7377
print("pytest-httpbin server hit an exception serving request: %s" % e)
7478
print("attempting to ignore so the rest of the tests can run")
@@ -106,6 +110,16 @@ def __del__(self):
106110
def start(self):
107111
self._thread.start()
108112

113+
def __enter__(self):
114+
self.start()
115+
return self
116+
117+
def __exit__(self, *args, **kwargs):
118+
self.stop()
119+
suppress_exc = self._server.__exit__(*args, **kwargs)
120+
self._thread.join()
121+
return suppress_exc
122+
109123
def __add__(self, other):
110124
return self.url + other
111125

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ disable-noqa = True
99
max-line-length = 88
1010
extend-ignore =
1111
E203, # whitespace before : is not PEP8 compliant (& conflicts with black)
12+
13+
14+
[tool:pytest]
15+
addopts = --strict-config --strict-markers
16+
filterwarnings = error
17+
xfail_strict = true

tests/test_server.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import contextlib
23

34
import pytest
45
import requests
@@ -68,6 +69,7 @@ def test_fixed_port_environment_variables(protocol):
6869
# just have different port to avoid adrress already in use
6970
# if the second test run too fast after the first one (happens on pypy)
7071
port = 12345 + len(protocol)
72+
server = contextlib.nullcontext()
7173

7274
try:
7375
envvar_original = os.environ.get(envvar, None)
@@ -76,10 +78,7 @@ def test_fixed_port_environment_variables(protocol):
7678
assert server.port == port
7779
finally:
7880
# if we don't do this, it blocks:
79-
try:
80-
server.start()
81-
server.stop()
82-
except UnboundLocalError:
81+
with server:
8382
pass
8483

8584
# restore the original environ:

tests/util.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ def get_raw_http_response(host, port, path):
1414
]
1515

1616
# Connect to the server
17-
s = socket.socket()
18-
s.connect((host, port))
17+
with socket.socket() as s:
18+
s.connect((host, port))
1919

20-
# Send an HTTP request
21-
s.send(CRLF.join(request))
20+
# Send an HTTP request
21+
s.send(CRLF.join(request))
2222

23-
# Get the response (in several parts, if necessary)
24-
response = b""
25-
buffer = s.recv(4096)
26-
while buffer:
27-
response += buffer
23+
# Get the response (in several parts, if necessary)
24+
response = b""
2825
buffer = s.recv(4096)
26+
while buffer:
27+
response += buffer
28+
buffer = s.recv(4096)
2929

30-
return response
30+
return response

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ envlist = py37, py38, py39, py310, pypy3
1212
wheel = True
1313
wheel_build_env = build
1414
extras = test
15-
commands = pytest -v -s
15+
commands = pytest -v -s {posargs}
1616

1717
[testenv:build]
1818
# empty environment to build universal wheel once per tox invocation

0 commit comments

Comments
 (0)