From b9149d6e68116a9525e3495332928b455e09b7b1 Mon Sep 17 00:00:00 2001 From: Mathieu Virbel Date: Thu, 2 Nov 2023 21:00:14 +0100 Subject: [PATCH] server: ensure retry works even with 303 redirection --- server/tests/test_retry_decorator.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/server/tests/test_retry_decorator.py b/server/tests/test_retry_decorator.py index 22729eac..c60a490f 100644 --- a/server/tests/test_retry_decorator.py +++ b/server/tests/test_retry_decorator.py @@ -1,3 +1,4 @@ +import asyncio import pytest import httpx from reflector.utils.retry import ( @@ -8,6 +9,31 @@ from reflector.utils.retry import ( ) +@pytest.mark.asyncio +async def test_retry_redirect(httpx_mock): + async def custom_response(request: httpx.Request): + if request.url.path == "/hello": + await asyncio.sleep(1) + return httpx.Response( + status_code=303, headers={"location": "https://test_url/redirected"} + ) + elif request.url.path == "/redirected": + return httpx.Response(status_code=200, json={"hello": "world"}) + else: + raise Exception("Unexpected path") + + httpx_mock.add_callback(custom_response) + async with httpx.AsyncClient() as client: + # timeout should not triggered, as it will end up ok + # even though the first request is a 303 and took more that 0.5 + resp = await retry(client.get)( + "https://test_url/hello", + retry_timeout=0.5, + follow_redirects=True, + ) + assert resp.json() == {"hello": "world"} + + @pytest.mark.asyncio async def test_retry_httpx(httpx_mock): # this code should be force a retry