From b0d4a00c0e17f91c40a4a042a9729d097d2015ad Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 06:31:42 +0000 Subject: [PATCH] fix: accept all 2xx status codes in check_for_status (issue #205) Agent-Logs-Url: https://github.com/FlexMeasures/flexmeasures-client/sessions/fc24eb27-c607-47e2-8d1d-916f5f26ceaa Co-authored-by: Flix6x <30658763+Flix6x@users.noreply.github.com> --- src/flexmeasures_client/response_handling.py | 17 ++++++++-- tests/client/test_init.py | 34 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/flexmeasures_client/response_handling.py b/src/flexmeasures_client/response_handling.py index 34f26d96..b665c702 100644 --- a/src/flexmeasures_client/response_handling.py +++ b/src/flexmeasures_client/response_handling.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import logging from typing import TYPE_CHECKING from aiohttp import ContentTypeError @@ -11,6 +12,8 @@ if TYPE_CHECKING: # Only imports the below statements during type checking from flexmeasures_client.client import FlexMeasuresClient +logger = logging.getLogger(__name__) + async def check_response( self: FlexMeasuresClient, response, polling_step: int, reauth_once: bool, url: URL @@ -92,6 +95,16 @@ def check_content_type(response): def check_for_status(status, expected_status): - """Check if status is expected""" - if status != expected_status: + """Check if status is an acceptable 2xx status code. + + Accepts any 2xx status code as success. Logs at INFO level if the status + differs from the expected status but is still a 2xx success code. + Raises ValueError for non-2xx status codes. + """ + if 200 <= status < 300: + if status != expected_status: + logger.info( + f"Received HTTP status {status} instead of expected {expected_status}." + ) + else: raise ValueError(f"Request failed with status code {status}") diff --git a/tests/client/test_init.py b/tests/client/test_init.py index e60b0225..5e9202d8 100644 --- a/tests/client/test_init.py +++ b/tests/client/test_init.py @@ -527,3 +527,37 @@ def test_check_for_status_failure(): """check_for_status raises ValueError on wrong status.""" with pytest.raises(ValueError, match="Request failed with status code 400"): check_for_status(400, 200) + + +def test_check_for_status_exact_match(caplog): + """check_for_status does not raise and does not log when status matches expected.""" + import logging + + with caplog.at_level(logging.INFO, logger="flexmeasures_client.response_handling"): + check_for_status(200, 200) + assert caplog.records == [] + + +def test_check_for_status_different_2xx_logs_info(caplog): + """check_for_status logs at INFO level when status is 2xx but differs from expected.""" + import logging + + with caplog.at_level(logging.INFO, logger="flexmeasures_client.response_handling"): + check_for_status(202, 200) + assert len(caplog.records) == 1 + assert caplog.records[0].levelno == logging.INFO + assert "202" in caplog.records[0].message + assert "200" in caplog.records[0].message + + +@pytest.mark.parametrize("status", [200, 201, 202, 204, 299]) +def test_check_for_status_various_2xx_pass(status): + """check_for_status does not raise for any 2xx status code, even when it differs from expected.""" + check_for_status(status, 200) + + +@pytest.mark.parametrize("status", [199, 300, 400, 404, 500, 503]) +def test_check_for_status_non_2xx_raises(status): + """check_for_status raises ValueError for non-2xx status codes.""" + with pytest.raises(ValueError, match=f"Request failed with status code {status}"): + check_for_status(status, 200)