storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/httprange_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2016 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cmd 18 19 import ( 20 "testing" 21 ) 22 23 func TestHTTPRequestRangeSpec(t *testing.T) { 24 resourceSize := int64(10) 25 validRangeSpecs := []struct { 26 spec string 27 expOffset, expLength int64 28 }{ 29 {"bytes=0-", 0, 10}, 30 {"bytes=1-", 1, 9}, 31 {"bytes=0-9", 0, 10}, 32 {"bytes=1-10", 1, 9}, 33 {"bytes=1-1", 1, 1}, 34 {"bytes=2-5", 2, 4}, 35 {"bytes=-5", 5, 5}, 36 {"bytes=-1", 9, 1}, 37 {"bytes=-1000", 0, 10}, 38 } 39 for i, testCase := range validRangeSpecs { 40 rs, err := parseRequestRangeSpec(testCase.spec) 41 if err != nil { 42 t.Errorf("unexpected err: %v", err) 43 } 44 o, l, err := rs.GetOffsetLength(resourceSize) 45 if err != nil { 46 t.Errorf("unexpected err: %v", err) 47 } 48 if o != testCase.expOffset || l != testCase.expLength { 49 t.Errorf("Case %d: got bad offset/length: %d,%d expected: %d,%d", 50 i, o, l, testCase.expOffset, testCase.expLength) 51 } 52 } 53 54 unparsableRangeSpecs := []string{ 55 "bytes=-", 56 "bytes==", 57 "bytes==1-10", 58 "bytes=", 59 "bytes=aa", 60 "aa", 61 "", 62 "bytes=1-10-", 63 "bytes=1--10", 64 "bytes=-1-10", 65 "bytes=0-+3", 66 "bytes=+3-+5", 67 "bytes=10-11,12-10", // Unsupported by S3/MinIO (valid in RFC) 68 } 69 for i, urs := range unparsableRangeSpecs { 70 rs, err := parseRequestRangeSpec(urs) 71 if err == nil { 72 t.Errorf("Case %d: Did not get an expected error - got %v", i, rs) 73 } 74 if err == errInvalidRange { 75 t.Errorf("Case %d: Got invalid range error instead of a parse error", i) 76 } 77 if rs != nil { 78 t.Errorf("Case %d: Got non-nil rs though err != nil: %v", i, rs) 79 } 80 } 81 82 invalidRangeSpecs := []string{ 83 "bytes=5-3", 84 "bytes=10-10", 85 "bytes=10-", 86 "bytes=100-", 87 "bytes=-0", 88 } 89 for i, irs := range invalidRangeSpecs { 90 var err1, err2 error 91 var rs *HTTPRangeSpec 92 var o, l int64 93 rs, err1 = parseRequestRangeSpec(irs) 94 if err1 == nil { 95 o, l, err2 = rs.GetOffsetLength(resourceSize) 96 } 97 if err1 == errInvalidRange || (err1 == nil && err2 == errInvalidRange) { 98 continue 99 } 100 t.Errorf("Case %d: Expected errInvalidRange but: %v %v %d %d %v", i, rs, err1, o, l, err2) 101 } 102 }