-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-download.js
More file actions
64 lines (50 loc) · 2.44 KB
/
test-download.js
File metadata and controls
64 lines (50 loc) · 2.44 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
// Test para simular la descarga del frontend con fetch
const fetch = require('node-fetch');
async function testCoverLetterDownload() {
try {
console.log("🧪 Probando descarga de cover letter...");
const response = await fetch('http://localhost:3000/offers/cover-letter/68176351f05b008452a0ae56/685145288af4b068aa621640', {
method: "GET",
headers: {
"Content-Type": "application/json",
"Origin": "http://localhost:5174"
},
});
if (!response.ok) {
throw new Error("Error downloading PDF");
}
console.log("📋 Headers de respuesta:");
for (const [key, value] of response.headers.entries()) {
console.log(` ${key}: ${value}`);
}
const disposition = response.headers.get("Content-Disposition");
console.log("\n🔍 Content-Disposition header:", disposition);
let filename = "cover-letter.pdf";
if (disposition && disposition.includes("filename=")) {
const filenameMatch = disposition.match(/filename="([^"]+)"/);
const filenameStarMatch = disposition.match(/filename=([^;]+)/);
console.log("filenameMatch:", filenameMatch);
console.log("filenameStarMatch:", filenameStarMatch);
if (filenameMatch && filenameMatch[1]) {
filename = filenameMatch[1];
console.log("✅ Filename extraído (con comillas):", filename);
} else if (filenameStarMatch && filenameStarMatch[1]) {
filename = filenameStarMatch[1].replace(/"/g, "");
console.log("✅ Filename extraído (sin comillas):", filename);
}
} else {
console.log("❌ No se encontró Content-Disposition o filename en el header");
}
console.log("📁 Filename final para descarga:", filename);
// Obtener el contenido y guardarlo
const buffer = await response.buffer();
console.log(`📄 PDF descargado: ${buffer.length} bytes`);
// Simular la escritura del archivo
const fs = require('fs');
fs.writeFileSync(`test-${filename}`, buffer);
console.log(`✅ Archivo guardado como: test-${filename}`);
} catch (error) {
console.error("❌ Error:", error.message);
}
}
testCoverLetterDownload();