Skip to content

Commit 68fa166

Browse files
committed
fix: treat redirects as reachable in connection check
- Enable follow_redirects in httpx client for HEAD requests - Accept 2xx and 3xx status codes as successful connection - Add test for redirect handling in check_connection Generated with Ripperdoc Co-Authored-By: Ripperdoc
1 parent 36dc7c4 commit 68fa166

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/olah/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@
6767

6868
async def check_connection(url: str) -> bool:
6969
try:
70-
async with httpx.AsyncClient() as client:
70+
async with httpx.AsyncClient(follow_redirects=True) as client:
7171
response = await client.request(
7272
method="HEAD",
7373
url=url,
7474
timeout=10,
7575
)
76-
return response.status_code == 200
76+
return 200 <= response.status_code < 400
7777
except httpx.TimeoutException:
7878
return False
7979

tests/test_server_layers.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,39 @@ async def fake_ensure_repo_visibility(app, repo, authorization):
418418
)
419419

420420
assert response.status_code == 401
421+
422+
423+
@pytest.mark.asyncio
424+
async def test_check_connection_treats_redirects_as_reachable(monkeypatch):
425+
import importlib
426+
427+
server_module = importlib.import_module("olah.server")
428+
captured = {}
429+
430+
class FakeClient:
431+
def __init__(self, *args, **kwargs):
432+
captured["follow_redirects"] = kwargs.get("follow_redirects")
433+
434+
async def __aenter__(self):
435+
return self
436+
437+
async def __aexit__(self, exc_type, exc, tb):
438+
return False
439+
440+
async def request(self, method, url, timeout):
441+
captured["request"] = (method, url, timeout)
442+
return SimpleNamespace(status_code=307)
443+
444+
monkeypatch.setattr(server_module.httpx, "AsyncClient", FakeClient)
445+
446+
ok = await server_module.check_connection(
447+
"https://huggingface.co/datasets/Salesforce/wikitext/resolve/main/.gitattributes"
448+
)
449+
450+
assert ok is True
451+
assert captured["follow_redirects"] is True
452+
assert captured["request"] == (
453+
"HEAD",
454+
"https://huggingface.co/datasets/Salesforce/wikitext/resolve/main/.gitattributes",
455+
10,
456+
)

0 commit comments

Comments
 (0)