|
1 | 1 | # pylint: disable=redefined-outer-name |
2 | 2 | # pylint: disable=unused-argument |
3 | 3 |
|
4 | | -from asyncio import BaseEventLoop |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import AsyncIterable |
5 | 6 |
|
6 | 7 | import pytest |
7 | | -from aiohttp import web |
8 | | -from aiohttp.test_utils import TestClient |
| 8 | +from aiohttp import ClientResponse, ClientSession |
| 9 | +from aioresponses import aioresponses |
9 | 10 | from simcore_sdk.node_ports_common.file_io_utils import ( |
10 | 11 | ExtendedClientResponseError, |
| 12 | + _check_for_aws_http_errors, |
11 | 13 | _raise_for_status, |
12 | 14 | ) |
13 | 15 |
|
14 | | -TEST_ERROR = "OPSIE there was an error here" |
| 16 | +A_TEST_ROUTE = "http://a-fake-address:1249/test-route" |
15 | 17 |
|
16 | 18 |
|
17 | 19 | @pytest.fixture |
18 | | -def test_client(loop: BaseEventLoop, aiohttp_client) -> TestClient: |
19 | | - async def raise_error(request): |
20 | | - return web.Response(body=TEST_ERROR, status=web.HTTPBadRequest.status_code) |
| 20 | +async def client_session() -> AsyncIterable[ClientSession]: |
| 21 | + async with ClientSession() as session: |
| 22 | + yield session |
21 | 23 |
|
22 | | - app = web.Application() |
23 | | - app.router.add_get("/", raise_error) |
24 | | - return loop.run_until_complete(aiohttp_client(app)) |
25 | 24 |
|
| 25 | +async def test_raise_for_status( |
| 26 | + aioresponses_mocker: aioresponses, client_session: ClientSession |
| 27 | +): |
| 28 | + aioresponses_mocker.get( |
| 29 | + A_TEST_ROUTE, body="OPSIE there was an error here", status=400 |
| 30 | + ) |
26 | 31 |
|
27 | | -async def test_raise_for_status(test_client: TestClient): |
28 | | - resp = await test_client.get("/") |
29 | | - with pytest.raises(ExtendedClientResponseError) as exe_info: |
30 | | - await _raise_for_status(resp) |
31 | | - assert TEST_ERROR in f"{exe_info.value}" |
| 32 | + async with client_session.get(A_TEST_ROUTE) as resp: |
| 33 | + assert isinstance(resp, ClientResponse) |
| 34 | + |
| 35 | + with pytest.raises(ExtendedClientResponseError) as exe_info: |
| 36 | + await _raise_for_status(resp) |
| 37 | + assert "OPSIE there was an error here" in f"{exe_info.value}" |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class _TestParams: |
| 42 | + will_retry: bool |
| 43 | + status_code: int |
| 44 | + body: str = "" |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "test_params", |
| 49 | + [ |
| 50 | + _TestParams( |
| 51 | + will_retry=True, |
| 52 | + status_code=400, |
| 53 | + body='<?xml version="1.0" encoding="UTF-8"?><Error><Code>RequestTimeout</Code>' |
| 54 | + "<Message>Your socket connection to the server was not read from or written to within " |
| 55 | + "the timeout period. Idle connections will be closed.</Message>" |
| 56 | + "<RequestId>7EE901348D6C6812</RequestId><HostId>" |
| 57 | + "FfQE7jdbUt39E6mcQq/" |
| 58 | + "ZeNR52ghjv60fccNT4gCE4IranXjsGLG+L6FUyiIxx1tAuXL9xtz2NAY7ZlbzMTm94fhY3TBiCBmf" |
| 59 | + "</HostId></Error>", |
| 60 | + ), |
| 61 | + _TestParams(will_retry=True, status_code=500), |
| 62 | + _TestParams(will_retry=True, status_code=503), |
| 63 | + _TestParams(will_retry=False, status_code=400), |
| 64 | + _TestParams(will_retry=False, status_code=200), |
| 65 | + _TestParams(will_retry=False, status_code=399), |
| 66 | + ], |
| 67 | +) |
| 68 | +async def test_check_for_aws_http_errors( |
| 69 | + aioresponses_mocker: aioresponses, |
| 70 | + client_session: ClientSession, |
| 71 | + test_params: _TestParams, |
| 72 | +): |
| 73 | + aioresponses_mocker.get( |
| 74 | + A_TEST_ROUTE, body=test_params.body, status=test_params.status_code |
| 75 | + ) |
| 76 | + |
| 77 | + async with client_session.get(A_TEST_ROUTE) as resp: |
| 78 | + try: |
| 79 | + await _raise_for_status(resp) |
| 80 | + except ExtendedClientResponseError as exception: |
| 81 | + assert _check_for_aws_http_errors(exception) is test_params.will_retry |
0 commit comments