-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtest-ftp-stream.js
More file actions
169 lines (141 loc) · 5.85 KB
/
test-ftp-stream.js
File metadata and controls
169 lines (141 loc) · 5.85 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import http from 'http';
// 测试配置
const testConfig = {
host: '192.168.31.10',
port: 2121
};
const testFile = '/播放数据加密.txt';
const baseUrl = 'http://localhost:3000';
// URL 编码配置
const encodedConfig = encodeURIComponent(JSON.stringify(testConfig));
console.log('🚀 开始测试 FTP 文件流代理 HTTP 直链功能...\n');
// 测试1: 完整文件下载
function testFullDownload() {
return new Promise((resolve, reject) => {
console.log('📥 测试1: 完整文件下载');
const url = `${baseUrl}/ftp/file?path=${encodeURIComponent(testFile)}&config=${encodedConfig}`;
http.get(url, (res) => {
console.log(` 状态码: ${res.statusCode}`);
console.log(` Content-Type: ${res.headers['content-type']}`);
console.log(` Content-Length: ${res.headers['content-length']}`);
console.log(` Accept-Ranges: ${res.headers['accept-ranges']}`);
console.log(` Cache-Control: ${res.headers['cache-control']}`);
let dataLength = 0;
res.on('data', (chunk) => {
dataLength += chunk.length;
});
res.on('end', () => {
console.log(` 实际接收数据长度: ${dataLength} bytes`);
console.log(' ✅ 完整文件下载测试通过\n');
resolve();
});
}).on('error', reject);
});
}
// 测试2: 范围请求
function testRangeRequest() {
return new Promise((resolve, reject) => {
console.log('📊 测试2: 范围请求 (bytes=0-99)');
const url = `${baseUrl}/ftp/file?path=${encodeURIComponent(testFile)}&config=${encodedConfig}`;
const options = {
headers: {
'Range': 'bytes=0-99'
}
};
const req = http.request(url, options, (res) => {
console.log(` 状态码: ${res.statusCode}`);
console.log(` Content-Range: ${res.headers['content-range']}`);
console.log(` Content-Length: ${res.headers['content-length']}`);
let dataLength = 0;
res.on('data', (chunk) => {
dataLength += chunk.length;
});
res.on('end', () => {
console.log(` 实际接收数据长度: ${dataLength} bytes`);
if (res.statusCode === 206 && dataLength === 100) {
console.log(' ✅ 范围请求测试通过\n');
} else if (res.statusCode === 200) {
console.log(' ⚠️ 服务器不支持范围请求,返回完整文件\n');
} else {
console.log(' ❌ 范围请求测试失败\n');
}
resolve();
});
});
req.on('error', reject);
req.end();
});
}
// 测试3: HEAD 请求
function testHeadRequest() {
return new Promise((resolve, reject) => {
console.log('🔍 测试3: HEAD 请求');
const url = `${baseUrl}/ftp/file?path=${encodeURIComponent(testFile)}&config=${encodedConfig}`;
const options = {
method: 'HEAD'
};
const req = http.request(url, options, (res) => {
console.log(` 状态码: ${res.statusCode}`);
console.log(` Content-Type: ${res.headers['content-type']}`);
console.log(` Content-Length: ${res.headers['content-length']}`);
console.log(` Accept-Ranges: ${res.headers['accept-ranges']}`);
let dataLength = 0;
res.on('data', (chunk) => {
dataLength += chunk.length;
});
res.on('end', () => {
console.log(` 实际接收数据长度: ${dataLength} bytes (应该为0)`);
if (dataLength === 0 && res.statusCode === 200) {
console.log(' ✅ HEAD 请求测试通过\n');
} else {
console.log(' ❌ HEAD 请求测试失败\n');
}
resolve();
});
});
req.on('error', reject);
req.end();
});
}
// 测试4: 流式传输性能
function testStreamPerformance() {
return new Promise((resolve, reject) => {
console.log('⚡ 测试4: 流式传输性能');
const url = `${baseUrl}/ftp/file?path=${encodeURIComponent(testFile)}&config=${encodedConfig}`;
const startTime = Date.now();
let firstByteTime = null;
let dataLength = 0;
http.get(url, (res) => {
res.on('data', (chunk) => {
if (!firstByteTime) {
firstByteTime = Date.now();
console.log(` 首字节延迟: ${firstByteTime - startTime}ms`);
}
dataLength += chunk.length;
});
res.on('end', () => {
const endTime = Date.now();
const totalTime = endTime - startTime;
const throughput = (dataLength / 1024 / (totalTime / 1000)).toFixed(2);
console.log(` 总传输时间: ${totalTime}ms`);
console.log(` 传输速度: ${throughput} KB/s`);
console.log(' ✅ 流式传输性能测试完成\n');
resolve();
});
}).on('error', reject);
});
}
// 运行所有测试
async function runAllTests() {
try {
await testFullDownload();
await testRangeRequest();
await testHeadRequest();
await testStreamPerformance();
console.log('🎉 所有 FTP 文件流代理 HTTP 直链功能测试完成!');
} catch (error) {
console.error('❌ 测试失败:', error.message);
process.exit(1);
}
}
runAllTests();