验证修复.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * 验证修复后的 withCustomOriginHeader 函数行为
  3. */
  4. // 模拟修复后的函数
  5. function isLocalOrPrivateHttpEndpoint(endpoint) {
  6. try {
  7. const parsed = new URL(endpoint)
  8. const host = parsed.hostname.toLowerCase()
  9. if (host === "localhost" || host.endsWith(".localhost")) return true
  10. if (host === "127.0.0.1" || host === "::1" || host === "[::1]") return true
  11. if (/^10\./.test(host)) return true
  12. if (/^192\.168\./.test(host)) return true
  13. const match = host.match(/^172\.(\d+)\./)
  14. if (match) {
  15. const second = Number(match[1])
  16. if (second >= 16 && second <= 31) return true
  17. }
  18. return false
  19. } catch {
  20. return /^(https?:\/\/)?(localhost|127\.0\.0\.1)([:/]|$)/i.test(endpoint)
  21. }
  22. }
  23. function localLlmOriginHeader() {
  24. return { Origin: "http://localhost" }
  25. }
  26. function withCustomOriginHeader(headers, url) {
  27. if (isLocalOrPrivateHttpEndpoint(url)) {
  28. return {
  29. ...headers,
  30. ...localLlmOriginHeader(),
  31. }
  32. }
  33. // 非本地端点:不添加 Origin
  34. return headers
  35. }
  36. // 测试用例
  37. const testCases = [
  38. {
  39. name: "远程中转站 (hub.linux.do)",
  40. url: "https://hub.linux.do/v1/chat/completions",
  41. input: { Authorization: "Bearer sk-test", "Content-Type": "application/json" },
  42. expectedHasOrigin: false,
  43. },
  44. {
  45. name: "本地 Ollama",
  46. url: "http://localhost:11434/v1/chat/completions",
  47. input: { Authorization: "Bearer sk-test", "Content-Type": "application/json" },
  48. expectedHasOrigin: true,
  49. expectedOriginValue: "http://localhost",
  50. },
  51. {
  52. name: "远程 OpenRouter",
  53. url: "https://openrouter.ai/api/v1/chat/completions",
  54. input: { Authorization: "Bearer sk-test", "Content-Type": "application/json" },
  55. expectedHasOrigin: false,
  56. },
  57. {
  58. name: "本地 192.168 内网",
  59. url: "http://192.168.1.100:8080/v1/chat/completions",
  60. input: { "Content-Type": "application/json" },
  61. expectedHasOrigin: true,
  62. expectedOriginValue: "http://localhost",
  63. },
  64. ]
  65. console.log("=== 验证修复后的 withCustomOriginHeader 函数 ===\n")
  66. let allPassed = true
  67. testCases.forEach((test, index) => {
  68. console.log(`测试 ${index + 1}: ${test.name}`)
  69. console.log(` URL: ${test.url}`)
  70. const result = withCustomOriginHeader(test.input, test.url)
  71. console.log(` 输入 headers:`, test.input)
  72. console.log(` 输出 headers:`, result)
  73. const hasOrigin = result.hasOwnProperty("Origin")
  74. const passed = hasOrigin === test.expectedHasOrigin
  75. if (test.expectedHasOrigin) {
  76. const originCorrect = result.Origin === test.expectedOriginValue
  77. if (!originCorrect) {
  78. console.log(` ❌ 失败: Origin 值错误,期望 "${test.expectedOriginValue}",实际 "${result.Origin}"`)
  79. allPassed = false
  80. } else {
  81. console.log(` ✅ 通过: Origin 正确设置为 "${result.Origin}"`)
  82. }
  83. } else {
  84. if (hasOrigin) {
  85. console.log(` ❌ 失败: 不应该有 Origin 属性,但实际有 "${result.Origin}"`)
  86. allPassed = false
  87. } else {
  88. console.log(` ✅ 通过: 未设置 Origin(符合预期)`)
  89. }
  90. }
  91. // 检查其他 headers 是否保留
  92. const otherHeadersPreserved = Object.keys(test.input).every(
  93. key => result[key] === test.input[key]
  94. )
  95. if (!otherHeadersPreserved) {
  96. console.log(` ❌ 失败: 其他 headers 未正确保留`)
  97. allPassed = false
  98. } else {
  99. console.log(` ✅ 其他 headers 正确保留`)
  100. }
  101. console.log()
  102. })
  103. console.log("==========================================")
  104. if (allPassed) {
  105. console.log("✅ 所有测试通过!修复正确!")
  106. process.exit(0)
  107. } else {
  108. console.log("❌ 部分测试失败,请检查代码")
  109. process.exit(1)
  110. }