@@ -4,12 +4,14 @@ import (
44 "context"
55 "crypto/rand"
66 "crypto/sha256"
7+ "errors"
78 "fmt"
89 "io"
910 "log"
1011 "net/http"
1112 "net/http/httptest"
1213 "os"
14+ "path/filepath"
1315 "sync"
1416 "testing"
1517
@@ -26,12 +28,12 @@ func TestUninterruptedDownload(t *testing.T) {
2628 r := require .New (t )
2729 ctx := context .Background ()
2830
29- serverURL , expectedHash , cleanup := serveInterruptedTestFile (t , fileSize , 0 )
31+ serverURLs , expectedHash , cleanup := serveInterruptedTestFiles (t , fileSize , 0 , 1 )
3032 defer cleanup ()
3133
3234 hasher := sha256 .New ()
3335
34- err := DownloadURL (ctx , serverURL , io .Discard , WithExpectedHash (hasher , expectedHash ))
36+ err := DownloadURLMultipart (ctx , serverURLs , io .Discard , WithExpectedHash (hasher , expectedHash ))
3537 r .NoError (err )
3638
3739 givenHash := hasher .Sum (nil )
@@ -42,54 +44,96 @@ func TestUninterruptedMismatch(t *testing.T) {
4244 r := require .New (t )
4345 ctx := context .Background ()
4446
45- serverURL , _ , cleanup := serveInterruptedTestFile (t , fileSize , 0 )
47+ serverURLs , _ , cleanup := serveInterruptedTestFiles (t , fileSize , 0 , 1 )
4648 defer cleanup ()
4749
4850 hasher := sha256 .New ()
4951
50- err := DownloadURL (ctx , serverURL , io .Discard , WithExpectedHash (hasher , []byte {}))
52+ err := DownloadURLMultipart (ctx , serverURLs , io .Discard , WithExpectedHash (hasher , []byte {}))
5153 r .Error (err )
5254}
5355
5456func TestInterruptedDownload (t * testing.T ) {
5557 r := require .New (t )
5658 ctx := context .Background ()
5759
58- serverURL , expectedHash , cleanup := serveInterruptedTestFile (t , fileSize , interruptAt )
60+ serverURLs , expectedHash , cleanup := serveInterruptedTestFiles (t , fileSize , interruptAt , 1 )
5961 defer cleanup ()
6062
6163 hasher := sha256 .New ()
6264
63- err := DownloadURL (ctx , serverURL , io .Discard , WithExpectedHash (hasher , expectedHash ))
65+ err := DownloadURLMultipart (ctx , serverURLs , io .Discard , WithExpectedHash (hasher , expectedHash ))
6466 r .NoError (err )
6567}
6668
67- // derrived from https://github.com/vansante/go-dl-stream/blob/e29aef86498f37d3506126bc258193f1c913ea55/download_test.go#L166
68- func serveInterruptedTestFile (t * testing.T , fileSize , interruptAt int64 ) (serverURL string , sha256Hash []byte , cleanup func ()) {
69- rndFile , err := os .CreateTemp (os .TempDir (), "random_file_*.rnd" )
70- assert .NoError (t , err )
71- filePath := rndFile .Name ()
69+ func TestDownloadMultipart (t * testing.T ) {
70+ r := require .New (t )
71+ ctx := context .Background ()
72+
73+ serverURLs , expectedHash , cleanup := serveInterruptedTestFiles (t , fileSize , 0 , 10 )
74+ defer cleanup ()
7275
7376 hasher := sha256 .New ()
74- _ , err = io .Copy (io .MultiWriter (hasher , rndFile ), io .LimitReader (rand .Reader , fileSize ))
75- assert .NoError (t , err )
76- assert .NoError (t , rndFile .Close ())
7777
78- mux := http .NewServeMux ()
79- mux .HandleFunc ("/" , func (writer http.ResponseWriter , request * http.Request ) {
80- log .Printf ("Serving random interrupted file (size: %d, interuptAt: %d), Range: %s" , fileSize , interruptAt , request .Header .Get (rangeHeader ))
78+ err := DownloadURLMultipart (ctx , serverURLs , io .Discard , WithExpectedHash (hasher , expectedHash ))
79+ r .NoError (err )
80+ }
81+
82+ func TestDownloadMultipartInterrupted (t * testing.T ) {
83+ r := require .New (t )
84+ ctx := context .Background ()
85+
86+ serverURLs , expectedHash , cleanup := serveInterruptedTestFiles (t , fileSize , interruptAt , 10 )
87+ defer cleanup ()
88+
89+ hasher := sha256 .New ()
8190
82- http .ServeFile (& interruptibleHTTPWriter {
83- ResponseWriter : writer ,
84- writer : writer ,
85- interruptAt : interruptAt ,
86- }, request , filePath )
91+ err := DownloadURLMultipart (ctx , serverURLs , io .Discard , WithExpectedHash (hasher , expectedHash ))
92+ r .NoError (err )
93+ }
94+
95+ func TestErrNonRetryable (t * testing.T ) {
96+ err := NonRetryableWrapf ("test" )
97+ require .True (t , errors .Is (err , ErrNonRetryable {}))
98+ }
8799
88- })
100+ // derrived from https://github.com/vansante/go-dl-stream/blob/e29aef86498f37d3506126bc258193f1c913ea55/download_test.go#L166
101+ func serveInterruptedTestFiles (t * testing.T , fileSize , interruptAt int64 , parts int ) ([]string , []byte , func ()) {
102+ mux := http .NewServeMux ()
89103 server := httptest .NewServer (mux )
104+ hasher := sha256 .New ()
105+ filePaths := []string {}
106+ urls := []string {}
107+
108+ for i := 0 ; i < parts ; i ++ {
109+ rndFile , err := os .CreateTemp (os .TempDir (), "random_file_*.rnd" )
110+ assert .NoError (t , err )
111+ filePath := rndFile .Name ()
112+ filePaths = append (filePaths , filePath )
113+ filePathBase := filepath .Base (filePath )
90114
91- return server .URL , hasher .Sum (nil ), func () {
92- _ = os .Remove (filePath )
115+ _ , err = io .Copy (io .MultiWriter (hasher , rndFile ), io .LimitReader (rand .Reader , fileSize ))
116+ assert .NoError (t , err )
117+ assert .NoError (t , rndFile .Close ())
118+
119+ mux .HandleFunc (filePath , func (writer http.ResponseWriter , request * http.Request ) {
120+ log .Printf ("Serving random interrupted file %s (size: %d, interuptAt: %d), Range: %s" , filePathBase , fileSize , interruptAt , request .Header .Get (rangeHeader ))
121+
122+ http .ServeFile (& interruptibleHTTPWriter {
123+ ResponseWriter : writer ,
124+ writer : writer ,
125+ interruptAt : interruptAt ,
126+ }, request , filePath )
127+
128+ })
129+ urls = append (urls , server .URL + filePath )
130+
131+ }
132+
133+ return urls , hasher .Sum (nil ), func () {
134+ for _ , path := range filePaths {
135+ _ = os .Remove (path )
136+ }
93137 }
94138}
95139
0 commit comments