🧱 Chunk is a download manager for slow and unstable servers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #19 from devils2ndself/issue-18

Added tests for `getDownloadSize`

authored by

Eduardo Cuducos and committed by
GitHub
4e96cd29 a27be85e

+78 -3
+9 -2
main.go
··· 147 147 if resp.StatusCode != 200 { 148 148 return 0, fmt.Errorf("got unexpected http response status for %s: %s", u, resp.Status) 149 149 } 150 - if resp.ContentLength <= 0 && resp.Header.Get("Content-Range") != "" { 150 + if resp.ContentLength <= 0 { 151 + if resp.Header.Get("Content-Range") == "" { 152 + // TODO: find a way to throw an error on no-content with keeping the tests run as usual 153 + return 0, nil 154 + } 151 155 var s uint64 152 156 p := strings.Split(resp.Header.Get("Content-Range"), "/") 153 157 fmt.Sscan(p[len(p)-1], &s) ··· 254 258 // NewDownloader creates a downloader with the defalt configuration. Check 255 259 // the constants in this package for their values. 256 260 func DefaultDownloader() *Downloader { 257 - return &Downloader{ 261 + d := Downloader{ 258 262 TimeoutPerChunk: DefaultTimeoutPerChunk, 259 263 MaxParallelDownloadsPerServer: DefaultMaxParallelDownloadsPerServer, 260 264 MaxRetriesPerChunk: DefaultMaxRetriesPerChunk, 261 265 ChunkSize: DefaultChunkSize, 262 266 WaitBetweenRetries: DefaultWaitBetweenRetries, 263 267 } 268 + d.client = &http.Client{Timeout: d.TimeoutPerChunk} 269 + 270 + return &d 264 271 } 265 272 266 273 func main() {
+69 -1
main_test.go
··· 217 217 } 218 218 } 219 219 220 - // TODO: add tests for getDownloadSize (success with Content-Length, success with Content-Range, failure) 220 + func TestGetDownloadSize_ContentLength(t *testing.T) { 221 + s := httptest.NewServer(http.HandlerFunc( 222 + func(w http.ResponseWriter, r *http.Request) { 223 + fmt.Fprint(w, "Test") 224 + }, 225 + )) 226 + defer s.Close() 227 + 228 + d := DefaultDownloader() 229 + got, err := d.getDownloadSize(context.Background(), s.URL) 230 + 231 + if err != nil { 232 + t.Errorf("expected no error getting the file size, got %s", err) 233 + } 234 + if got != 4 { 235 + t.Errorf("invalid size, expected 4, got: %d", got) 236 + } 237 + } 238 + 239 + func TestGetDownloadSize_ContentRange(t *testing.T) { 240 + s := httptest.NewServer(http.HandlerFunc( 241 + func(w http.ResponseWriter, r *http.Request) { 242 + w.Header().Set("Content-Range", "bytes 1-10/123") 243 + fmt.Fprint(w, "") 244 + }, 245 + )) 246 + defer s.Close() 247 + 248 + d := DefaultDownloader() 249 + got, err := d.getDownloadSize(context.Background(), s.URL) 250 + 251 + if err != nil { 252 + t.Errorf("expected no error getting the file size, got %s", err) 253 + } 254 + if got != 123 { 255 + t.Errorf("invalid size, expected 123, got: %d", got) 256 + } 257 + } 258 + 259 + func TestGetDownloadSize_ErrorInvalidURL(t *testing.T) { 260 + d := DefaultDownloader() 261 + got, err := d.getDownloadSize(context.Background(), "test") 262 + 263 + if err == nil { 264 + t.Errorf("expected an error, got nil") 265 + } 266 + if got != 0 { 267 + t.Errorf("invalid size, expected 0, got: %d", got) 268 + } 269 + } 270 + 271 + func TestGetDownloadSize_NoContent(t *testing.T) { 272 + s := httptest.NewServer(http.HandlerFunc( 273 + func(w http.ResponseWriter, r *http.Request) { 274 + fmt.Fprint(w, "") 275 + }, 276 + )) 277 + defer s.Close() 278 + 279 + d := DefaultDownloader() 280 + got, err := d.getDownloadSize(context.Background(), s.URL) 281 + 282 + if err != nil { 283 + t.Errorf("expected no error getting the file size, got %s", err) 284 + } 285 + if got != 0 { 286 + t.Errorf("invalid size, expected 0, got: %d", got) 287 + } 288 + }