Skip to content

Commit 1dd8ef3

Browse files
author
jschaff
committed
Remove the HTTPTransport from the client building ...
Adding a blank transport removes our auto-discovery of the proxy configurations. Removing it for the base level tests appears to allow for automatic proxy configuration discovery
1 parent 91100c2 commit 1dd8ef3

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

biothings_client/client/asynchronous.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ async def _build_http_client(self, cache_db: Union[str, Path] = None) -> None:
107107
:return: None
108108
"""
109109
if not self.http_client_setup:
110-
http_transport = httpx.AsyncHTTPTransport()
111-
self.http_client = httpx.AsyncClient(transport=http_transport)
110+
self.http_client = httpx.AsyncClient()
112111
self.http_client_setup = True
113112
self.http_cache_client_setup = False
114113

biothings_client/client/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ def _build_http_client(self, cache_db: Union[str, Path] = None) -> None:
105105
to set the values for the http_client property
106106
"""
107107
if not self.http_client_setup:
108-
http_transport = httpx.HTTPTransport()
109-
self.http_client = httpx.Client(transport=http_transport)
108+
self.http_client = httpx.Client()
110109
self.http_client_setup = True
111110
self.http_cache_client_setup = False
112111

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Fixtures for the biothings_client testing
33
"""
44

5+
import os
6+
57
import pytest
68

79
from biothings_client import get_client, get_async_client
@@ -111,3 +113,12 @@ def async_geneset_client() -> AsyncMyGenesetInfo:
111113
client = "geneset"
112114
geneset_client = get_async_client(client)
113115
return geneset_client
116+
117+
118+
@pytest.fixture(scope="function")
119+
def mock_client_proxy_configuration() -> None:
120+
os.environ["HTTP_PROXY"] = "http://fakehttpproxyhost:6374"
121+
os.environ["HTTPS_PROXY"] = "http://fakehttpsproxyhost:6375"
122+
yield
123+
os.environ.pop("HTTP_PROXY", None)
124+
os.environ.pop("HTTPS_PROXY", None)

tests/test_async.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import List
66

7+
import httpx
78
import pytest
89

910
import biothings_client
@@ -82,3 +83,35 @@ async def test_url_protocol(client_name: str):
8283
# Transform back to HTTPS
8384
client_instance.use_https()
8485
client_instance.url.startswith(https_protocol)
86+
87+
88+
@pytest.mark.asyncio
89+
async def test_async_client_proxy_discovery(mock_client_proxy_configuration):
90+
"""
91+
Tests for verifying that we properly auto-discover the
92+
proxy configuration from the environment using the built-in
93+
methods provided by HTTPX
94+
95+
Brought to light by user issues on VPN
96+
https://github.com/biothings/mygene.py/issues/26#issuecomment-2588065562
97+
"""
98+
client_name = "gene"
99+
gene_client = biothings_client.get_async_client(client_name)
100+
await gene_client._build_http_client()
101+
for url_pattern, http_transport in gene_client.http_client._mounts.items():
102+
assert isinstance(url_pattern, httpx._utils.URLPattern)
103+
assert isinstance(http_transport, httpx.AsyncHTTPTransport)
104+
105+
if url_pattern.pattern == "https://":
106+
proxy_url = http_transport._pool._proxy_url
107+
assert proxy_url.scheme == b"http"
108+
assert proxy_url.host == b"fakehttpsproxyhost"
109+
assert proxy_url.port == 6375
110+
assert proxy_url.target == b"/"
111+
112+
elif url_pattern.pattern == "http://":
113+
proxy_url = http_transport._pool._proxy_url
114+
assert proxy_url.scheme == b"http"
115+
assert proxy_url.host == b"fakehttpproxyhost"
116+
assert proxy_url.port == 6374
117+
assert proxy_url.target == b"/"

tests/test_sync.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import List
66

7+
import httpx
78
import pytest
89

910
import biothings_client
@@ -79,3 +80,34 @@ def test_url_protocol(client_name: str):
7980
# Transform back to HTTPS
8081
client_instance.use_https()
8182
client_instance.url.startswith(https_protocol)
83+
84+
85+
def test_client_proxy_discovery(mock_client_proxy_configuration):
86+
"""
87+
Tests for verifying that we properly auto-discover the
88+
proxy configuration from the environment using the built-in
89+
methods provided by HTTPX
90+
91+
Brought to light by user issues on VPN
92+
https://github.com/biothings/mygene.py/issues/26#issuecomment-2588065562
93+
"""
94+
client_name = "gene"
95+
gene_client = biothings_client.get_client(client_name)
96+
gene_client._build_http_client()
97+
for url_pattern, http_transport in gene_client.http_client._mounts.items():
98+
assert isinstance(url_pattern, httpx._utils.URLPattern)
99+
assert isinstance(http_transport, httpx.HTTPTransport)
100+
101+
if url_pattern.pattern == "https://":
102+
proxy_url = http_transport._pool._proxy_url
103+
assert proxy_url.scheme == b"http"
104+
assert proxy_url.host == b"fakehttpsproxyhost"
105+
assert proxy_url.port == 6375
106+
assert proxy_url.target == b"/"
107+
108+
elif url_pattern.pattern == "http://":
109+
proxy_url = http_transport._pool._proxy_url
110+
assert proxy_url.scheme == b"http"
111+
assert proxy_url.host == b"fakehttpproxyhost"
112+
assert proxy_url.port == 6374
113+
assert proxy_url.target == b"/"

0 commit comments

Comments
 (0)