6
6
"io/ioutil"
7
7
"os"
8
8
"testing"
9
+ "time"
9
10
10
11
"github.com/melbahja/got"
11
12
)
@@ -38,9 +39,11 @@ func TestDownloading(t *testing.T) {
38
39
t .Run ("downloadOkFileTest" , downloadOkFileTest )
39
40
t .Run ("downloadNotFoundTest" , downloadNotFoundTest )
40
41
t .Run ("downloadOkFileContentTest" , downloadOkFileContentTest )
42
+ t .Run ("downloadTimeoutContextTest" , downloadTimeoutContextTest )
41
43
t .Run ("downloadHeadNotSupported" , downloadHeadNotSupported )
42
44
t .Run ("downloadPartialContentNotSupportedTest" , downloadPartialContentNotSupportedTest )
43
45
t .Run ("getFilenameTest" , getFilenameTest )
46
+ t .Run ("coverTests" , coverTests )
44
47
}
45
48
46
49
func getInfoTest (t * testing.T ) {
@@ -50,8 +53,6 @@ func getInfoTest(t *testing.T) {
50
53
51
54
dl := got .NewDownload (context .Background (), httpt .URL + "/ok_file" , tmpFile )
52
55
53
- dl .Client = got .GetDefaultClient ()
54
-
55
56
info , err := dl .GetInfo ()
56
57
57
58
if err != nil {
@@ -75,8 +76,6 @@ func getFilenameTest(t *testing.T) {
75
76
76
77
dl := got .NewDownload (context .Background (), httpt .URL + "/file_name" , tmpFile )
77
78
78
- dl .Client = got .GetDefaultClient ()
79
-
80
79
info , err := dl .GetInfo ()
81
80
82
81
if err != nil {
@@ -220,14 +219,48 @@ func downloadOkFileContentTest(t *testing.T) {
220
219
221
220
}
222
221
222
+ func downloadTimeoutContextTest (t * testing.T ) {
223
+
224
+ tmpFile := createTemp ()
225
+ defer clean (tmpFile )
226
+
227
+ ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond * 500 )
228
+ defer cancel ()
229
+
230
+ d := got .NewDownload (ctx , httpt .URL + "/ok_file_with_range_delay" , tmpFile )
231
+ d .ChunkSize = 2
232
+
233
+ if err := d .Init (); err != nil {
234
+ t .Error (err )
235
+ }
236
+
237
+ if err := d .Start (); err == nil {
238
+ t .Error ("Expecting context deadline" )
239
+ }
240
+
241
+ d = got .NewDownload (ctx , httpt .URL + "/ok_file_with_range_delay" , tmpFile )
242
+
243
+ if _ , err := d .GetInfo (); err == nil {
244
+ t .Error ("Expecting context deadline" )
245
+ }
246
+
247
+ // just to cover request error.
248
+ g := got .NewWithContext (ctx )
249
+ err := g .Download ("invalid://ok_file_with_range_delay" , tmpFile )
250
+
251
+ if err == nil {
252
+ t .Errorf ("Expecting invalid scheme error" )
253
+ }
254
+ }
255
+
223
256
func downloadHeadNotSupported (t * testing.T ) {
224
257
225
258
tmpFile := createTemp ()
226
259
defer clean (tmpFile )
227
260
228
261
d := & got.Download {
229
262
URL : httpt .URL + "/found_and_head_not_allowed" ,
230
- Dest : tmpFile ,
263
+ Dest : "/invalid/path/for_testing_got_start_method" ,
231
264
}
232
265
233
266
// init
@@ -237,14 +270,16 @@ func downloadHeadNotSupported(t *testing.T) {
237
270
}
238
271
239
272
if d .TotalSize () != 0 {
240
-
241
273
t .Error ("Size should be 0" )
242
274
}
243
275
244
276
if d .IsRangeable () != false {
245
-
246
277
t .Error ("rangeable should be false" )
247
278
}
279
+
280
+ if err := d .Start (); err == nil {
281
+ t .Error ("Expecting invalid path error" )
282
+ }
248
283
}
249
284
250
285
func downloadPartialContentNotSupportedTest (t * testing.T ) {
@@ -281,6 +316,35 @@ func downloadPartialContentNotSupportedTest(t *testing.T) {
281
316
}
282
317
}
283
318
319
+ func coverTests (t * testing.T ) {
320
+
321
+ // Just for testing
322
+ destPath := createTemp ()
323
+ defer clean (destPath )
324
+
325
+ // cover default dest path.
326
+ // cover progress func and methods
327
+ d := & got.Download {
328
+ URL : httpt .URL + "/ok_file_with_range_delay" ,
329
+ }
330
+
331
+ // init
332
+ if err := d .Init (); err != nil {
333
+ t .Error (err )
334
+ }
335
+
336
+ if d .Name () != got .DefaultFileName {
337
+ t .Errorf ("Expecting name to be: %s but got: %s" , got .DefaultFileName , d .Name ())
338
+ }
339
+
340
+ go d .RunProgress (func (d * got.Download ) {
341
+ d .Size ()
342
+ d .Speed ()
343
+ d .AvgSpeed ()
344
+ d .TotalCost ()
345
+ })
346
+ }
347
+
284
348
func ExampleDownload () {
285
349
286
350
// Just for testing
0 commit comments