github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/rest/headers_test.go (about) 1 package rest 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestParseSizeFromHeaders(t *testing.T) { 11 testCases := []struct { 12 ContentLength, ContentRange string 13 Size int64 14 }{{ 15 "", "", -1, 16 }, { 17 "42", "", 42, 18 }, { 19 "42", "invalid", -1, 20 }, { 21 "", "bytes 22-33/42", 42, 22 }, { 23 "12", "bytes 22-33/42", 42, 24 }, { 25 "12", "otherUnit 22-33/42", -1, 26 }, { 27 "12", "bytes 22-33/*", -1, 28 }, { 29 "0", "bytes */42", 42, 30 }} 31 for _, testCase := range testCases { 32 headers := make(http.Header, 2) 33 if len(testCase.ContentLength) > 0 { 34 headers.Set("Content-Length", testCase.ContentLength) 35 } 36 if len(testCase.ContentRange) > 0 { 37 headers.Set("Content-Range", testCase.ContentRange) 38 } 39 assert.Equalf(t, testCase.Size, ParseSizeFromHeaders(headers), "%+v", testCase) 40 } 41 }