generated from Java-2025Fall/final-vibevault-template
94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
import https from 'https';
|
|
import http from 'http';
|
|
|
|
// 要测试的音频URL列表
|
|
const audioUrls = [
|
|
{
|
|
name: '歌曲3 - 曹操',
|
|
url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-4.mp3'
|
|
},
|
|
{
|
|
name: '歌曲4 - 她说',
|
|
url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3'
|
|
},
|
|
{
|
|
name: '歌曲5 - 背对背拥抱',
|
|
url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3'
|
|
},
|
|
{
|
|
name: '歌曲6 - 一千年以后',
|
|
url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'
|
|
}
|
|
];
|
|
|
|
// 测试URL是否能正常访问
|
|
function testAudioUrl(url) {
|
|
return new Promise((resolve, reject) => {
|
|
const protocol = url.startsWith('https') ? https : http;
|
|
|
|
protocol.get(url, (res) => {
|
|
// 检查响应头中的Content-Type
|
|
const contentType = res.headers['content-type'] || '';
|
|
const contentLength = res.headers['content-length'] || 0;
|
|
|
|
// 检查状态码和内容类型
|
|
if (res.statusCode === 200 && contentType.includes('audio')) {
|
|
resolve({
|
|
success: true,
|
|
message: `✓ ${url} 可以正常访问`,
|
|
contentType: contentType,
|
|
contentLength: `${(contentLength / 1024 / 1024).toFixed(2)} MB`
|
|
});
|
|
} else {
|
|
resolve({
|
|
success: false,
|
|
message: `✗ ${url} 访问失败`,
|
|
statusCode: res.statusCode,
|
|
contentType: contentType
|
|
});
|
|
}
|
|
|
|
// 关闭连接
|
|
res.destroy();
|
|
}).on('error', (err) => {
|
|
reject({
|
|
success: false,
|
|
message: `✗ ${url} 发生错误: ${err.message}`
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// 运行所有测试
|
|
async function runTests() {
|
|
console.log('开始测试音频URL...\n');
|
|
|
|
for (const audio of audioUrls) {
|
|
console.log(`测试 ${audio.name}:`);
|
|
console.log(`URL: ${audio.url}`);
|
|
|
|
try {
|
|
const result = await testAudioUrl(audio.url);
|
|
console.log(result.message);
|
|
if (result.success) {
|
|
console.log(` 内容类型: ${result.contentType}`);
|
|
console.log(` 文件大小: ${result.contentLength}`);
|
|
} else {
|
|
if (result.statusCode) {
|
|
console.log(` 状态码: ${result.statusCode}`);
|
|
}
|
|
if (result.contentType) {
|
|
console.log(` 内容类型: ${result.contentType}`);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log(error.message);
|
|
}
|
|
|
|
console.log('');
|
|
}
|
|
|
|
console.log('测试完成!');
|
|
}
|
|
|
|
runTests(); |