conftest.py
This file
Path
tests/conftest.py
58 lines · 1.5 KiB · Python
Last changed by
Add automatic code formatting to Requests (#6095)Nate Prewittbefore adoption2a6f290
History of this fileTake it
1try:2 from http.server import HTTPServer, SimpleHTTPRequestHandler3except ImportError:4 from BaseHTTPServer import HTTPServer5 from SimpleHTTPServer import SimpleHTTPRequestHandler67import ssl8import threading910import pytest1112from requests.compat import urljoin131415def prepare_url(value):16 # Issue #1483: Make sure the URL always has a trailing slash17 httpbin_url = value.url.rstrip("/") + "/"1819 def inner(*suffix):20 return urljoin(httpbin_url, "/".join(suffix))2122 return inner232425@pytest.fixture26def httpbin(httpbin):27 return prepare_url(httpbin)282930@pytest.fixture31def httpbin_secure(httpbin_secure):32 return prepare_url(httpbin_secure)333435@pytest.fixture36def nosan_server(tmp_path_factory):37 # delay importing until the fixture in order to make it possible38 # to deselect the test via command-line when trustme is not available39 import trustme4041 tmpdir = tmp_path_factory.mktemp("certs")42 ca = trustme.CA()43 # only commonName, no subjectAltName44 server_cert = ca.issue_cert(common_name="localhost")45 ca_bundle = str(tmpdir / "ca.pem")46 ca.cert_pem.write_to_path(ca_bundle)4748 context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)49 server_cert.configure_cert(context)50 server = HTTPServer(("localhost", 0), SimpleHTTPRequestHandler)51 server.socket = context.wrap_socket(server.socket, server_side=True)52 server_thread = threading.Thread(target=server.serve_forever)53 server_thread.start()5455 yield "localhost", server.server_address[1], ca_bundle5657 server.shutdown()58 server_thread.join()