regex-based _strip_etag with weak-marker test
This commit is contained in:
parent
9ed24ad53e
commit
57e89426ea
2 changed files with 35 additions and 2 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import re
|
||||||
from collections.abc import AsyncIterator
|
from collections.abc import AsyncIterator
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from urllib.parse import unquote, urljoin, urlparse
|
from urllib.parse import unquote, urljoin, urlparse
|
||||||
|
|
@ -65,10 +66,20 @@ class _PropfindEntry:
|
||||||
self.content_type = content_type
|
self.content_type = content_type
|
||||||
|
|
||||||
|
|
||||||
|
# Matches an ETag value with optional leading whitespace, optional weak
|
||||||
|
# marker ``W/``, optional surrounding double quotes, and optional trailing
|
||||||
|
# whitespace. The non-greedy capture pulls out just the opaque inner value.
|
||||||
|
_ETAG_RE = re.compile(r'^\s*(?:W/)?"?(.*?)"?\s*$')
|
||||||
|
|
||||||
|
|
||||||
def _strip_etag(value: str | None) -> str | None:
|
def _strip_etag(value: str | None) -> str | None:
|
||||||
|
"""Return the opaque part of an ETag header value (or ``getetag`` element):
|
||||||
|
strip surrounding whitespace, the optional ``W/`` weak marker, and
|
||||||
|
optional surrounding double quotes. Returns ``None`` for empty input."""
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
cleaned = value.strip().strip('"').strip("W/").strip().strip('"')
|
match = _ETAG_RE.match(value)
|
||||||
|
cleaned = match.group(1) if match else value.strip()
|
||||||
return cleaned or None
|
return cleaned or None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,29 @@ import httpx
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from haiku.rag.ingester.sources.base import SourceEventKind
|
from haiku.rag.ingester.sources.base import SourceEventKind
|
||||||
from haiku.rag.ingester.sources.webdav import WebDAVSource
|
from haiku.rag.ingester.sources.webdav import WebDAVSource, _strip_etag
|
||||||
|
|
||||||
|
|
||||||
|
def test_strip_etag_strong_quoted():
|
||||||
|
assert _strip_etag('"abc123"') == "abc123"
|
||||||
|
|
||||||
|
|
||||||
|
def test_strip_etag_weak_marker():
|
||||||
|
assert _strip_etag('W/"abc123"') == "abc123"
|
||||||
|
|
||||||
|
|
||||||
|
def test_strip_etag_unquoted():
|
||||||
|
assert _strip_etag("abc123") == "abc123"
|
||||||
|
|
||||||
|
|
||||||
|
def test_strip_etag_whitespace():
|
||||||
|
assert _strip_etag(' W/"abc" ') == "abc"
|
||||||
|
|
||||||
|
|
||||||
|
def test_strip_etag_empty_returns_none():
|
||||||
|
assert _strip_etag("") is None
|
||||||
|
assert _strip_etag('""') is None
|
||||||
|
assert _strip_etag(None) is None
|
||||||
|
|
||||||
|
|
||||||
def _transport(handler) -> httpx.MockTransport:
|
def _transport(handler) -> httpx.MockTransport:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue