|
| 1 | +from http import HTTPStatus |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from ipinfo.details import Details |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.asyncio |
| 9 | +async def test_middleware_appends_ip_info(async_client, ipinfo_async_plus_middleware): |
| 10 | + with mock.patch("ipinfo.AsyncHandlerPlus.getDetails") as mocked_getDetails: |
| 11 | + mocked_getDetails.return_value = Details({"ip": "127.0.0.1"}) |
| 12 | + res = await async_client.get("/test_view/") |
| 13 | + assert res.status_code == HTTPStatus.OK |
| 14 | + assert b"For testing: 127.0.0.1" in res.content |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.asyncio |
| 18 | +async def test_middleware_filters(async_client, ipinfo_async_plus_middleware): |
| 19 | + res = await async_client.get("/test_view/", USER_AGENT="some bot") |
| 20 | + assert res.status_code == HTTPStatus.OK |
| 21 | + assert b"Request filtered." in res.content |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_middleware_behind_proxy(async_client, ipinfo_async_plus_middleware): |
| 26 | + with mock.patch("ipinfo.AsyncHandlerPlus.getDetails") as mocked_getDetails: |
| 27 | + mocked_getDetails.return_value = Details({"ip": "93.44.186.197"}) |
| 28 | + res = await async_client.get("/test_view/", X_FORWARDED_FOR="93.44.186.197") |
| 29 | + |
| 30 | + mocked_getDetails.assert_called_once_with("93.44.186.197") |
| 31 | + assert res.status_code == HTTPStatus.OK |
| 32 | + assert b"For testing: 93.44.186.197" in res.content |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.asyncio |
| 36 | +async def test_middleware_not_behind_proxy(async_client, ipinfo_async_plus_middleware): |
| 37 | + with mock.patch("ipinfo.AsyncHandlerPlus.getDetails") as mocked_getDetails: |
| 38 | + mocked_getDetails.return_value = Details({"ip": "127.0.0.1"}) |
| 39 | + res = await async_client.get("/test_view/") |
| 40 | + |
| 41 | + mocked_getDetails.assert_called_once_with("127.0.0.1") |
| 42 | + assert res.status_code == HTTPStatus.OK |
| 43 | + assert b"For testing: 127.0.0.1" in res.content |
0 commit comments