66 "io/ioutil"
77 "os"
88 "testing"
9+ "time"
910
1011 "github.com/melbahja/got"
1112)
@@ -38,9 +39,11 @@ func TestDownloading(t *testing.T) {
3839 t .Run ("downloadOkFileTest" , downloadOkFileTest )
3940 t .Run ("downloadNotFoundTest" , downloadNotFoundTest )
4041 t .Run ("downloadOkFileContentTest" , downloadOkFileContentTest )
42+ t .Run ("downloadTimeoutContextTest" , downloadTimeoutContextTest )
4143 t .Run ("downloadHeadNotSupported" , downloadHeadNotSupported )
4244 t .Run ("downloadPartialContentNotSupportedTest" , downloadPartialContentNotSupportedTest )
4345 t .Run ("getFilenameTest" , getFilenameTest )
46+ t .Run ("coverTests" , coverTests )
4447}
4548
4649func getInfoTest (t * testing.T ) {
@@ -50,8 +53,6 @@ func getInfoTest(t *testing.T) {
5053
5154 dl := got .NewDownload (context .Background (), httpt .URL + "/ok_file" , tmpFile )
5255
53- dl .Client = got .GetDefaultClient ()
54-
5556 info , err := dl .GetInfo ()
5657
5758 if err != nil {
@@ -75,8 +76,6 @@ func getFilenameTest(t *testing.T) {
7576
7677 dl := got .NewDownload (context .Background (), httpt .URL + "/file_name" , tmpFile )
7778
78- dl .Client = got .GetDefaultClient ()
79-
8079 info , err := dl .GetInfo ()
8180
8281 if err != nil {
@@ -220,14 +219,48 @@ func downloadOkFileContentTest(t *testing.T) {
220219
221220}
222221
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+
223256func downloadHeadNotSupported (t * testing.T ) {
224257
225258 tmpFile := createTemp ()
226259 defer clean (tmpFile )
227260
228261 d := & got.Download {
229262 URL : httpt .URL + "/found_and_head_not_allowed" ,
230- Dest : tmpFile ,
263+ Dest : "/invalid/path/for_testing_got_start_method" ,
231264 }
232265
233266 // init
@@ -237,14 +270,16 @@ func downloadHeadNotSupported(t *testing.T) {
237270 }
238271
239272 if d .TotalSize () != 0 {
240-
241273 t .Error ("Size should be 0" )
242274 }
243275
244276 if d .IsRangeable () != false {
245-
246277 t .Error ("rangeable should be false" )
247278 }
279+
280+ if err := d .Start (); err == nil {
281+ t .Error ("Expecting invalid path error" )
282+ }
248283}
249284
250285func downloadPartialContentNotSupportedTest (t * testing.T ) {
@@ -281,6 +316,35 @@ func downloadPartialContentNotSupportedTest(t *testing.T) {
281316 }
282317}
283318
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+
284348func ExampleDownload () {
285349
286350 // Just for testing
0 commit comments