Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/courier/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,10 @@ def __init__(
)

def _enforce_trailing_slash(self, url: URL) -> URL:
if url.raw_path.endswith(b"/"):
path = url.raw_path.split(b"?", 1)[0]
if path.endswith(b"/"):
return url
return url.copy_with(raw_path=url.raw_path + b"/")
return url.copy_with(raw_path=path + b"/").copy_with(query=url.query or None)

def _make_status_error_from_response(
self,
Expand Down Expand Up @@ -466,8 +467,13 @@ def _prepare_url(self, url: str) -> URL:
# Copied from httpx's `_merge_url` method.
merge_url = URL(url)
if merge_url.is_relative_url:
merge_raw_path = self.base_url.raw_path + merge_url.raw_path.lstrip(b"/")
return self.base_url.copy_with(raw_path=merge_raw_path)
base_path = self.base_url.raw_path.split(b"?", 1)[0]
request_path = merge_url.raw_path.split(b"?", 1)[0]
query = b"&".join(part for part in (self.base_url.query, merge_url.query) if part)
return self.base_url.copy_with(raw_path=base_path + request_path.lstrip(b"/")).copy_with(
query=query or None,
fragment=merge_url.fragment or self.base_url.fragment or None,
)

return merge_url

Expand Down
104 changes: 104 additions & 0 deletions tests/test_base_url_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from __future__ import annotations

import httpx
import pytest

from courier import Courier, AsyncCourier


@pytest.mark.parametrize("prefix", ["/gateway", "/gateway/"])
@pytest.mark.parametrize("suffix", ["?workspace=demo", "?workspace=demo#section", "#section"])
def test_sync_base_url_components(prefix: str, suffix: str) -> None:
requests: list[httpx.Request] = []

def respond(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json={"id": "message-id"})

with Courier(
api_key="test-key",
base_url=f"https://gateway.example{prefix}{suffix}",
http_client=httpx.Client(transport=httpx.MockTransport(respond)),
) as client:
client.messages.retrieve("message-id")

assert len(requests) == 1
assert requests[0].url.path == "/gateway/messages/message-id"
assert requests[0].url.params == httpx.QueryParams("workspace=demo" if "?" in suffix else "")
assert requests[0].url.fragment == ("section" if "#" in suffix else "")


@pytest.mark.parametrize("prefix", ["/gateway", "/gateway/"])
@pytest.mark.parametrize("suffix", ["?workspace=demo", "?workspace=demo#section", "#section"])
async def test_async_base_url_components(prefix: str, suffix: str) -> None:
requests: list[httpx.Request] = []

def respond(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json={"id": "message-id"})

async with AsyncCourier(
api_key="test-key",
base_url=f"https://gateway.example{prefix}{suffix}",
http_client=httpx.AsyncClient(transport=httpx.MockTransport(respond)),
) as client:
await client.messages.retrieve("message-id")

assert len(requests) == 1
assert requests[0].url.path == "/gateway/messages/message-id"
assert requests[0].url.params == httpx.QueryParams("workspace=demo" if "?" in suffix else "")
assert requests[0].url.fragment == ("section" if "#" in suffix else "")


@pytest.mark.parametrize(
"endpoint, expected",
[
(
"/messages/message-id?view=full",
"https://gateway.example/gateway%2Ftenant/messages/message-id?workspace=demo&view=full",
),
("https://other.example/messages/message-id?view=full", "https://other.example/messages/message-id?view=full"),
],
)
def test_sync_request_query_and_absolute_url(endpoint: str, expected: str) -> None:
requests: list[httpx.Request] = []

def respond(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json={})

with Courier(
api_key="test-key",
base_url="https://gateway.example/gateway%2Ftenant?workspace=demo",
http_client=httpx.Client(transport=httpx.MockTransport(respond)),
) as client:
client.get(endpoint, cast_to=object)

assert requests[0].url == expected


@pytest.mark.parametrize(
"endpoint, expected",
[
(
"/messages/message-id?view=full",
"https://gateway.example/gateway%2Ftenant/messages/message-id?workspace=demo&view=full",
),
("https://other.example/messages/message-id?view=full", "https://other.example/messages/message-id?view=full"),
],
)
async def test_async_request_query_and_absolute_url(endpoint: str, expected: str) -> None:
requests: list[httpx.Request] = []

def respond(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json={})

async with AsyncCourier(
api_key="test-key",
base_url="https://gateway.example/gateway%2Ftenant?workspace=demo",
http_client=httpx.AsyncClient(transport=httpx.MockTransport(respond)),
) as client:
await client.get(endpoint, cast_to=object)

assert requests[0].url == expected