Skip to content

Commit c8626a1

Browse files
Use path_prefix to compute base_url in httpx (#243)
* Use path_prefix to compute base_url in httpx * unit test * add similar unit test for requests nodes --------- Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
1 parent d3c1d8d commit c8626a1

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

elastic_transport/_node/_http_httpx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def __init__(self, config: NodeConfig):
106106
ssl_context.load_cert_chain(config.client_cert)
107107

108108
self.client = httpx.AsyncClient(
109-
base_url=f"{config.scheme}://{config.host}:{config.port}",
109+
base_url=f"{config.scheme}://{config.host}:{config.port}{config.path_prefix}",
110110
limits=httpx.Limits(max_connections=config.connections_per_node),
111111
verify=ssl_context or False,
112112
timeout=config.request_timeout,

tests/node/test_http_httpx.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ def test_ca_certs_with_verify_ssl_false_raises_error(self):
8181
str(exc.value) == "You cannot use 'ca_certs' when 'verify_certs=False'"
8282
)
8383

84+
def test_path_prefix(self):
85+
node = create_node(
86+
NodeConfig(
87+
"http",
88+
"localhost",
89+
9200,
90+
path_prefix="/test",
91+
)
92+
)
93+
assert node.base_url == "http://localhost:9200/test"
94+
assert node.client.base_url == "http://localhost:9200/test/"
95+
8496

8597
@pytest.mark.anyio
8698
class TestHttpxAsyncNode:

tests/node/test_http_requests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,14 @@ def test_requests_custom_auth(self):
235235
node.perform_request("GET", "/")
236236
(request,), _ = node.session.send.call_args
237237
assert request.headers["authorization"] == "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
238+
239+
def test_path_prefix(self):
240+
node = self._get_mock_node(
241+
NodeConfig(
242+
"http",
243+
"localhost",
244+
9200,
245+
path_prefix="/test",
246+
)
247+
)
248+
assert node.base_url == "http://localhost:9200/test"

0 commit comments

Comments
 (0)