|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from aiohttp import ( |
| 4 | + ClientSession, |
| 5 | +) |
3 | 6 | import requests |
4 | 7 |
|
5 | 8 | from ens.utils import ( |
@@ -70,6 +73,44 @@ def raise_for_status(): pass # noqa: E704 |
70 | 73 | def json(): return {'not_data': OFFCHAIN_RESOLVER_DATA} # noqa: E704 |
71 | 74 |
|
72 | 75 |
|
| 76 | +class AsyncMockHttpSuccessResponse: |
| 77 | + status_code = 200 |
| 78 | + |
| 79 | + def __init__(self, request_type, *args, **_kwargs): |
| 80 | + # validate the expected urls |
| 81 | + if request_type == 'get': |
| 82 | + assert args[1] == EXPECTED_GET_URL |
| 83 | + elif request_type == 'post': |
| 84 | + assert args[1] == EXPECTED_POST_URL |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def raise_for_status(): pass # noqa: E704 |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + async def json(): return {'data': OFFCHAIN_RESOLVER_DATA} # noqa: E704 |
| 91 | + |
| 92 | + @property |
| 93 | + def status(self): |
| 94 | + return self.status_code |
| 95 | + |
| 96 | + |
| 97 | +class AsyncMockHttpBadFormatResponse: |
| 98 | + status_code = 200 |
| 99 | + |
| 100 | + def __init__(self, *args): |
| 101 | + assert args[1] == EXPECTED_GET_URL |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def raise_for_status(): pass # noqa: E704 |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + async def json(): return {'not_data': OFFCHAIN_RESOLVER_DATA} # noqa: E704' |
| 108 | + |
| 109 | + @property |
| 110 | + def status(self): |
| 111 | + return self.status_code |
| 112 | + |
| 113 | + |
73 | 114 | def test_offchain_resolution_with_get_request(ens, monkeypatch): |
74 | 115 | # mock GET response with real return data from 'offchainexample.eth' resolver |
75 | 116 | def mock_get(*args, **kwargs): |
@@ -123,3 +164,45 @@ def test_offchain_resolver_function_call_raises_with_ccip_read_disabled(ens, mon |
123 | 164 | ens_encode_name('offchainexample.eth'), |
124 | 165 | ENCODED_ADDR_CALLDATA, |
125 | 166 | ) |
| 167 | + |
| 168 | + |
| 169 | +@pytest.mark.asyncio |
| 170 | +async def test_async_offchain_resolution_with_get_request(async_ens, monkeypatch): |
| 171 | + # mock GET response with real return data from 'offchainexample.eth' resolver |
| 172 | + async def mock_get(*args, **kwargs): |
| 173 | + return AsyncMockHttpSuccessResponse('get', *args, **kwargs) |
| 174 | + |
| 175 | + monkeypatch.setattr(ClientSession, 'get', mock_get) |
| 176 | + |
| 177 | + assert await async_ens.address('offchainexample.eth') == EXPECTED_RESOLVED_ADDRESS |
| 178 | + |
| 179 | + |
| 180 | +@pytest.mark.asyncio |
| 181 | +async def test_async_offchain_resolution_with_post_request(async_ens, monkeypatch): |
| 182 | + # mock POST response with real return data from 'offchainexample.eth' resolver |
| 183 | + async def mock_post(*args, **kwargs): |
| 184 | + return AsyncMockHttpSuccessResponse('post', *args, **kwargs) |
| 185 | + |
| 186 | + monkeypatch.setattr(ClientSession, 'post', mock_post) |
| 187 | + |
| 188 | + assert await async_ens.address('offchainexample.eth') == EXPECTED_RESOLVED_ADDRESS |
| 189 | + |
| 190 | + |
| 191 | +@pytest.mark.asyncio |
| 192 | +async def test_async_offchain_resolution_raises_when_all_supplied_urls_fail(async_ens): |
| 193 | + # with no mocked responses, requests to all urls will fail |
| 194 | + with pytest.raises(Exception, match='Offchain lookup failed for supplied urls.'): |
| 195 | + await async_ens.address('offchainexample.eth') |
| 196 | + |
| 197 | + |
| 198 | +@pytest.mark.asyncio |
| 199 | +async def test_async_offchain_resolution_with_improperly_formatted_http_response(async_ens, |
| 200 | + monkeypatch): |
| 201 | + async def mock_get(*args, **_): |
| 202 | + return AsyncMockHttpBadFormatResponse(*args) |
| 203 | + |
| 204 | + monkeypatch.setattr(ClientSession, 'get', mock_get) |
| 205 | + with pytest.raises(ValidationError, match=( |
| 206 | + "Improperly formatted response for offchain lookup HTTP request - missing 'data' field." |
| 207 | + )): |
| 208 | + await async_ens.address('offchainexample.eth') |
0 commit comments