| 1 |
try: |
| 2 |
from http.server import HTTPServer, SimpleHTTPRequestHandler |
| 3 |
except ImportError: |
| 4 |
from BaseHTTPServer import HTTPServer |
| 5 |
from SimpleHTTPServer import SimpleHTTPRequestHandler |
| 6 |
|
| 7 |
import ssl |
| 8 |
import threading |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from requests.compat import urljoin |
| 13 |
|
| 14 |
|
| 15 |
def prepare_url(value): |
| 16 |
# Issue #1483: Make sure the URL always has a trailing slash |
| 17 |
httpbin_url = value.url.rstrip("/") + "/" |
| 18 |
|
| 19 |
def inner(*suffix): |
| 20 |
return urljoin(httpbin_url, "/".join(suffix)) |
| 21 |
|
| 22 |
return inner |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture(autouse=True) |
| 26 |
def 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) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture |
| 35 |
def httpbin(httpbin): |
| 36 |
return prepare_url(httpbin) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def httpbin_secure(httpbin_secure): |
| 41 |
return prepare_url(httpbin_secure) |
| 42 |
|
| 43 |
|
| 44 |
@pytest.fixture |
| 45 |
def nosan_server(tmp_path_factory): |
| 46 |
# delay importing until the fixture in order to make it possible |
| 47 |
# to deselect the test via command-line when trustme is not available |
| 48 |
import trustme |
| 49 |
|
| 50 |
tmpdir = tmp_path_factory.mktemp("certs") |
| 51 |
ca = trustme.CA() |
| 52 |
# only commonName, no subjectAltName |
| 53 |
server_cert = ca.issue_cert(common_name="localhost") |
| 54 |
ca_bundle = str(tmpdir / "ca.pem") |
| 55 |
ca.cert_pem.write_to_path(ca_bundle) |
| 56 |
|
| 57 |
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() |
| 63 |
|
| 64 |
yield "localhost", server.server_address[1], ca_bundle |
| 65 |
|
| 66 |
server.shutdown() |
| 67 |
server_thread.join() |