11package acceptHeaders
22
3- import "testing"
3+ import (
4+ "reflect"
5+ "testing"
6+ )
7+
8+ func TestParseAccept (t * testing.T ) {
9+ accept := "text/html, */*;q=0.6, text/plain;q=0.9, application/json;q=0.7, image/*;q=0.8, image/png;q=0.8"
10+ accepts := ParseAccepts (accept )
11+ if len (accepts ) != 6 {
12+ t .Error (len (accepts ))
13+ }
14+
15+ if ! reflect .DeepEqual (accepts [0 ], acceptItem {"text/html" , 1000 , 0 }) {
16+ t .Error (accepts [0 ])
17+ }
18+ if ! reflect .DeepEqual (accepts [1 ], acceptItem {"text/plain" , 900 , 0 }) {
19+ t .Error (accepts [1 ])
20+ }
21+ if ! reflect .DeepEqual (accepts [2 ], acceptItem {"image/png" , 800 , 0 }) {
22+ t .Error (accepts [2 ])
23+ }
24+ if ! reflect .DeepEqual (accepts [3 ], acceptItem {"image/*" , 800 , 1 }) {
25+ t .Error (accepts [3 ])
26+ }
27+ if ! reflect .DeepEqual (accepts [4 ], acceptItem {"application/json" , 700 , 0 }) {
28+ t .Error (accepts [4 ])
29+ }
30+ if ! reflect .DeepEqual (accepts [5 ], acceptItem {"*/*" , 600 , 2 }) {
31+ t .Error (accepts [5 ])
32+ }
33+
34+ var index int
35+ var preferred string
36+
37+ index , preferred , _ = accepts .GetPreferredValue ([]string {"text/plain" , "text/html" })
38+ if index != 1 {
39+ t .Error (index )
40+ }
41+ if preferred != "text/html" {
42+ t .Error (preferred )
43+ }
44+
45+ index , preferred , _ = accepts .GetPreferredValue ([]string {"image/jpeg" , "image/png" })
46+ if index != 1 {
47+ t .Error (index )
48+ }
49+ if preferred != "image/png" {
50+ t .Error (preferred )
51+ }
52+
53+ index , preferred , _ = accepts .GetPreferredValue ([]string {"image/png" , "image/jpeg" })
54+ if index != 0 {
55+ t .Error (index )
56+ }
57+ if preferred != "image/png" {
58+ t .Error (preferred )
59+ }
60+
61+ index , preferred , _ = accepts .GetPreferredValue ([]string {"image/webp" , "image/jpeg" })
62+ if index != 0 {
63+ t .Error (index )
64+ }
65+ if preferred != "image/webp" {
66+ t .Error (preferred )
67+ }
68+ }
469
5- func TestParseAccepts (t * testing.T ) {
70+ func TestParseAcceptLanguage (t * testing.T ) {
671 acceptLanguage := "zh;q=0.9,zh-CN,en;q=0.7,en-US;q=0.8"
772 accepts := ParseAccepts (acceptLanguage )
873 if len (accepts ) != 4 {
@@ -38,7 +103,7 @@ func TestParseAccepts(t *testing.T) {
38103 }
39104}
40105
41- func TestParseAccepts2 (t * testing.T ) {
106+ func TestParseAcceptEncoding (t * testing.T ) {
42107 acceptEncoding := "gzip, deflate"
43108 accepts := ParseAccepts (acceptEncoding )
44109
@@ -61,7 +126,7 @@ func TestParseAccepts2(t *testing.T) {
61126 }
62127}
63128
64- func TestGetPreferredValue (t * testing.T ) {
129+ func TestParseAcceptEncoding2 (t * testing.T ) {
65130 acceptEncoding := "gzip;v=b3;q=0.9, deflate"
66131 accepts := ParseAccepts (acceptEncoding )
67132
0 commit comments