|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { handleCustomNetworkLoadResource } from '../networkLoadResourceHandler.js'; |
| 3 | + |
| 4 | +function createConnectionSpy(): { |
| 5 | + connection: Parameters<typeof handleCustomNetworkLoadResource>[0]; |
| 6 | + sendMessage: ReturnType<typeof vi.fn>; |
| 7 | +} { |
| 8 | + const sendMessage = vi.fn(); |
| 9 | + |
| 10 | + return { |
| 11 | + connection: { |
| 12 | + debugger: { |
| 13 | + sendMessage, |
| 14 | + }, |
| 15 | + }, |
| 16 | + sendMessage, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('handleCustomNetworkLoadResource', () => { |
| 21 | + afterEach(() => { |
| 22 | + vi.restoreAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not intercept messages other than Network.loadNetworkResource', () => { |
| 26 | + const { connection, sendMessage } = createConnectionSpy(); |
| 27 | + |
| 28 | + const result = handleCustomNetworkLoadResource( |
| 29 | + connection, |
| 30 | + { |
| 31 | + id: 1, |
| 32 | + method: 'Runtime.enable', |
| 33 | + }, |
| 34 | + 'http://127.0.0.1:8081' |
| 35 | + ); |
| 36 | + |
| 37 | + expect(result).toBe(false); |
| 38 | + expect(sendMessage).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should not intercept malformed Network.loadNetworkResource messages', () => { |
| 42 | + const { connection, sendMessage } = createConnectionSpy(); |
| 43 | + |
| 44 | + const result = handleCustomNetworkLoadResource( |
| 45 | + connection, |
| 46 | + { |
| 47 | + id: 1, |
| 48 | + method: 'Network.loadNetworkResource', |
| 49 | + params: {}, |
| 50 | + }, |
| 51 | + 'http://127.0.0.1:8081' |
| 52 | + ); |
| 53 | + |
| 54 | + expect(result).toBe(false); |
| 55 | + expect(sendMessage).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not intercept same-origin Network.loadNetworkResource requests', () => { |
| 59 | + const { connection, sendMessage } = createConnectionSpy(); |
| 60 | + const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 61 | + |
| 62 | + const result = handleCustomNetworkLoadResource( |
| 63 | + connection, |
| 64 | + { |
| 65 | + id: 1, |
| 66 | + method: 'Network.loadNetworkResource', |
| 67 | + params: { |
| 68 | + url: 'http://127.0.0.1:8081/main.bundle.map', |
| 69 | + }, |
| 70 | + }, |
| 71 | + 'http://127.0.0.1:8081' |
| 72 | + ); |
| 73 | + |
| 74 | + expect(result).toBe(false); |
| 75 | + expect(fetchSpy).not.toHaveBeenCalled(); |
| 76 | + expect(sendMessage).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should intercept cross-origin Network.loadNetworkResource requests', async () => { |
| 80 | + const { connection, sendMessage } = createConnectionSpy(); |
| 81 | + vi.spyOn(globalThis, 'fetch').mockResolvedValue({ |
| 82 | + status: 200, |
| 83 | + headers: new Headers([['content-type', 'application/json']]), |
| 84 | + text: () => Promise.resolve('{"ok":true}'), |
| 85 | + } as unknown as Response); |
| 86 | + |
| 87 | + const handled = handleCustomNetworkLoadResource( |
| 88 | + connection, |
| 89 | + { |
| 90 | + id: 7, |
| 91 | + method: 'Network.loadNetworkResource', |
| 92 | + params: { |
| 93 | + url: 'http://10.10.10.10:9000/remote.bundle.map', |
| 94 | + }, |
| 95 | + }, |
| 96 | + 'http://127.0.0.1:8081' |
| 97 | + ); |
| 98 | + |
| 99 | + expect(handled).toBe(true); |
| 100 | + |
| 101 | + await vi.waitFor(() => { |
| 102 | + expect(sendMessage).toHaveBeenCalledWith({ |
| 103 | + id: 7, |
| 104 | + result: { |
| 105 | + resource: { |
| 106 | + success: true, |
| 107 | + httpStatusCode: 200, |
| 108 | + headers: { |
| 109 | + 'content-type': 'application/json', |
| 110 | + }, |
| 111 | + content: Buffer.from('{"ok":true}').toString('base64'), |
| 112 | + base64Encoded: true, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should return a CDP failure response when resource fetch fails', async () => { |
| 120 | + const { connection, sendMessage } = createConnectionSpy(); |
| 121 | + vi.spyOn(globalThis, 'fetch').mockRejectedValue( |
| 122 | + new Error('network failed') |
| 123 | + ); |
| 124 | + |
| 125 | + const handled = handleCustomNetworkLoadResource( |
| 126 | + connection, |
| 127 | + { |
| 128 | + id: 9, |
| 129 | + method: 'Network.loadNetworkResource', |
| 130 | + params: { |
| 131 | + url: 'http://10.10.10.10:9000/remote.bundle.map', |
| 132 | + }, |
| 133 | + }, |
| 134 | + 'http://127.0.0.1:8081' |
| 135 | + ); |
| 136 | + |
| 137 | + expect(handled).toBe(true); |
| 138 | + |
| 139 | + await vi.waitFor(() => { |
| 140 | + expect(sendMessage).toHaveBeenCalledWith({ |
| 141 | + id: 9, |
| 142 | + result: { |
| 143 | + resource: { |
| 144 | + success: false, |
| 145 | + netErrorName: 'net::ERR_FAILED', |
| 146 | + netError: -2, |
| 147 | + httpStatusCode: 500, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments