github.com/tyler-smith/grab@v2.0.1-0.20190224022517-abcee96e61b1+incompatible/response_test.go (about) 1 package grab 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 ) 8 9 // testComplete validates that a completed Response has all the desired fields. 10 func testComplete(t *testing.T, resp *Response) { 11 <-resp.Done 12 if !resp.IsComplete() { 13 t.Errorf("Response.IsComplete returned false") 14 } 15 16 if resp.Start.IsZero() { 17 t.Errorf("Response.Start is zero") 18 } 19 20 if resp.End.IsZero() { 21 t.Error("Response.End is zero") 22 } 23 24 if eta := resp.ETA(); eta != resp.End { 25 t.Errorf("Response.ETA is not equal to Response.End: %v", eta) 26 } 27 28 // the following fields should only be set if no error occurred 29 if resp.Err() == nil { 30 if resp.Filename == "" { 31 t.Errorf("Response.Filename is empty") 32 } 33 34 if resp.Size == 0 { 35 t.Error("Response.Size is zero") 36 } 37 38 if p := resp.Progress(); p != 1.00 { 39 t.Errorf("Response.Progress returned %v (%v/%v bytes), expected 1", p, resp.BytesComplete(), resp.Size) 40 } 41 } 42 } 43 44 // TestResponseProgress tests the functions which indicate the progress of an 45 // in-process file transfer. 46 func TestResponseProgress(t *testing.T) { 47 filename := ".testResponseProgress" 48 defer os.Remove(filename) 49 50 sleep := 300 // ms 51 size := int64(1024 * 8) // bytes 52 53 // request a slow transfer 54 req, _ := NewRequest(filename, fmt.Sprintf("%s?sleep=%v&size=%v", ts.URL, sleep, size)) 55 resp := DefaultClient.Do(req) 56 57 // make sure transfer has not started 58 if resp.IsComplete() { 59 t.Errorf("Transfer should not have started") 60 } 61 62 if p := resp.Progress(); p != 0 { 63 t.Errorf("Transfer should not have started yet but progress is %v", p) 64 } 65 66 // wait for transfer to complete 67 <-resp.Done 68 69 // make sure transfer is complete 70 if p := resp.Progress(); p != 1 { 71 t.Errorf("Transfer is complete but progress is %v", p) 72 } 73 74 if s := resp.BytesComplete(); s != size { 75 t.Errorf("Expected to transfer %v bytes, got %v", size, s) 76 } 77 }