|
1 |
| -import { describe, it, expect, vi } from 'vitest' |
2 |
| -import { parseCommandLineArgs, shouldIncludeTool, mcpProxy } from './utils' |
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 2 | +import { parseCommandLineArgs, shouldIncludeTool, mcpProxy, setupOAuthCallbackServerWithLongPoll } from './utils' |
3 | 3 | import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
|
| 4 | +import { EventEmitter } from 'events' |
| 5 | +import express from 'express' |
4 | 6 |
|
5 | 7 | // All sanitizeUrl tests have been moved to the strict-url-sanitise package
|
6 | 8 |
|
@@ -322,6 +324,100 @@ describe('Feature: Command Line Arguments Parsing', () => {
|
322 | 324 | expect(result.transportStrategy).toBe('sse-only')
|
323 | 325 | expect(result.ignoredTools).toEqual(['tool1', 'tool2'])
|
324 | 326 | })
|
| 327 | + |
| 328 | + it('Scenario: Use default auth timeout when not specified', async () => { |
| 329 | + // Given command line arguments without --auth-timeout flag |
| 330 | + const args = ['https://example.com/sse'] |
| 331 | + const usage = 'test usage' |
| 332 | + |
| 333 | + // When parsing the command line arguments |
| 334 | + const result = await parseCommandLineArgs(args, usage) |
| 335 | + |
| 336 | + // Then the default auth timeout should be 30000ms |
| 337 | + expect(result.authTimeoutMs).toBe(30000) |
| 338 | + }) |
| 339 | + |
| 340 | + it('Scenario: Parse valid auth timeout in seconds and convert to milliseconds', async () => { |
| 341 | + // Given command line arguments with valid --auth-timeout |
| 342 | + const args = ['https://example.com/sse', '--auth-timeout', '60'] |
| 343 | + const usage = 'test usage' |
| 344 | + |
| 345 | + // When parsing the command line arguments |
| 346 | + const result = await parseCommandLineArgs(args, usage) |
| 347 | + |
| 348 | + // Then the timeout should be converted to milliseconds |
| 349 | + expect(result.authTimeoutMs).toBe(60000) |
| 350 | + }) |
| 351 | + |
| 352 | + it('Scenario: Use default timeout when invalid auth timeout value is provided', async () => { |
| 353 | + // Given command line arguments with invalid --auth-timeout value |
| 354 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 355 | + const args = ['https://example.com/sse', '--auth-timeout', 'invalid'] |
| 356 | + const usage = 'test usage' |
| 357 | + |
| 358 | + // When parsing the command line arguments |
| 359 | + const result = await parseCommandLineArgs(args, usage) |
| 360 | + |
| 361 | + // Then the default timeout should be used and warning logged |
| 362 | + expect(result.authTimeoutMs).toBe(30000) |
| 363 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 364 | + expect.stringContaining('Warning: Ignoring invalid auth timeout value: invalid. Must be a positive number.'), |
| 365 | + ) |
| 366 | + |
| 367 | + consoleSpy.mockRestore() |
| 368 | + }) |
| 369 | + |
| 370 | + it('Scenario: Use default timeout when negative auth timeout value is provided', async () => { |
| 371 | + // Given command line arguments with negative --auth-timeout value |
| 372 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 373 | + const args = ['https://example.com/sse', '--auth-timeout', '-30'] |
| 374 | + const usage = 'test usage' |
| 375 | + |
| 376 | + // When parsing the command line arguments |
| 377 | + const result = await parseCommandLineArgs(args, usage) |
| 378 | + |
| 379 | + // Then the default timeout should be used and warning logged |
| 380 | + expect(result.authTimeoutMs).toBe(30000) |
| 381 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 382 | + expect.stringContaining('Warning: Ignoring invalid auth timeout value: -30. Must be a positive number.'), |
| 383 | + ) |
| 384 | + |
| 385 | + consoleSpy.mockRestore() |
| 386 | + }) |
| 387 | + |
| 388 | + it('Scenario: Use default timeout when zero auth timeout value is provided', async () => { |
| 389 | + // Given command line arguments with zero --auth-timeout value |
| 390 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 391 | + const args = ['https://example.com/sse', '--auth-timeout', '0'] |
| 392 | + const usage = 'test usage' |
| 393 | + |
| 394 | + // When parsing the command line arguments |
| 395 | + const result = await parseCommandLineArgs(args, usage) |
| 396 | + |
| 397 | + // Then the default timeout should be used and warning logged |
| 398 | + expect(result.authTimeoutMs).toBe(30000) |
| 399 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 400 | + expect.stringContaining('Warning: Ignoring invalid auth timeout value: 0. Must be a positive number.'), |
| 401 | + ) |
| 402 | + |
| 403 | + consoleSpy.mockRestore() |
| 404 | + }) |
| 405 | + |
| 406 | + it('Scenario: Log when using custom auth timeout', async () => { |
| 407 | + // Given command line arguments with custom --auth-timeout value |
| 408 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 409 | + const args = ['https://example.com/sse', '--auth-timeout', '45'] |
| 410 | + const usage = 'test usage' |
| 411 | + |
| 412 | + // When parsing the command line arguments |
| 413 | + const result = await parseCommandLineArgs(args, usage) |
| 414 | + |
| 415 | + // Then the custom timeout should be used and logged |
| 416 | + expect(result.authTimeoutMs).toBe(45000) |
| 417 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Using auth callback timeout: 45 seconds')) |
| 418 | + |
| 419 | + consoleSpy.mockRestore() |
| 420 | + }) |
325 | 421 | })
|
326 | 422 |
|
327 | 423 | describe('Feature: Tool Filtering with Ignore Patterns', () => {
|
@@ -773,3 +869,49 @@ describe('Feature: MCP Proxy', () => {
|
773 | 869 | )
|
774 | 870 | })
|
775 | 871 | })
|
| 872 | + |
| 873 | +describe('setupOAuthCallbackServerWithLongPoll', () => { |
| 874 | + let server: any |
| 875 | + let events: EventEmitter |
| 876 | + |
| 877 | + beforeEach(() => { |
| 878 | + events = new EventEmitter() |
| 879 | + }) |
| 880 | + |
| 881 | + afterEach(() => { |
| 882 | + if (server) { |
| 883 | + server.close() |
| 884 | + server = null |
| 885 | + } |
| 886 | + }) |
| 887 | + |
| 888 | + it('should use custom timeout when authTimeoutMs is provided', async () => { |
| 889 | + const customTimeout = 5000 |
| 890 | + const result = setupOAuthCallbackServerWithLongPoll({ |
| 891 | + port: 0, // Use any available port |
| 892 | + path: '/oauth/callback', |
| 893 | + events, |
| 894 | + authTimeoutMs: customTimeout, |
| 895 | + }) |
| 896 | + |
| 897 | + server = result.server |
| 898 | + |
| 899 | + // Test that the server was created |
| 900 | + expect(server).toBeDefined() |
| 901 | + expect(typeof result.waitForAuthCode).toBe('function') |
| 902 | + }) |
| 903 | + |
| 904 | + it('should use default timeout when authTimeoutMs is not provided', async () => { |
| 905 | + const result = setupOAuthCallbackServerWithLongPoll({ |
| 906 | + port: 0, // Use any available port |
| 907 | + path: '/oauth/callback', |
| 908 | + events, |
| 909 | + }) |
| 910 | + |
| 911 | + server = result.server |
| 912 | + |
| 913 | + // Test that the server was created with defaults |
| 914 | + expect(server).toBeDefined() |
| 915 | + expect(typeof result.waitForAuthCode).toBe('function') |
| 916 | + }) |
| 917 | +}) |
0 commit comments