Skip to content

Commit 365c501

Browse files
adamtheturtleclaude
andcommitted
Fix urljoin in query.py and add path-prefix base URL tests
- Apply the same urljoin → string concatenation fix to query.py (CloudRecoService had the identical bug) - Add TestCustomBaseVWSURL::test_custom_base_url_with_path_prefix and TestCustomBaseVWQURL::test_custom_base_url_with_path_prefix to assert that a base URL containing a path prefix is preserved in the outgoing request URL; these tests would fail with the old urljoin approach Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2946610 commit 365c501

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/vws/query.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import json
66
from http import HTTPMethod, HTTPStatus
77
from typing import Any, BinaryIO
8-
from urllib.parse import urljoin
98

109
import requests
1110
from beartype import BeartypeConf, beartype
@@ -146,7 +145,7 @@ def query(
146145

147146
requests_response = requests.request(
148147
method=method,
149-
url=urljoin(base=self._base_vwq_url, url=request_path),
148+
url=self._base_vwq_url.rstrip("/") + request_path,
150149
headers=headers,
151150
data=content,
152151
timeout=self._request_timeout_seconds,

tests/test_query.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import datetime
44
import io
5+
import secrets
56
import uuid
67
from typing import BinaryIO
78

89
import pytest
910
import requests
11+
import responses as responses_mock
1012
from freezegun import freeze_time
1113
from mock_vws import MockVWS
1214
from mock_vws.database import CloudDatabase
@@ -190,6 +192,40 @@ def test_custom_base_url(image: io.BytesIO | BinaryIO) -> None:
190192
match = matches[0]
191193
assert match.target_id == target_id
192194

195+
@staticmethod
196+
@responses_mock.activate
197+
def test_custom_base_url_with_path_prefix(
198+
image: io.BytesIO | BinaryIO,
199+
) -> None:
200+
"""
201+
A base VWQ URL with a path prefix is used as-is, without the
202+
prefix
203+
being dropped.
204+
"""
205+
base_vwq_url = "http://example.com/prefix"
206+
responses_mock.add(
207+
method=responses_mock.POST,
208+
url="http://example.com/prefix/v1/query",
209+
json={
210+
"result_code": "Success",
211+
"results": [],
212+
"query_id": "abc",
213+
},
214+
status=200,
215+
)
216+
cloud_reco_client = CloudRecoService(
217+
client_access_key=secrets.token_hex(),
218+
client_secret_key=secrets.token_hex(),
219+
base_vwq_url=base_vwq_url,
220+
)
221+
222+
cloud_reco_client.query(image=image)
223+
224+
assert len(responses_mock.calls) == 1
225+
assert responses_mock.calls[0].request.url == (
226+
"http://example.com/prefix/v1/query"
227+
)
228+
193229

194230
class TestMaxNumResults:
195231
"""Tests for the ``max_num_results`` parameter of ``query``."""

tests/test_vws.py

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

1010
import pytest
1111
import requests
12+
import responses as responses_mock
1213
from freezegun import freeze_time
1314
from mock_vws import MockVWS
1415
from mock_vws.database import CloudDatabase
@@ -246,6 +247,38 @@ def test_custom_base_url(image: io.BytesIO | BinaryIO) -> None:
246247
application_metadata=None,
247248
)
248249

250+
@staticmethod
251+
@responses_mock.activate
252+
def test_custom_base_url_with_path_prefix() -> None:
253+
"""
254+
A base VWS URL with a path prefix is used as-is, without the
255+
prefix
256+
being dropped.
257+
"""
258+
base_vws_url = "http://example.com/prefix"
259+
responses_mock.add(
260+
method=responses_mock.GET,
261+
url="http://example.com/prefix/targets",
262+
json={
263+
"result_code": "Success",
264+
"results": [],
265+
"transaction_id": "abc",
266+
},
267+
status=200,
268+
)
269+
vws_client = VWS(
270+
server_access_key=secrets.token_hex(),
271+
server_secret_key=secrets.token_hex(),
272+
base_vws_url=base_vws_url,
273+
)
274+
275+
vws_client.list_targets()
276+
277+
assert len(responses_mock.calls) == 1
278+
assert responses_mock.calls[0].request.url == (
279+
"http://example.com/prefix/targets"
280+
)
281+
249282

250283
class TestListTargets:
251284
"""Tests for listing targets."""

0 commit comments

Comments
 (0)