Merge pull request #891 from cbcoutinho/fix/309-dav-encoding-exceptiongroup
fix(vector): URL-encode DAV paths and unwrap TaskGroup exceptions
This commit is contained in:
@@ -510,3 +510,133 @@ def test_parse_search_response_decodes_non_ascii_paths(mocker):
|
||||
assert results[0]["href"] == "/remote.php/dav/files/testuser/学生邮箱/report.pdf"
|
||||
# name comes from <d:displayname>, which is not URL-encoded; sanity-check it.
|
||||
assert results[0]["name"] == "report.pdf"
|
||||
|
||||
|
||||
def _request_url(mock_http_client) -> str:
|
||||
"""Positional URL passed to the underlying httpx ``request`` call."""
|
||||
return mock_http_client.request.call_args[0][1]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize(
|
||||
"path, expected",
|
||||
[
|
||||
("", "/remote.php/dav/files/testuser/"),
|
||||
("/Documents/notes.txt", "/remote.php/dav/files/testuser/Documents/notes.txt"),
|
||||
("Documents/notes.txt", "/remote.php/dav/files/testuser/Documents/notes.txt"),
|
||||
("a/b #1.pdf", "/remote.php/dav/files/testuser/a/b%20%231.pdf"),
|
||||
("law/x, y z.pdf", "/remote.php/dav/files/testuser/law/x%2C%20y%20%20z.pdf"),
|
||||
(
|
||||
"学生邮箱/r.pdf",
|
||||
"/remote.php/dav/files/testuser/%E5%AD%A6%E7%94%9F%E9%82%AE%E7%AE%B1/r.pdf",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_webdav_path_encoding(path, expected):
|
||||
"""_webdav_path encodes the decoded caller path once, preserving '/', and
|
||||
strips a leading slash. Every caller-path builder routes through this, so
|
||||
it is the single source of truth for their encoding."""
|
||||
client = WebDAVClient(AsyncMock(), "testuser")
|
||||
assert client._webdav_path(path) == expected
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_encode_dav_path_encodes_exactly_once():
|
||||
"""Pins the decoded-input precondition: a literal '%' becomes '%25', so an
|
||||
already-encoded path passed in error would double-encode (caught here)."""
|
||||
from nextcloud_mcp_server.client.webdav import _encode_dav_path
|
||||
|
||||
assert _encode_dav_path("already%20encoded.pdf") == "already%2520encoded.pdf"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_read_file_encodes_special_chars(mocker):
|
||||
"""read_file must percent-encode '#', commas, and spaces in the path (card 309).
|
||||
|
||||
Paths arrive already URL-decoded from PROPFIND/REPORT, so an unencoded '#'
|
||||
reaches httpx as a URL fragment and silently truncates the request → 404 on
|
||||
valid files (e.g. OHR-Bench law filenames). The outgoing request path must be
|
||||
percent-encoded.
|
||||
"""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.content = b"%PDF-1.4 data"
|
||||
mock_response.headers = {"content-type": "application/pdf"}
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
# Name with a '#', a comma, a double space and a trailing space before ".pdf".
|
||||
await client.read_file("law/ADMA BioManufacturing, LLC - Amendment #2 .pdf")
|
||||
|
||||
url = _request_url(mock_http_client)
|
||||
assert url.startswith("/remote.php/dav/files/testuser/")
|
||||
# The hazardous characters are encoded; path separators are preserved.
|
||||
assert "%23" in url # '#'
|
||||
assert "%2C" in url # ','
|
||||
assert "%20" in url # space
|
||||
assert "#" not in url
|
||||
assert ", " not in url
|
||||
assert "/law/" in url
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_read_file_ascii_path_unchanged(mocker):
|
||||
"""A plain ASCII path must pass through unchanged (no spurious encoding)."""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.content = b"data"
|
||||
mock_response.headers = {"content-type": "text/plain"}
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
await client.read_file("Documents/notes.txt")
|
||||
|
||||
assert (
|
||||
_request_url(mock_http_client)
|
||||
== "/remote.php/dav/files/testuser/Documents/notes.txt"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_move_resource_encodes_destination_header(mocker):
|
||||
"""The MOVE Destination header must be percent-encoded too (card 309)."""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.status_code = 201
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
await client.move_resource("a/old.pdf", "b/new #1.pdf")
|
||||
|
||||
call = mock_http_client.request.call_args
|
||||
# Source is the request path; destination is the header.
|
||||
assert call[0][1] == "/remote.php/dav/files/testuser/a/old.pdf"
|
||||
destination = call.kwargs["headers"]["Destination"]
|
||||
assert "%23" in destination
|
||||
assert "#" not in destination
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_copy_resource_encodes_destination_header(mocker):
|
||||
"""The COPY Destination header must be percent-encoded too (card 309)."""
|
||||
mock_http_client = AsyncMock()
|
||||
client = WebDAVClient(mock_http_client, "testuser")
|
||||
|
||||
mock_response = AsyncMock()
|
||||
mock_response.status_code = 201
|
||||
mock_response.raise_for_status = mocker.Mock()
|
||||
mock_http_client.request = AsyncMock(return_value=mock_response)
|
||||
|
||||
await client.copy_resource("a/old.pdf", "b/new #1.pdf")
|
||||
|
||||
call = mock_http_client.request.call_args
|
||||
assert call[0][1] == "/remote.php/dav/files/testuser/a/old.pdf"
|
||||
destination = call.kwargs["headers"]["Destination"]
|
||||
assert "%23" in destination
|
||||
assert "#" not in destination
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Unit tests for vector-sync error formatting (card 309)."""
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from nextcloud_mcp_server.vector._errors import format_exception_group
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_format_plain_exception_returns_repr():
|
||||
exc = httpx.ConnectError("Connection error")
|
||||
assert format_exception_group(exc) == repr(exc)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_format_exception_group_names_leaf_cause():
|
||||
"""A single-child group must surface the real ConnectError, not the group's
|
||||
useless 'unhandled errors in a TaskGroup' default message."""
|
||||
leaf = httpx.ConnectError("Connection error")
|
||||
group = BaseExceptionGroup("unhandled errors in a TaskGroup", [leaf])
|
||||
|
||||
formatted = format_exception_group(group)
|
||||
|
||||
assert "ConnectError" in formatted
|
||||
# Assert the full leaf repr survives, not just the type name -- guards a
|
||||
# future format change that kept the type but dropped the message.
|
||||
assert repr(leaf) in formatted
|
||||
assert "unhandled errors in a TaskGroup" not in formatted
|
||||
assert "1 sub-exception" in formatted
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_format_nested_exception_group_flattens_all_leaves():
|
||||
inner = BaseExceptionGroup(
|
||||
"inner", [ValueError("bad value"), httpx.ConnectError("conn")]
|
||||
)
|
||||
outer = BaseExceptionGroup("outer", [inner, RuntimeError("boom")])
|
||||
|
||||
formatted = format_exception_group(outer)
|
||||
|
||||
assert "ValueError" in formatted
|
||||
assert "ConnectError" in formatted
|
||||
assert "RuntimeError" in formatted
|
||||
assert "3 sub-exceptions" in formatted
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Regression test for processor_task's exception handler (card 309 / PR #891).
|
||||
|
||||
If ``receive_stream.receive()`` raises something other than
|
||||
``TimeoutError``/``EndOfStream`` before any document is bound, the broad
|
||||
``except`` handler must not crash on an unbound ``doc_task`` name.
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import anyio
|
||||
import pytest
|
||||
|
||||
from nextcloud_mcp_server.vector.processor import processor_task
|
||||
|
||||
|
||||
class _ReceiveBoomThenEnd:
|
||||
"""First receive() raises a non-Timeout error (no doc_task bound yet); the
|
||||
second ends the stream so the loop exits."""
|
||||
|
||||
def __init__(self, shutdown: anyio.Event):
|
||||
self._calls = 0
|
||||
self._shutdown = shutdown
|
||||
|
||||
async def receive(self):
|
||||
self._calls += 1
|
||||
if self._calls == 1:
|
||||
raise RuntimeError("transport blew up before any document")
|
||||
self._shutdown.set()
|
||||
raise anyio.EndOfStream
|
||||
|
||||
def statistics(self): # pragma: no cover - not reached on the error path
|
||||
return MagicMock(current_buffer_used=0)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
async def test_processor_task_receive_error_does_not_raise_unbound(caplog):
|
||||
shutdown = anyio.Event()
|
||||
stream = _ReceiveBoomThenEnd(shutdown)
|
||||
|
||||
# Must complete without a NameError leaking out of the except handler.
|
||||
with caplog.at_level("ERROR", logger="nextcloud_mcp_server.vector.processor"):
|
||||
await processor_task(
|
||||
worker_id=0,
|
||||
receive_stream=stream, # type: ignore[arg-type]
|
||||
shutdown_event=shutdown,
|
||||
nc_client=MagicMock(),
|
||||
user_id="alice",
|
||||
)
|
||||
|
||||
assert any("RuntimeError" in rec.message for rec in caplog.records)
|
||||
Reference in New Issue
Block a user