Skip to content
Merged
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
8 changes: 6 additions & 2 deletions src/specify_cli/authentication/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def _make_req(auth_headers: dict[str, str]) -> urllib.request.Request:
try:
return opener.open(req, timeout=timeout)
except urllib.error.HTTPError as exc:
exc.close()
if exc.code in (401, 403):
exc.close()
continue # try next entry
raise

Expand All @@ -273,4 +273,8 @@ def _make_req(auth_headers: dict[str, str]) -> urllib.request.Request:
# No auth is attached on this path, so the handler's host list is empty:
# here it runs redirect validation only, not auth stripping.
opener = urllib.request.build_opener(_StripAuthOnRedirect((), redirect_validator))
return opener.open(req, timeout=timeout)
try:
return opener.open(req, timeout=timeout)
except urllib.error.HTTPError as exc:
exc.close()
raise
19 changes: 19 additions & 0 deletions tests/specify_cli/authentication/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,25 @@ def _set_config(self, monkeypatch, entries):
from specify_cli.authentication import http as _mod
monkeypatch.setattr(_mod, "_config_override", entries)

@pytest.mark.parametrize("authenticated", [False, True])
def test_http_error_closes_response_body(self, monkeypatch, authenticated):
import urllib.error
from unittest.mock import patch
from specify_cli.authentication.http import open_url

entries = [_github_entry(token="tok")] if authenticated else []
self._set_config(monkeypatch, entries)
body = io.BytesIO(b"server error")
error = urllib.error.HTTPError("url", 500, "ISE", {}, body)
with patch("specify_cli.authentication.http.urllib.request.build_opener") as build_opener:
build_opener.return_value.open.side_effect = error
with pytest.raises(urllib.error.HTTPError) as raised:
open_url("https://github.com/org/repo")

assert raised.value is error
assert body.closed
build_opener.return_value.open.assert_called_once()

def test_500_raises_immediately(self, monkeypatch):
import urllib.error
from unittest.mock import MagicMock, patch
Expand Down
Loading