-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_accept_test.go
104 lines (71 loc) · 2.27 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
package negotiator
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ParseAcceptTestSuite struct {
suite.Suite
parser *headerParser
header http.Header
}
func (s *ParseAcceptTestSuite) SetupTest() {
s.header = make(http.Header)
s.parser = newHeaderParser(s.header, true)
}
func (s *ParseAcceptTestSuite) TestEmpty() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "")
specs := s.parser.parse(headerAccept)
assert.Equal(1, len(specs))
equalSpec(assert, specs[0], "*/*", 1.0)
}
func (s *ParseAcceptTestSuite) TestAsterisk() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "*/*")
specs := s.parser.parse(headerAccept)
assert.Equal(1, len(specs))
equalSpec(assert, specs[0], "*/*", 1.0)
}
func (s *ParseAcceptTestSuite) TestOneType() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "application/json")
specs := s.parser.parse(headerAccept)
assert.Equal(1, len(specs))
equalSpec(assert, specs[0], "application/json", 1.0)
}
func (s *ParseAcceptTestSuite) TestOneTypeWithQZero() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "application/json;q=0")
specs := s.parser.parse(headerAccept)
assert.Equal(0, len(specs))
}
func (s *ParseAcceptTestSuite) TestSortByQ() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "application/json;q=0.2, text/html")
specs := s.parser.parse(headerAccept)
assert.Equal(2, len(specs))
equalSpec(assert, specs[0], "text/html", 1.0)
equalSpec(assert, specs[1], "application/json", 0.2)
}
func (s *ParseAcceptTestSuite) TestSuffixAsterisk() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "text/*")
specs := s.parser.parse(headerAccept)
assert.Equal(1, len(specs))
equalSpec(assert, specs[0], "text/*", 1.0)
}
func (s *ParseAcceptTestSuite) TestSortWithAsterisk() {
assert := assert.New(s.T())
s.header.Set(headerAccept, "text/plain, application/json;q=0.5, text/html, */*;q=0.1")
specs := s.parser.parse(headerAccept)
assert.Equal(4, len(specs))
equalSpec(assert, specs[0], "text/plain", 1.0)
equalSpec(assert, specs[1], "text/html", 1.0)
equalSpec(assert, specs[2], "application/json", 0.5)
equalSpec(assert, specs[3], "*/*", 0.1)
}
func TestParseAccept(t *testing.T) {
suite.Run(t, new(ParseAcceptTestSuite))
}