From 6fb79d68ddeef06cbb5a91103d645f708f591809 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Thu, 21 Sep 2023 08:42:07 +0200 Subject: [PATCH] Add BaseSnapExecutor test --- .../common/BaseSnapExecutor.test.browser.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts index 387bc7c2c7..4fd192f847 100644 --- a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts +++ b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts @@ -820,6 +820,55 @@ describe('BaseSnapExecutor', () => { }); }); + it('reports when outbound requests are made using fetch', async () => { + const CODE = ` + module.exports.onRpcRequest = () => fetch('https://metamask.io').then(res => res.text()); + `; + + const fetchSpy = spy(globalThis, 'fetch'); + + fetchSpy.mockImplementation(async () => { + return new Response('foo'); + }); + + const executor = new TestSnapExecutor(); + await executor.executeSnap(1, MOCK_SNAP_ID, CODE, ['fetch']); + + expect(await executor.readCommand()).toStrictEqual({ + jsonrpc: '2.0', + id: 1, + result: 'OK', + }); + + await executor.writeCommand({ + jsonrpc: '2.0', + id: 2, + method: 'snapRpc', + params: [ + MOCK_SNAP_ID, + HandlerType.OnRpcRequest, + MOCK_ORIGIN, + { jsonrpc: '2.0', method: '', params: [] }, + ], + }); + + expect(await executor.readCommand()).toStrictEqual({ + jsonrpc: '2.0', + method: 'OutboundRequest', + }); + + expect(await executor.readCommand()).toStrictEqual({ + jsonrpc: '2.0', + method: 'OutboundResponse', + }); + + expect(await executor.readCommand()).toStrictEqual({ + id: 2, + jsonrpc: '2.0', + result: 'foo', + }); + }); + it('notifies execution service of out of band errors via unhandledrejection', async () => { const CODE = ` module.exports.onRpcRequest = async () => 'foo';