|
| 1 | +import { Readable } from 'node:stream' |
1 | 2 | import { describe, expect, it, vi } from 'vitest' |
2 | | -import { formatHttpRequestError } from '../http' |
| 3 | +import { formatHttpRequestError, registerHttpHandlers } from '../http' |
| 4 | + |
| 5 | +const { AgentMock, handleMock, requestMock } = vi.hoisted(() => ({ |
| 6 | + AgentMock: class { |
| 7 | + options: unknown |
| 8 | + |
| 9 | + constructor(options: unknown) { |
| 10 | + this.options = options |
| 11 | + } |
| 12 | + }, |
| 13 | + handleMock: vi.fn(), |
| 14 | + requestMock: vi.fn(), |
| 15 | +})) |
3 | 16 |
|
4 | 17 | vi.mock('electron', () => ({ |
5 | 18 | ipcMain: { |
6 | | - handle: vi.fn(), |
| 19 | + handle: handleMock, |
7 | 20 | }, |
8 | 21 | })) |
9 | 22 |
|
| 23 | +vi.mock('undici', () => ({ |
| 24 | + Agent: vi.fn(AgentMock), |
| 25 | + request: requestMock, |
| 26 | +})) |
| 27 | + |
10 | 28 | vi.mock('../../../storage', () => ({ |
11 | | - useHttpStorage: vi.fn(), |
| 29 | + useHttpStorage: () => ({ |
| 30 | + environments: { |
| 31 | + getEnvironments: () => [], |
| 32 | + }, |
| 33 | + history: { |
| 34 | + appendEntry: vi.fn(), |
| 35 | + }, |
| 36 | + }), |
12 | 37 | })) |
13 | 38 |
|
14 | 39 | describe('formatHttpRequestError', () => { |
@@ -67,3 +92,47 @@ describe('formatHttpRequestError', () => { |
67 | 92 | ) |
68 | 93 | }) |
69 | 94 | }) |
| 95 | + |
| 96 | +describe('registerHttpHandlers', () => { |
| 97 | + it('uses an insecure dispatcher when certificate verification is skipped', async () => { |
| 98 | + requestMock.mockResolvedValueOnce({ |
| 99 | + body: Readable.from([]), |
| 100 | + headers: {}, |
| 101 | + statusCode: 200, |
| 102 | + }) |
| 103 | + |
| 104 | + registerHttpHandlers() |
| 105 | + const handler = handleMock.mock.calls.find( |
| 106 | + ([channel]) => channel === 'spaces:http:execute', |
| 107 | + )?.[1] |
| 108 | + |
| 109 | + await handler(null, { |
| 110 | + environmentId: null, |
| 111 | + request: { |
| 112 | + auth: { type: 'none' }, |
| 113 | + body: null, |
| 114 | + bodyType: 'none', |
| 115 | + formData: [], |
| 116 | + headers: [], |
| 117 | + method: 'GET', |
| 118 | + query: [], |
| 119 | + url: 'https://example.test', |
| 120 | + }, |
| 121 | + requestId: null, |
| 122 | + skipCertificateVerification: true, |
| 123 | + }) |
| 124 | + |
| 125 | + expect(requestMock).toHaveBeenCalledWith( |
| 126 | + 'https://example.test/', |
| 127 | + expect.objectContaining({ |
| 128 | + dispatcher: expect.objectContaining({ |
| 129 | + options: { |
| 130 | + connect: { |
| 131 | + rejectUnauthorized: false, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }), |
| 135 | + }), |
| 136 | + ) |
| 137 | + }) |
| 138 | +}) |
0 commit comments