|
1 |
| -import { describe, it, expect } from 'vitest' |
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' |
| 2 | +import { parseCommandLineArgs, connectToRemoteServer } from './utils' |
| 3 | +import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js' |
2 | 4 |
|
3 | 5 | // All sanitizeUrl tests have been moved to the strict-url-sanitise package
|
| 6 | + |
| 7 | +describe('parseCommandLineArgs', () => { |
| 8 | + it('should parse --insecure flag correctly', async () => { |
| 9 | + const args = ['https://example.com', '--insecure'] |
| 10 | + const result = await parseCommandLineArgs(args, 'Test usage') |
| 11 | + |
| 12 | + expect(result.insecure).toBe(true) |
| 13 | + expect(result.serverUrl).toBe('https://example.com') |
| 14 | + }) |
| 15 | + |
| 16 | + it('should default insecure to false when not provided', async () => { |
| 17 | + const args = ['https://example.com'] |
| 18 | + const result = await parseCommandLineArgs(args, 'Test usage') |
| 19 | + |
| 20 | + expect(result.insecure).toBe(false) |
| 21 | + expect(result.serverUrl).toBe('https://example.com') |
| 22 | + }) |
| 23 | + |
| 24 | + it('should work with multiple flags including --insecure', async () => { |
| 25 | + const args = ['https://example.com', '--debug', '--insecure', '--allow-http'] |
| 26 | + const result = await parseCommandLineArgs(args, 'Test usage') |
| 27 | + |
| 28 | + expect(result.insecure).toBe(true) |
| 29 | + expect(result.debug).toBe(true) |
| 30 | + expect(result.serverUrl).toBe('https://example.com') |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +describe('connectToRemoteServer insecure flag', () => { |
| 35 | + let originalTlsReject: string | undefined |
| 36 | + let mockExit: any |
| 37 | + let mockLog: any |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + // Save original environment variable |
| 41 | + originalTlsReject = process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 42 | + |
| 43 | + // Mock process.exit to prevent actual exits during tests |
| 44 | + mockExit = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 45 | + throw new Error('process.exit called') |
| 46 | + }) |
| 47 | + |
| 48 | + // Mock console.error to capture log messages |
| 49 | + mockLog = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 50 | + }) |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + // Restore original environment variable |
| 54 | + if (originalTlsReject !== undefined) { |
| 55 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalTlsReject |
| 56 | + } else { |
| 57 | + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 58 | + } |
| 59 | + |
| 60 | + // Restore mocks |
| 61 | + mockExit.mockRestore() |
| 62 | + mockLog.mockRestore() |
| 63 | + }) |
| 64 | + |
| 65 | + it('should fail when --insecure conflicts with NODE_TLS_REJECT_UNAUTHORIZED=1', async () => { |
| 66 | + // Set environment variable to enable cert verification (conflicts with --insecure) |
| 67 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1' |
| 68 | + |
| 69 | + const mockAuthProvider = {} as OAuthClientProvider |
| 70 | + const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) |
| 71 | + |
| 72 | + await expect( |
| 73 | + connectToRemoteServer( |
| 74 | + null, |
| 75 | + 'https://example.com', |
| 76 | + mockAuthProvider, |
| 77 | + {}, |
| 78 | + mockAuthInitializer, |
| 79 | + 'http-first', |
| 80 | + new Set(), |
| 81 | + true // insecure flag |
| 82 | + ) |
| 83 | + ).rejects.toThrow('process.exit called') |
| 84 | + |
| 85 | + // Check that error message was logged |
| 86 | + expect(mockLog).toHaveBeenCalledWith( |
| 87 | + expect.stringContaining('Cannot use --insecure flag while NODE_TLS_REJECT_UNAUTHORIZED') |
| 88 | + ) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should fail when --insecure conflicts with NODE_TLS_REJECT_UNAUTHORIZED=true', async () => { |
| 92 | + // Set environment variable to enable cert verification (conflicts with --insecure) |
| 93 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = 'true' |
| 94 | + |
| 95 | + const mockAuthProvider = {} as OAuthClientProvider |
| 96 | + const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) |
| 97 | + |
| 98 | + await expect( |
| 99 | + connectToRemoteServer( |
| 100 | + null, |
| 101 | + 'https://example.com', |
| 102 | + mockAuthProvider, |
| 103 | + {}, |
| 104 | + mockAuthInitializer, |
| 105 | + 'http-first', |
| 106 | + new Set(), |
| 107 | + true // insecure flag |
| 108 | + ) |
| 109 | + ).rejects.toThrow('process.exit called') |
| 110 | + |
| 111 | + // Check that error message was logged |
| 112 | + expect(mockLog).toHaveBeenCalledWith( |
| 113 | + expect.stringContaining('Cannot use --insecure flag while NODE_TLS_REJECT_UNAUTHORIZED') |
| 114 | + ) |
| 115 | + }) |
| 116 | + |
| 117 | + it('should proceed when --insecure is compatible with NODE_TLS_REJECT_UNAUTHORIZED=0', async () => { |
| 118 | + // Set environment variable to disable cert verification (compatible with --insecure) |
| 119 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' |
| 120 | + |
| 121 | + const mockAuthProvider = {} as OAuthClientProvider |
| 122 | + const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) |
| 123 | + |
| 124 | + // This test will likely fail due to network issues, but we're just testing that |
| 125 | + // the conflict detection doesn't trigger process.exit |
| 126 | + try { |
| 127 | + await connectToRemoteServer( |
| 128 | + null, |
| 129 | + 'https://example.com', |
| 130 | + mockAuthProvider, |
| 131 | + {}, |
| 132 | + mockAuthInitializer, |
| 133 | + 'http-first', |
| 134 | + new Set(), |
| 135 | + true // insecure flag |
| 136 | + ) |
| 137 | + } catch (error) { |
| 138 | + // We expect this to fail due to network/connection issues, not conflict detection |
| 139 | + expect(error).not.toEqual(new Error('process.exit called')) |
| 140 | + } |
| 141 | + |
| 142 | + // Should not have called process.exit |
| 143 | + expect(mockExit).not.toHaveBeenCalled() |
| 144 | + }) |
| 145 | + |
| 146 | + it('should set and restore NODE_TLS_REJECT_UNAUTHORIZED when unset with --insecure', async () => { |
| 147 | + // Ensure environment variable is unset |
| 148 | + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 149 | + |
| 150 | + const mockAuthProvider = {} as OAuthClientProvider |
| 151 | + const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) |
| 152 | + |
| 153 | + // This test will likely fail due to network issues, but we're testing env var handling |
| 154 | + try { |
| 155 | + await connectToRemoteServer( |
| 156 | + null, |
| 157 | + 'https://example.com', |
| 158 | + mockAuthProvider, |
| 159 | + {}, |
| 160 | + mockAuthInitializer, |
| 161 | + 'http-first', |
| 162 | + new Set(), |
| 163 | + true // insecure flag |
| 164 | + ) |
| 165 | + } catch (error) { |
| 166 | + // We expect this to fail due to network/connection issues |
| 167 | + expect(error).not.toEqual(new Error('process.exit called')) |
| 168 | + } |
| 169 | + |
| 170 | + // Environment variable should be restored to unset state |
| 171 | + expect(process.env.NODE_TLS_REJECT_UNAUTHORIZED).toBeUndefined() |
| 172 | + |
| 173 | + // Should not have called process.exit |
| 174 | + expect(mockExit).not.toHaveBeenCalled() |
| 175 | + |
| 176 | + // Should have logged that we're setting the env var |
| 177 | + expect(mockLog).toHaveBeenCalledWith( |
| 178 | + expect.stringContaining('Setting NODE_TLS_REJECT_UNAUTHORIZED=0 for --insecure connection') |
| 179 | + ) |
| 180 | + }) |
| 181 | + |
| 182 | + it('should not modify NODE_TLS_REJECT_UNAUTHORIZED when --insecure is false', async () => { |
| 183 | + // Set a specific value |
| 184 | + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1' |
| 185 | + const originalValue = process.env.NODE_TLS_REJECT_UNAUTHORIZED |
| 186 | + |
| 187 | + const mockAuthProvider = {} as OAuthClientProvider |
| 188 | + const mockAuthInitializer = async () => ({ waitForAuthCode: async () => 'test', skipBrowserAuth: false }) |
| 189 | + |
| 190 | + // This test will likely fail due to network issues |
| 191 | + try { |
| 192 | + await connectToRemoteServer( |
| 193 | + null, |
| 194 | + 'https://example.com', |
| 195 | + mockAuthProvider, |
| 196 | + {}, |
| 197 | + mockAuthInitializer, |
| 198 | + 'http-first', |
| 199 | + new Set(), |
| 200 | + false // insecure flag is false |
| 201 | + ) |
| 202 | + } catch (error) { |
| 203 | + // We expect this to fail due to network/connection issues |
| 204 | + expect(error).not.toEqual(new Error('process.exit called')) |
| 205 | + } |
| 206 | + |
| 207 | + // Environment variable should remain unchanged |
| 208 | + expect(process.env.NODE_TLS_REJECT_UNAUTHORIZED).toBe(originalValue) |
| 209 | + |
| 210 | + // Should not have called process.exit |
| 211 | + expect(mockExit).not.toHaveBeenCalled() |
| 212 | + }) |
| 213 | +}) |
0 commit comments