-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverify-fix.js
More file actions
131 lines (112 loc) · 3.84 KB
/
verify-fix.js
File metadata and controls
131 lines (112 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { ApiClient } from "./dist/index.js";
console.log("✅ Testing header handling in ApiClient...\n");
// Mock fetch para capturar los headers
const mockResponses = [];
let callCount = 0;
const mockFetch = async (url, init) => {
callCount++;
mockResponses.push({ url, init });
console.log(`📤 Request #${callCount}:`);
console.log(` URL: ${url}`);
console.log(` Method: ${init.method}`);
console.log(` Headers type: ${typeof init.headers}`);
console.log(
` Headers is Object: ${init.headers && typeof init.headers === "object" && !(init.headers instanceof Headers)}`
);
console.log(` Headers:`, init.headers);
console.log("");
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
};
// Test 1: Headers con Authorization
console.log("🧪 Test 1: GET request con Authorization header");
const client1 = new ApiClient({
baseURL: "https://api.groq.com/openai/v1",
defaultHeaders: {
Authorization: "Bearer test-key-123",
"X-Custom": "value",
},
fetchImpl: mockFetch,
});
await client1.get("/chat/completions");
// Test 2: POST con Content-Type personalizado
console.log("🧪 Test 2: POST request con Content-Type personalizado");
const client2 = new ApiClient({
baseURL: "https://api.example.com",
defaultHeaders: {
"Content-Type": "text/plain",
},
fetchImpl: mockFetch,
});
await client2.post("/data", { text: "hello" });
// Test 3: POST sin Content-Type (debería auto-setearse)
console.log("🧪 Test 3: POST request sin Content-Type (auto-set)");
const client3 = new ApiClient({
baseURL: "https://api.example.com",
defaultHeaders: {
Authorization: "Bearer token",
},
fetchImpl: mockFetch,
});
await client3.post("/api/users", { name: "John" });
// Verificaciones
console.log("");
console.log("📊 RESULTADOS:");
console.log("================");
let passed = 0;
let failed = 0;
// Verificar Test 1
const test1Headers = mockResponses[0].init.headers;
if (typeof test1Headers === "object" && !(test1Headers instanceof Headers)) {
console.log("✅ Test 1: Headers es objeto plano");
passed++;
} else {
console.log("❌ Test 1: Headers NO es objeto plano");
failed++;
}
// Los headers se normalizan a minúsculas (estándar HTTP)
if (test1Headers["authorization"] === "Bearer test-key-123") {
console.log("✅ Test 1: Authorization header preservado");
passed++;
} else {
console.log("❌ Test 1: Authorization header NO preservado");
console.log(" Esperado: Bearer test-key-123");
console.log(" Recibido:", test1Headers["authorization"]);
failed++;
}
// Verificar Test 2
const test2Headers = mockResponses[1].init.headers;
if (test2Headers["content-type"] === "text/plain") {
console.log("✅ Test 2: Content-Type personalizado respetado");
passed++;
} else {
console.log("❌ Test 2: Content-Type personalizado NO respetado");
console.log(" Esperado: text/plain");
console.log(" Recibido:", test2Headers["content-type"]);
failed++;
}
// Verificar Test 3
const test3Headers = mockResponses[2].init.headers;
if (test3Headers["content-type"] === "application/json") {
console.log("✅ Test 3: Content-Type auto-seteado correctamente");
passed++;
} else {
console.log("❌ Test 3: Content-Type NO auto-seteado");
console.log(" Esperado: application/json");
console.log(" Recibido:", test3Headers["content-type"]);
failed++;
}
console.log("");
console.log(`Total: ${passed} passed, ${failed} failed`);
console.log("");
if (failed === 0) {
console.log(
"🎉 ¡Todos los tests pasaron! La corrección funciona correctamente."
);
process.exit(0);
} else {
console.log("⚠️ Algunos tests fallaron. Revisar la implementación.");
process.exit(1);
}