oauth-callback.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { test, expect, describe, afterEach } from "bun:test"
  2. import { McpOAuthCallback } from "../../src/mcp/oauth-callback"
  3. import { parseRedirectUri } from "../../src/mcp/oauth-provider"
  4. describe("McpOAuthCallback.ensureRunning", () => {
  5. afterEach(async () => {
  6. await McpOAuthCallback.stop()
  7. })
  8. test("starts server with default config when no redirectUri provided", async () => {
  9. await McpOAuthCallback.ensureRunning()
  10. expect(McpOAuthCallback.isRunning()).toBe(true)
  11. })
  12. test("starts server with custom redirectUri", async () => {
  13. await McpOAuthCallback.ensureRunning("http://127.0.0.1:18000/custom/callback")
  14. expect(McpOAuthCallback.isRunning()).toBe(true)
  15. })
  16. test("is idempotent when called with same redirectUri", async () => {
  17. await McpOAuthCallback.ensureRunning("http://127.0.0.1:18001/callback")
  18. await McpOAuthCallback.ensureRunning("http://127.0.0.1:18001/callback")
  19. expect(McpOAuthCallback.isRunning()).toBe(true)
  20. })
  21. test("restarts server when redirectUri changes", async () => {
  22. await McpOAuthCallback.ensureRunning("http://127.0.0.1:18002/path1")
  23. expect(McpOAuthCallback.isRunning()).toBe(true)
  24. await McpOAuthCallback.ensureRunning("http://127.0.0.1:18003/path2")
  25. expect(McpOAuthCallback.isRunning()).toBe(true)
  26. })
  27. test("isRunning returns false when not started", async () => {
  28. expect(McpOAuthCallback.isRunning()).toBe(false)
  29. })
  30. test("isRunning returns false after stop", async () => {
  31. await McpOAuthCallback.ensureRunning()
  32. await McpOAuthCallback.stop()
  33. expect(McpOAuthCallback.isRunning()).toBe(false)
  34. })
  35. })
  36. describe("parseRedirectUri", () => {
  37. test("returns defaults when no URI provided", () => {
  38. const result = parseRedirectUri()
  39. expect(result.port).toBe(19876)
  40. expect(result.path).toBe("/mcp/oauth/callback")
  41. })
  42. test("parses port and path from URI", () => {
  43. const result = parseRedirectUri("http://127.0.0.1:8080/oauth/callback")
  44. expect(result.port).toBe(8080)
  45. expect(result.path).toBe("/oauth/callback")
  46. })
  47. test("defaults to port 80 for http without explicit port", () => {
  48. const result = parseRedirectUri("http://127.0.0.1/callback")
  49. expect(result.port).toBe(80)
  50. expect(result.path).toBe("/callback")
  51. })
  52. test("defaults to port 443 for https without explicit port", () => {
  53. const result = parseRedirectUri("https://127.0.0.1/callback")
  54. expect(result.port).toBe(443)
  55. expect(result.path).toBe("/callback")
  56. })
  57. test("returns defaults for invalid URI", () => {
  58. const result = parseRedirectUri("not-a-valid-url")
  59. expect(result.port).toBe(19876)
  60. expect(result.path).toBe("/mcp/oauth/callback")
  61. })
  62. })