-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser_accept_test.go
148 lines (138 loc) · 3.5 KB
/
parser_accept_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package mimeheader_test
import (
"fmt"
"reflect"
"testing"
"github.com/aohorodnyk/mimeheader"
)
func ExampleParseAcceptHeader() {
header := "image/*; q=0.9; s=4, application/json; q=0.9; b=3;, text/plain,image/png;q=0.9, image/jpeg,image/svg;q=0.8"
ah := mimeheader.ParseAcceptHeader(header)
fmt.Println(ah.Negotiate([]string{"application/xml", "image/tiff"}, "text/javascript"))
fmt.Println(ah.Negotiate([]string{"application/xml", "image/png"}, "text/javascript"))
fmt.Println(ah.Negotiate([]string{"application/xml", "image/svg"}, "text/javascript"))
fmt.Println(ah.Negotiate([]string{"text/dart", "application/dart"}, "text/javascript"))
fmt.Println(ah.Match("image/svg"))
fmt.Println(ah.Match("text/javascript"))
// Output:
// image/* image/tiff true
// image/png image/png true
// image/* image/svg true
// text/javascript false
// true
// false
}
func TestParseAcceptHeader(t *testing.T) {
t.Parallel()
for _, prov := range providerParseAcceptHeader() {
prov := prov
t.Run(prov.name, func(t *testing.T) {
t.Parallel()
act := mimeheader.ParseAcceptHeader(prov.header)
if !reflect.DeepEqual(prov.exp, act) {
t.Fatalf("AcceptHeaders are not equal.\nExpected: %+v\nActual: %+v", prov.exp, act)
}
})
}
}
type parseAcceptHeader struct {
name string
header string
exp mimeheader.AcceptHeader
}
func providerParseAcceptHeader() []parseAcceptHeader {
return []parseAcceptHeader{
{
name: "Empty",
header: "",
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
},
{
name: "Empty object",
header: "{}",
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
},
{
name: "Full wildcard",
header: "*/*",
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
{
MimeType: mimeheader.MimeType{
Type: "*",
Subtype: "*",
Params: map[string]string{},
},
Quality: 1.0,
},
}),
},
{
name: "Header with sorting",
header: "*/*; q=0.9; s=1, image/*; q=0.9; s=4, application/json; q=0.9; b=3;, text/plain",
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "plain",
Params: map[string]string{},
},
Quality: 1.0,
},
{
MimeType: mimeheader.MimeType{
Type: "application",
Subtype: "json",
Params: map[string]string{"q": "0.9", "b": "3"},
},
Quality: 0.9,
},
{
MimeType: mimeheader.MimeType{
Type: "image",
Subtype: "*",
Params: map[string]string{"q": "0.9", "s": "4"},
},
Quality: 0.9,
},
{
MimeType: mimeheader.MimeType{
Type: "*",
Subtype: "*",
Params: map[string]string{"q": "0.9", "s": "1"},
},
Quality: 0.9,
},
}),
},
{
name: "Lost wildcard",
header: "*/* q=0.9; s=1, image/*; q=0.9; s=4, application/json; q=0.9; b=3;, text/plain",
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "plain",
Params: map[string]string{},
},
Quality: 1.0,
},
{
MimeType: mimeheader.MimeType{
Type: "application",
Subtype: "json",
Params: map[string]string{"q": "0.9", "b": "3"},
},
Quality: 0.9,
},
{
MimeType: mimeheader.MimeType{
Type: "image",
Subtype: "*",
Params: map[string]string{"q": "0.9", "s": "4"},
},
Quality: 0.9,
},
}),
},
}
}