forked from huydx/hget
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp_test.go
More file actions
440 lines (388 loc) · 11.2 KB
/
http_test.go
File metadata and controls
440 lines (388 loc) · 11.2 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package main
import (
"context"
"net/http"
"net/http/httptest"
"os"
"os/user"
"path/filepath"
"strings"
"testing"
"time"
"golang.org/x/time/rate"
)
func TestPartCalculate(t *testing.T) {
// Disable progress bar for tests
displayProgress = false
// Setup test environment
originalDataFolder := dataFolder
dataFolder = ".hget_test/"
defer func() {
dataFolder = originalDataFolder
usr, _ := user.Current()
testFolder := filepath.Join(usr.HomeDir, dataFolder)
os.RemoveAll(testFolder)
}()
// Test with different numbers of parts
testCases := []struct {
parts int64
totalSize int64
url string
expectParts int
}{
{10, 100, "http://foo.bar/file", 10},
{5, 1000, "http://example.com/largefile", 5},
{1, 50, "http://test.org/smallfile", 1},
{3, 10, "http://tiny.file/data", 3}, // Small file, multiple parts
}
for _, tc := range testCases {
parts := partCalculate(tc.parts, tc.totalSize, tc.url)
// Check number of parts
if len(parts) != tc.expectParts {
t.Errorf("Expected %d parts, got %d", tc.expectParts, len(parts))
}
// Check part URLs
for i, part := range parts {
if part.URL != tc.url {
t.Errorf("Part %d: Expected URL %s, got %s", i, tc.url, part.URL)
}
// Check part index
if part.Index != int64(i) {
t.Errorf("Part %d: Expected Index %d, got %d", i, i, part.Index)
}
// Check ranges
expectedSize := tc.totalSize / tc.parts
if i < int(tc.parts-1) {
if part.RangeFrom != expectedSize*int64(i) {
t.Errorf("Part %d: Expected RangeFrom %d, got %d",
i, expectedSize*int64(i), part.RangeFrom)
}
if part.RangeTo != expectedSize*int64(i+1)-1 {
t.Errorf("Part %d: Expected RangeTo %d, got %d",
i, expectedSize*int64(i+1)-1, part.RangeTo)
}
} else {
// Last part might be larger due to division remainder
if part.RangeFrom != expectedSize*int64(i) {
t.Errorf("Part %d: Expected RangeFrom %d, got %d",
i, expectedSize*int64(i), part.RangeFrom)
}
if part.RangeTo != tc.totalSize {
t.Errorf("Part %d: Expected RangeTo %d, got %d",
i, tc.totalSize, part.RangeTo)
}
}
// Check path format
usr, _ := user.Current()
expectedBasePath := filepath.Join(usr.HomeDir, dataFolder)
if !strings.Contains(part.Path, expectedBasePath) {
t.Errorf("Part %d: Path does not contain expected base path: %s", i, part.Path)
}
fileName := filepath.Base(part.Path)
expectedPrefix := TaskFromURL(tc.url) + ".part"
if !strings.HasPrefix(fileName, expectedPrefix) {
t.Errorf("Part %d: Expected filename prefix %s, got %s",
i, expectedPrefix, fileName)
}
}
}
}
func TestProxyAwareHTTPClient(t *testing.T) {
// Test with no proxy
client := ProxyAwareHTTPClient("", false)
if client == nil {
t.Fatal("ProxyAwareHTTPClient returned nil with no proxy")
}
// Cannot easily test with an actual proxy, but can verify it doesn't crash
httpProxyClient := ProxyAwareHTTPClient("http://localhost:8080", false)
if httpProxyClient == nil {
t.Fatal("ProxyAwareHTTPClient returned nil with HTTP proxy")
}
socksProxyClient := ProxyAwareHTTPClient("localhost:1080", false)
if socksProxyClient == nil {
t.Fatal("ProxyAwareHTTPClient returned nil with SOCKS proxy")
}
// Test TLS skipVerify parameter
tlsClient := ProxyAwareHTTPClient("", true)
if tlsClient == nil {
t.Fatal("ProxyAwareHTTPClient returned nil with TLS skip verification")
}
// Can't directly access TLS config, but it shouldn't crash
}
// Helper function to parse integers
func parseInt(s string) int {
n := 0
for _, c := range s {
n = n*10 + int(c-'0')
}
return n
}
func TestHandleCompletedPart(t *testing.T) {
// Disable progress bar for tests
displayProgress = false
// Create a part that's already complete
part := Part{
Index: 0,
URL: "http://example.com/test",
Path: "test.part000000",
RangeFrom: 100, // RangeFrom equals RangeTo means no data to download
RangeTo: 100,
}
// Create channels
fileChan := make(chan string, 1)
stateSaveChan := make(chan Part, 1)
// Create downloader
downloader := &HTTPDownloader{
url: "http://example.com/test",
file: "test",
par: 1,
len: 100,
parts: []Part{part},
resumable: true,
}
// Handle the completed part
downloader.handleCompletedPart(part, fileChan, stateSaveChan)
// We no longer send file paths via fileChan in handleCompletedPart
select {
case <-fileChan:
t.Errorf("Did not expect path to be sent to fileChan")
default:
// ok
}
// Verify the part was sent to stateSaveChan
select {
case savedPart := <-stateSaveChan:
if savedPart.Index != part.Index ||
savedPart.URL != part.URL ||
savedPart.Path != part.Path ||
savedPart.RangeFrom != part.RangeFrom ||
savedPart.RangeTo != part.RangeTo {
t.Errorf("Saved part does not match original part")
}
default:
t.Errorf("No part sent to stateSaveChan")
}
}
func TestBuildRequestForPart(t *testing.T) {
// Test cases for different range situations
testCases := []struct {
description string
part Part
contentLen int64
parallelism int64
expected string
}{
{
description: "Single connection download (no range)",
part: Part{
Index: 0,
URL: "http://example.com/file",
Path: "file.part000000",
RangeFrom: 0,
RangeTo: 100,
},
contentLen: 100,
parallelism: 1,
expected: "", // No range header expected
},
{
description: "Multiple connection download with middle part",
part: Part{
Index: 1,
URL: "http://example.com/file",
Path: "file.part000001",
RangeFrom: 50,
RangeTo: 99,
},
contentLen: 200,
parallelism: 3,
expected: "bytes=50-99",
},
{
description: "Multiple connection download with last part",
part: Part{
Index: 2,
URL: "http://example.com/file",
Path: "file.part000002",
RangeFrom: 100,
RangeTo: 200,
},
contentLen: 200,
parallelism: 3,
expected: "bytes=100-",
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
// Create downloader
downloader := &HTTPDownloader{
url: tc.part.URL,
file: "file",
par: tc.parallelism,
len: tc.contentLen,
parts: []Part{tc.part},
resumable: true,
}
// Build request
req, err := downloader.buildRequestForPart(context.Background(), tc.part)
if err != nil {
t.Fatalf("buildRequestForPart failed: %v", err)
}
// Check range header
rangeHeader := req.Header.Get("Range")
if tc.expected == "" {
if rangeHeader != "" {
t.Errorf("Expected no Range header, got '%s'", rangeHeader)
}
} else {
if rangeHeader != tc.expected {
t.Errorf("Expected Range header '%s', got '%s'", tc.expected, rangeHeader)
}
}
// Check URL
if req.URL.String() != tc.part.URL {
t.Errorf("Expected URL %s, got %s", tc.part.URL, req.URL.String())
}
})
}
}
func TestCopyContent(t *testing.T) {
// Create test data
testData := "This is test data for copy content"
// Test regular copy (no rate limit)
t.Run("No Rate Limit", func(t *testing.T) {
// Create source and destination
src := strings.NewReader(testData)
var dst strings.Builder
// Create downloader with no rate limit
downloader := &HTTPDownloader{
rate: 0,
}
// Copy content
done := make(chan bool)
go downloader.copyContent(src, &dst, done)
// Wait for completion
<-done
// Verify copied content
if dst.String() != testData {
t.Errorf("Expected content '%s', got '%s'", testData, dst.String())
}
})
// Test copy with rate limit (can only test that it doesn't crash)
t.Run("With Rate Limit", func(t *testing.T) {
// Create source and destination
src := strings.NewReader(testData)
var dst strings.Builder
// Create downloader with rate limit
downloader := &HTTPDownloader{
rate: 1024, // 1KB/s
}
// Copy content
done := make(chan bool)
go downloader.copyContent(src, &dst, done)
// Wait for completion with timeout
select {
case <-done:
// Success
case <-time.After(2 * time.Second):
t.Fatalf("Copy with rate limit timed out")
}
// Verify copied content
if dst.String() != testData {
t.Errorf("Expected content '%s', got '%s'", testData, dst.String())
}
})
}
func TestCopyContentWithSharedLimiter(t *testing.T) {
// Verify copyContent path using sharedLimiter copies data correctly.
testData := "shared limiter content"
src := strings.NewReader(testData)
var dst strings.Builder
downloader := &HTTPDownloader{
sharedLimiter: rate.NewLimiter(rate.Limit(1<<20), 1<<20), // high limit to avoid slowness
}
done := make(chan bool)
go downloader.copyContent(src, &dst, done)
<-done
if dst.String() != testData {
t.Errorf("Expected content '%s', got '%s'", testData, dst.String())
}
}
func TestProxyAwareHTTPClientConfiguration(t *testing.T) {
// HTTP proxy config should set Transport.Proxy
client := ProxyAwareHTTPClient("http://127.0.0.1:3128", false)
tr, ok := client.Transport.(*http.Transport)
if !ok {
t.Fatalf("Transport is not *http.Transport")
}
if tr.Proxy == nil {
t.Errorf("expected HTTP proxy to be configured on Transport.Proxy")
}
// SOCKS5 proxy config should set DialContext
client = ProxyAwareHTTPClient("127.0.0.1:1080", false)
tr, ok = client.Transport.(*http.Transport)
if !ok || tr.DialContext == nil {
t.Errorf("expected SOCKS5 DialContext to be configured")
}
// Ensure timeouts are set
if tr.TLSHandshakeTimeout == 0 || tr.ResponseHeaderTimeout == 0 || tr.ExpectContinueTimeout == 0 {
t.Errorf("expected transport timeouts to be set")
}
}
func TestNewHTTPDownloaderProbe(t *testing.T) {
// HEAD returns Accept-Ranges and Content-Length
content := strings.Repeat("x", 1024)
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodHead:
w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Length", "1024")
w.WriteHeader(http.StatusOK)
case http.MethodGet:
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(content))
}
})
ts := httptest.NewServer(h)
defer ts.Close()
d := NewHTTPDownloader(ts.URL, 4, false, "", "")
if d.par != 4 {
t.Fatalf("expected par=4, got %d", d.par)
}
if d.len != 1024 {
t.Fatalf("expected len=1024, got %d", d.len)
}
if !d.resumable {
t.Fatalf("expected resumable=true")
}
if len(d.parts) != 4 {
t.Fatalf("expected 4 parts, got %d", len(d.parts))
}
}
func TestNewHTTPDownloaderRangeFallback(t *testing.T) {
// HEAD has no Accept-Ranges/Content-Length. GET with Range returns 206 + Content-Range
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodHead:
w.WriteHeader(http.StatusOK)
case http.MethodGet:
if rng := r.Header.Get("Range"); strings.HasPrefix(rng, "bytes=") {
w.Header().Set("Content-Range", "bytes 0-0/4096")
w.WriteHeader(http.StatusPartialContent)
_, _ = w.Write([]byte("x"))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(strings.Repeat("x", 4096)))
}
})
ts := httptest.NewServer(h)
defer ts.Close()
d := NewHTTPDownloader(ts.URL, 4, false, "", "")
if d.par != 4 {
t.Fatalf("expected par=4, got %d", d.par)
}
if d.len != 4096 {
t.Fatalf("expected len=4096, got %d", d.len)
}
}