forked from huydx/hget
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_test.go
333 lines (293 loc) · 8.16 KB
/
http_test.go
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
package main
import (
"os"
"os/user"
"path/filepath"
"strings"
"testing"
"time"
)
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)
// Verify the path was sent to fileChan
select {
case path := <-fileChan:
if path != part.Path {
t.Errorf("Expected path %s, got %s", part.Path, path)
}
default:
t.Errorf("No path sent to fileChan")
}
// 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(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())
}
})
}