Skip to content

Commit

Permalink
some tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Sep 8, 2020
1 parent 458a674 commit 640e07f
Showing 1 changed file with 71 additions and 7 deletions.
78 changes: 71 additions & 7 deletions download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/melbahja/got"
)
Expand Down Expand Up @@ -38,9 +39,11 @@ func TestDownloading(t *testing.T) {
t.Run("downloadOkFileTest", downloadOkFileTest)
t.Run("downloadNotFoundTest", downloadNotFoundTest)
t.Run("downloadOkFileContentTest", downloadOkFileContentTest)
t.Run("downloadTimeoutContextTest", downloadTimeoutContextTest)
t.Run("downloadHeadNotSupported", downloadHeadNotSupported)
t.Run("downloadPartialContentNotSupportedTest", downloadPartialContentNotSupportedTest)
t.Run("getFilenameTest", getFilenameTest)
t.Run("coverTests", coverTests)
}

func getInfoTest(t *testing.T) {
Expand All @@ -50,8 +53,6 @@ func getInfoTest(t *testing.T) {

dl := got.NewDownload(context.Background(), httpt.URL+"/ok_file", tmpFile)

dl.Client = got.GetDefaultClient()

info, err := dl.GetInfo()

if err != nil {
Expand All @@ -75,8 +76,6 @@ func getFilenameTest(t *testing.T) {

dl := got.NewDownload(context.Background(), httpt.URL+"/file_name", tmpFile)

dl.Client = got.GetDefaultClient()

info, err := dl.GetInfo()

if err != nil {
Expand Down Expand Up @@ -220,14 +219,48 @@ func downloadOkFileContentTest(t *testing.T) {

}

func downloadTimeoutContextTest(t *testing.T) {

tmpFile := createTemp()
defer clean(tmpFile)

ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer cancel()

d := got.NewDownload(ctx, httpt.URL+"/ok_file_with_range_delay", tmpFile)
d.ChunkSize = 2

if err := d.Init(); err != nil {
t.Error(err)
}

if err := d.Start(); err == nil {
t.Error("Expecting context deadline")
}

d = got.NewDownload(ctx, httpt.URL+"/ok_file_with_range_delay", tmpFile)

if _, err := d.GetInfo(); err == nil {
t.Error("Expecting context deadline")
}

// just to cover request error.
g := got.NewWithContext(ctx)
err := g.Download("invalid://ok_file_with_range_delay", tmpFile)

if err == nil {
t.Errorf("Expecting invalid scheme error")
}
}

func downloadHeadNotSupported(t *testing.T) {

tmpFile := createTemp()
defer clean(tmpFile)

d := &got.Download{
URL: httpt.URL + "/found_and_head_not_allowed",
Dest: tmpFile,
Dest: "/invalid/path/for_testing_got_start_method",
}

// init
Expand All @@ -237,14 +270,16 @@ func downloadHeadNotSupported(t *testing.T) {
}

if d.TotalSize() != 0 {

t.Error("Size should be 0")
}

if d.IsRangeable() != false {

t.Error("rangeable should be false")
}

if err := d.Start(); err == nil {
t.Error("Expecting invalid path error")
}
}

func downloadPartialContentNotSupportedTest(t *testing.T) {
Expand Down Expand Up @@ -281,6 +316,35 @@ func downloadPartialContentNotSupportedTest(t *testing.T) {
}
}

func coverTests(t *testing.T) {

// Just for testing
destPath := createTemp()
defer clean(destPath)

// cover default dest path.
// cover progress func and methods
d := &got.Download{
URL: httpt.URL + "/ok_file_with_range_delay",
}

// init
if err := d.Init(); err != nil {
t.Error(err)
}

if d.Name() != got.DefaultFileName {
t.Errorf("Expecting name to be: %s but got: %s", got.DefaultFileName, d.Name())
}

go d.RunProgress(func(d *got.Download) {
d.Size()
d.Speed()
d.AvgSpeed()
d.TotalCost()
})
}

func ExampleDownload() {

// Just for testing
Expand Down

0 comments on commit 640e07f

Please sign in to comment.