-
Notifications
You must be signed in to change notification settings - Fork 3
/
accept_header_add_test.go
160 lines (150 loc) · 3.79 KB
/
accept_header_add_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
149
150
151
152
153
154
155
156
157
158
159
160
package mimeheader_test
import (
"fmt"
"reflect"
"testing"
"github.com/aohorodnyk/mimeheader"
)
func ExampleAcceptHeader_Add() {
ah := mimeheader.ParseAcceptHeader("image/png")
fmt.Println(ah.Match("application/json"))
ah.Add(mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "application", Subtype: "*"}})
fmt.Println(ah.Match("application/json"))
// Output:
// false
// true
}
func TestAcceptHeader_Add(t *testing.T) {
t.Parallel()
for _, prov := range providerAcceptHeaderAdd() {
prov := prov
t.Run(prov.name, func(t *testing.T) {
t.Parallel()
prov.act.Add(prov.add)
if !reflect.DeepEqual(prov.exp, prov.act) {
t.Fatalf("Add data has unexpected result.\nExpected: %+v\nActual: %+v", prov.exp, prov.act)
}
})
}
}
type acceptHeaderAdd struct {
name string
act mimeheader.AcceptHeader
add mimeheader.MimeHeader
exp mimeheader.AcceptHeader
}
func providerAcceptHeaderAdd() []acceptHeaderAdd {
return []acceptHeaderAdd{
{
name: "Add empty header to empty AcceptHeader",
act: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
add: mimeheader.MimeHeader{},
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
},
{
name: "Add wrong subtype header to empty AcceptHeader",
act: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
add: mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "text"}},
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
},
{
name: "Add wrong type header to empty AcceptHeader",
act: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
add: mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Subtype: "plain"}},
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
},
{
name: "Add a header to empty AcceptHeader",
act: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
add: mimeheader.MimeHeader{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "*",
Params: map[string]string{"q": "1.0"},
},
Quality: 1.0,
},
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "*",
Params: map[string]string{"q": "1.0"},
},
Quality: 1.0,
},
}),
},
{
name: "Add a header to an AcceptHeader",
act: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "javascript",
Params: map[string]string{"q": "0.9"},
},
Quality: 1.0,
},
{
MimeType: mimeheader.MimeType{
Type: "application",
Subtype: "json",
Params: map[string]string{"q": "0.9"},
},
Quality: 0.9,
},
{
MimeType: mimeheader.MimeType{
Type: "application",
Subtype: "xml",
Params: map[string]string{},
},
Quality: 1.0,
},
}),
add: mimeheader.MimeHeader{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "*",
Params: map[string]string{},
},
Quality: 1.0,
},
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "javascript",
Params: map[string]string{"q": "0.9"},
},
Quality: 1.0,
},
{
MimeType: mimeheader.MimeType{
Type: "application",
Subtype: "xml",
Params: map[string]string{},
},
Quality: 1.0,
},
{
MimeType: mimeheader.MimeType{
Type: "text",
Subtype: "*",
Params: map[string]string{},
},
Quality: 1.0,
},
{
MimeType: mimeheader.MimeType{
Type: "application",
Subtype: "json",
Params: map[string]string{"q": "0.9"},
},
Quality: 0.9,
},
}),
},
}
}