Skip to content

Commit 5df2694

Browse files
committed
proxy: add ca_cert property, get_proxy_url method
Plus extend pytest plugin a bit..
1 parent 45bb5c6 commit 5df2694

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

pytest_httpserver/httpserver.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,13 @@ def __init__(
11621162
self.proxy_host = proxy_host
11631163
self.proxy_options = proxy_options
11641164

1165+
@property
1166+
def ca_cert(self):
1167+
return self.proxy_options["ca_file_cache"]
1168+
1169+
def get_proxy_url(self):
1170+
return self.url_for("")
1171+
11651172
def start(self):
11661173
proxy = WSGIProxMiddleware(
11671174
self.application,

pytest_httpserver/pytest_plugin.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ def stop(self):
3131
Plugin.PROXY = None
3232

3333

34+
@pytest.fixture(scope="session")
35+
def plugin_httpserver_class():
36+
yield PluginHTTPServer
37+
38+
39+
@pytest.fixture(scope="session")
40+
def plugin_proxy_class():
41+
print("plugin_proxy_class")
42+
yield PluginHTTPProxy
43+
44+
3445
def get_httpserver_listen_address():
3546
listen_host = os.environ.get("PYTEST_HTTPSERVER_HOST")
3647
listen_port = os.environ.get("PYTEST_HTTPSERVER_PORT")
@@ -46,7 +57,7 @@ def httpserver_listen_address():
4657

4758

4859
@pytest.fixture
49-
def httpserver(httpserver_listen_address):
60+
def httpserver(httpserver_listen_address, plugin_httpserver_class):
5061
if Plugin.SERVER:
5162
Plugin.SERVER.clear()
5263
yield Plugin.SERVER
@@ -58,13 +69,13 @@ def httpserver(httpserver_listen_address):
5869
if not port:
5970
port = HTTPServer.DEFAULT_LISTEN_PORT
6071

61-
server = PluginHTTPServer(host=host, port=port)
72+
server = plugin_httpserver_class(host=host, port=port)
6273
server.start()
6374
yield server
6475

6576

6677
@pytest.fixture
67-
def httpproxy(httpserver_listen_address, tmp_path):
78+
def httpproxy(httpserver_listen_address, tmp_path, plugin_proxy_class):
6879
if Plugin.PROXY:
6980
Plugin.PROXY.clear()
7081
yield Plugin.PROXY
@@ -78,7 +89,7 @@ def httpproxy(httpserver_listen_address, tmp_path):
7889

7990
ca_dir = tmp_path.joinpath("httpproxy_ca")
8091
ca_dir.mkdir(exist_ok=True)
81-
server = PluginHTTPProxy(host=host, port=port, proxy_options={"ca_file_cache": str(ca_dir.joinpath("wsgiprox-ca.pem"))})
92+
server = plugin_proxy_class(host=host, port=port, proxy_options={"ca_file_cache": str(ca_dir.joinpath("wsgiprox-ca.pem"))})
8293
server.start()
8394
yield server
8495

tests/test_proxy.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11

22
import requests
3+
from pytest_httpserver import HTTPProxy
34

4-
5-
def test_proxy_http(httpproxy):
5+
def test_proxy_http(httpproxy: HTTPProxy):
66
httpproxy.expect_request("/proxy/http://example.com/path/file.html").respond_with_data("Hello world!")
77

88
with requests.Session() as session:
9-
session.proxies = {"http": httpproxy.url_for("")}
9+
session.proxies = {"http": httpproxy.get_proxy_url()}
1010
resp = session.get("http://example.com/path/file.html", )
1111
assert resp.status_code == 200
1212
assert resp.text == "Hello world!"
1313

1414

15-
def test_proxy_https(httpproxy, tmp_path):
15+
def test_proxy_https(httpproxy: HTTPProxy):
1616
httpproxy.expect_request("/proxy/https://example.com/path/file.html").respond_with_data("Hello world!")
1717

1818
with requests.Session() as session:
19-
session.verify = httpproxy.proxy_options["ca_file_cache"]
20-
session.proxies = {"https": httpproxy.url_for("")}
19+
session.verify = httpproxy.ca_cert
20+
session.proxies = {"https": httpproxy.get_proxy_url()}
2121

2222
resp = session.get("https://example.com/path/file.html")
2323
assert resp.status_code == 200

0 commit comments

Comments
 (0)