Skip to content
Myr

conftest.py

This file

Path

tests/conftest.py

67 lines · 1.9 KiB · Python

Last changed by

Clear proxy env vars before every test run (#7423)Nate Prewittbefore adoption25340eb

History of this file

Take 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.fixture(autouse=True)26def clean_proxy_environ(monkeypatch):27    """Remove proxy related environment variables for every test."""28    proxy_vars = ("http_proxy", "https_proxy", "no_proxy", "ftp_proxy", "all_proxy")29    for var in proxy_vars:30        monkeypatch.delenv(var, raising=False)31        monkeypatch.delenv(var.upper(), raising=False)323334@pytest.fixture35def httpbin(httpbin):36    return prepare_url(httpbin)373839@pytest.fixture40def httpbin_secure(httpbin_secure):41    return prepare_url(httpbin_secure)424344@pytest.fixture45def nosan_server(tmp_path_factory):46    # delay importing until the fixture in order to make it possible47    # to deselect the test via command-line when trustme is not available48    import trustme4950    tmpdir = tmp_path_factory.mktemp("certs")51    ca = trustme.CA()52    # only commonName, no subjectAltName53    server_cert = ca.issue_cert(common_name="localhost")54    ca_bundle = str(tmpdir / "ca.pem")55    ca.cert_pem.write_to_path(ca_bundle)5657    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)58    server_cert.configure_cert(context)59    server = HTTPServer(("localhost", 0), SimpleHTTPRequestHandler)60    server.socket = context.wrap_socket(server.socket, server_side=True)61    server_thread = threading.Thread(target=server.serve_forever)62    server_thread.start()6364    yield "localhost", server.server_address[1], ca_bundle6566    server.shutdown()67    server_thread.join()