From 1188b72263c1445f751dfbd33eb11d128216c09e Mon Sep 17 00:00:00 2001 From: Mark Eaton Date: Wed, 11 Nov 2026 19:47:53 -0500 Subject: [PATCH] Test that a proxy error page for one repo doesn't crash the run --- tests/conftest.py | 11 +++++++++-- tests/test_stats.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e278fb0..69a90eb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,10 +15,17 @@ import language_stats def make_response(status, payload): """Build a real requests.Response so .ok, .json() and .raise_for_status() - behave exactly as they do against a live server.""" + behave exactly as they do against a live server. + + A bytes payload is sent as-is (e.g. an HTML error page from a proxy); + anything else is sent as JSON. + """ resp = requests.Response() resp.status_code = status - resp._content = json.dumps(payload).encode("utf-8") + if isinstance(payload, bytes): + resp._content = payload + else: + resp._content = json.dumps(payload).encode("utf-8") return resp diff --git a/tests/test_stats.py b/tests/test_stats.py index ed8447e..05b01e2 100644 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -86,6 +86,18 @@ def test_repo_whose_languages_are_forbidden_is_skipped_with_a_warning(gitea, cap ) +def test_repo_whose_languages_return_a_proxy_error_page_is_skipped(gitea, capsys): + # A reverse proxy in front of Gitea answers a slow request with an HTML + # page, not JSON; trying to parse it would crash the whole run. + gitea.add("GET", WEBSITE_LANGS, b"

504 Gateway Time-out

", 504) + gitea.add("GET", SCRIPTS_LANGS, {"Python": 2000}) + repos = [{"name": "website"}, {"name": "scripts"}] + assert aggregate_language_bytes(get_session(), repos) == {"Python": 2000} + assert ( + "Warning: Could not fetch languages for website: 504" in capsys.readouterr().err + ) + + def test_error_message_inside_a_success_response_is_skipped_with_a_warning( gitea, capsys ):