@@ -94,6 +94,23 @@ def status(self):
9494 return self .status_code
9595
9696
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+
97114def test_offchain_resolution_with_get_request (ens , monkeypatch ):
98115 # mock GET response with real return data from 'offchainexample.eth' resolver
99116 def mock_get (* args , ** kwargs ):
@@ -169,3 +186,23 @@ async def mock_post(*args, **kwargs):
169186 monkeypatch .setattr (ClientSession , 'post' , mock_post )
170187
171188 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