"""Tests for boxtime.http.http — async_http_get."""

import aiohttp
from aioresponses import aioresponses

from boxtime.http.http import async_http_get

URL = "http://test:9053/info"


async def test_success():
    with aioresponses() as m:
        m.get(URL, payload={"key": "value"})
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(session, URL)
    assert result == {"key": "value"}


async def test_connection_error():
    with aioresponses() as m:
        m.get(URL, exception=aiohttp.ClientConnectionError("refused"))
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(session, URL)
    assert result is None


async def test_http_error_4xx():
    with aioresponses() as m:
        m.get(URL, status=404)
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(session, URL)
    assert result is None


async def test_http_error_5xx():
    with aioresponses() as m:
        m.get(URL, status=500)
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(session, URL)
    assert result is None


async def test_timeout():
    with aioresponses() as m:
        m.get(URL, exception=TimeoutError())
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(session, URL)
    assert result is None


async def test_generic_client_error():
    with aioresponses() as m:
        m.get(URL, exception=aiohttp.ClientError("generic"))
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(session, URL)
    assert result is None


async def test_passes_params():
    url_with_params = f"{URL}?offset=0&limit=10"
    with aioresponses() as m:
        m.get(url_with_params, payload={"ok": True})
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(
                session, URL, params={"offset": 0, "limit": 10}
            )
    assert result == {"ok": True}


async def test_passes_headers():
    with aioresponses() as m:
        m.get(URL, payload={"ok": True})
        async with aiohttp.ClientSession() as session:
            result = await async_http_get(
                session, URL, headers={"X-Custom": "test"}
            )
    assert result == {"ok": True}
