paper-burner/local-proxy/test_upload_oss.js

59 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'fs';
import FormData from 'form-data';
import fetch from 'node-fetch';
async function testUpload() {
const url = 'http://localhost:3456/api/upload/oss';
const testFileName = 'dummy_test.pdf';
// 1. 创建测试用的假PDF文件
fs.writeFileSync(testFileName, '%PDF-1.4\n%Fake PDF content for testing OSS upload.\n');
console.log(`[测试] 已生成临时文件: ${testFileName}`);
try {
// 2. 构造 FormData 并附加文件
const form = new FormData();
form.append('file', fs.createReadStream(testFileName), testFileName);
console.log(`[测试] 正在向 ${url} 发送上传请求...`);
// 3. 发送请求
const response = await fetch(url, {
method: 'POST',
body: form,
// 注意node-fetch 和 form-data 配合时不需要手动设 Content-Type
// form-data 会自动带上正确的 boundary header
});
console.log(`[测试] HTTP 状态码: ${response.status}`);
const resultText = await response.text();
let resultJson;
try {
resultJson = JSON.parse(resultText);
} catch(e) {
resultJson = resultText;
}
console.log('[测试] 响应内容:');
console.log(resultJson);
if (response.ok) {
console.log('\n✅ 上传测试请求成功!');
} else {
console.log('\n❌ 上传测试失败,请检返回信息。');
}
} catch (err) {
console.error(`\n❌ 请求发生错误:`, err.message);
} finally {
// 4. 清理临时文件
if (fs.existsSync(testFileName)) {
fs.unlinkSync(testFileName);
console.log(`[测试] 已清理临时文件: ${testFileName}`);
}
}
}
testUpload();