gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/range_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gmhttp 6 7 import ( 8 "testing" 9 ) 10 11 var ParseRangeTests = []struct { 12 s string 13 length int64 14 r []httpRange 15 }{ 16 {"", 0, nil}, 17 {"", 1000, nil}, 18 {"foo", 0, nil}, 19 {"bytes=", 0, nil}, 20 {"bytes=7", 10, nil}, 21 {"bytes= 7 ", 10, nil}, 22 {"bytes=1-", 0, nil}, 23 {"bytes=5-4", 10, nil}, 24 {"bytes=0-2,5-4", 10, nil}, 25 {"bytes=2-5,4-3", 10, nil}, 26 {"bytes=--5,4--3", 10, nil}, 27 {"bytes=A-", 10, nil}, 28 {"bytes=A- ", 10, nil}, 29 {"bytes=A-Z", 10, nil}, 30 {"bytes= -Z", 10, nil}, 31 {"bytes=5-Z", 10, nil}, 32 {"bytes=Ran-dom, garbage", 10, nil}, 33 {"bytes=0x01-0x02", 10, nil}, 34 {"bytes= ", 10, nil}, 35 {"bytes= , , , ", 10, nil}, 36 37 {"bytes=0-9", 10, []httpRange{{0, 10}}}, 38 {"bytes=0-", 10, []httpRange{{0, 10}}}, 39 {"bytes=5-", 10, []httpRange{{5, 5}}}, 40 {"bytes=0-20", 10, []httpRange{{0, 10}}}, 41 {"bytes=15-,0-5", 10, []httpRange{{0, 6}}}, 42 {"bytes=1-2,5-", 10, []httpRange{{1, 2}, {5, 5}}}, 43 {"bytes=-2 , 7-", 11, []httpRange{{9, 2}, {7, 4}}}, 44 {"bytes=0-0 ,2-2, 7-", 11, []httpRange{{0, 1}, {2, 1}, {7, 4}}}, 45 {"bytes=-5", 10, []httpRange{{5, 5}}}, 46 {"bytes=-15", 10, []httpRange{{0, 10}}}, 47 {"bytes=0-499", 10000, []httpRange{{0, 500}}}, 48 {"bytes=500-999", 10000, []httpRange{{500, 500}}}, 49 {"bytes=-500", 10000, []httpRange{{9500, 500}}}, 50 {"bytes=9500-", 10000, []httpRange{{9500, 500}}}, 51 {"bytes=0-0,-1", 10000, []httpRange{{0, 1}, {9999, 1}}}, 52 {"bytes=500-600,601-999", 10000, []httpRange{{500, 101}, {601, 399}}}, 53 {"bytes=500-700,601-999", 10000, []httpRange{{500, 201}, {601, 399}}}, 54 55 // Match Apache laxity: 56 {"bytes= 1 -2 , 4- 5, 7 - 8 , ,,", 11, []httpRange{{1, 2}, {4, 2}, {7, 2}}}, 57 } 58 59 func TestParseRange(t *testing.T) { 60 for _, test := range ParseRangeTests { 61 r := test.r 62 ranges, err := parseRange(test.s, test.length) 63 if err != nil && r != nil { 64 t.Errorf("parseRange(%q) returned error %q", test.s, err) 65 } 66 if len(ranges) != len(r) { 67 t.Errorf("len(parseRange(%q)) = %d, want %d", test.s, len(ranges), len(r)) 68 continue 69 } 70 for i := range r { 71 if ranges[i].start != r[i].start { 72 t.Errorf("parseRange(%q)[%d].start = %d, want %d", test.s, i, ranges[i].start, r[i].start) 73 } 74 if ranges[i].length != r[i].length { 75 t.Errorf("parseRange(%q)[%d].length = %d, want %d", test.s, i, ranges[i].length, r[i].length) 76 } 77 } 78 } 79 }