-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept_test.go
174 lines (153 loc) · 4.42 KB
/
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package negotiator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMediaRange(t *testing.T) {
testio := []struct {
inp mediaRange
typ string
subtyp string
suffix string
}{
{mediaRange("application/json"), "application", "json", ""},
{mediaRange("application/*"), "application", "*", ""},
{mediaRange("*/*"), "*", "*", ""},
{mediaRange("application/json;indent=4"), "application", "json", ""},
{mediaRange("application/resource+json;indent=4"), "application", "resource+json", "json"},
{mediaRange("application resource"), "", "", ""},
}
for _, test := range testio {
t.Run(test.inp.Type(), func(t *testing.T) {
assert.Equal(t, test.typ, test.inp.Type(), "Types did not match")
assert.Equal(t, test.subtyp, test.inp.SubType(), "Subtypes did not match")
assert.Equal(t, test.suffix, test.inp.Suffix(), "Suffixes did not match")
})
}
}
func TestBadMediaRange(t *testing.T) {
testio := []struct {
inp string
err error
}{
{"application/json", nil},
{"application/*", nil},
{"*/*", nil},
{"application/json;indent=4", nil},
{"application/resource+json;indent=4", nil},
{"application resource", ErrInvalidMediaRange},
}
for _, test := range testio {
t.Run(test.inp, func(t *testing.T) {
if _, err := ParseAccept(test.inp); err != test.err {
t.Errorf("Expected %s, got %s", test.err, err)
}
})
}
}
func TestAcceptParams(t *testing.T) {
testio := []struct {
inp string
expected map[string]string
}{
{"application/json", map[string]string{}},
{"application/json;indent=4",
map[string]string{"indent": "4"}},
{"application/json;indent=4; charset=utf8",
map[string]string{"indent": "4", "charset": "utf8"}},
}
for _, test := range testio {
t.Run(test.inp, func(t *testing.T) {
acpt, err := ParseAccept(test.inp)
assert.Nil(t, err, "Unable to parse valid header: %s", test.inp)
for k, v := range test.expected {
assert.Contains(t, acpt.AcceptParams, k,
"Expected key %s not found in parsed params", k)
assert.Equal(t, acpt.AcceptParams[k], v,
"expected %s: %s to equal %s", k, acpt.AcceptParams[k], v)
}
})
}
}
func TestBadParams(t *testing.T) {
testio := []struct {
inp string
fail bool
}{
{"application/json", false},
{"application/json;q=0.3", false},
{"application/json;foo=bar", false},
{"application/json;foobar", true},
}
for _, test := range testio {
t.Run(test.inp, func(t *testing.T) {
_, err := ParseAccept(test.inp)
failed := err == nil && test.fail == true
assert.False(t, failed,
"Expected header %s to contain a bad header param", test.inp)
})
}
}
func TestAcceptQuality(t *testing.T) {
testio := []struct {
inp string
expected float64
}{
{"application/json", 0.9},
{"application/json;q=0.3", 0.3},
{"application/json;indent=4", 1.0},
{"application/json;indent=4;q=0.7", 0.7},
{"application/json;indent=4; q=0.4", 0.4},
}
for _, test := range testio {
t.Run(test.inp, func(t *testing.T) {
acpt, err := ParseAccept(test.inp)
assert.Nil(t, err, "Unable to parse valid header: %s", test.inp)
assert.Equal(t, test.expected, acpt.Quality,
"Expected quality of %f, got %f instead", test.expected, acpt.Quality)
})
}
}
func TestBadQuality(t *testing.T) {
testio := []struct {
inp string
fail bool
}{
{"application/json", false},
{"application/json;q=0.3", false},
{"application/json;q=1", false},
{"application/json;q=foobar", true},
}
for _, test := range testio {
t.Run(test.inp, func(t *testing.T) {
_, err := ParseAccept(test.inp)
failed := err == nil && test.fail == true
assert.False(t, failed,
"Expected header %s to contain a bad quality value", test.inp)
})
}
}
func TestAcceptExtensions(t *testing.T) {
testio := []struct {
inp string
expected map[string]string
}{
{"application/json", map[string]string{}},
{"application/json;indent=4;q=1.0;version=1",
map[string]string{"version": "1"}},
{"application/json;indent=4; q=1.0; version=2",
map[string]string{"version": "2"}},
}
for _, test := range testio {
t.Run(test.inp, func(t *testing.T) {
acpt, err := ParseAccept(test.inp)
assert.Nil(t, err, "Unable to parse valid header: %s", test.inp)
for k, v := range test.expected {
assert.Contains(t, acpt.AcceptExt, k,
"Expected key %s not found in parsed params", k)
assert.Equal(t, acpt.AcceptExt[k], v,
"expected %s: %s to equal %s", k, acpt.AcceptExt[k], v)
}
})
}
}