forked from jchannon/negotiator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptheader_test.go
158 lines (117 loc) · 4.47 KB
/
acceptheader_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
package negotiator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseMediaRanges_parses_single(t *testing.T) {
a := accept("application/json")
mr := a.ParseMediaRanges()
assert.Equal(t, 1, len(mr))
assert.Equal(t, "application/json", mr[0].Value)
assert.Equal(t, TypeSubtypeMediaRangeWeight, mr[0].Weight)
}
func TestParseMediaRanges_preserves_case_of_mediaRange(t *testing.T) {
a := accept("application/CEA")
mr := a.ParseMediaRanges()
assert.Equal(t, 1, len(mr))
assert.Equal(t, "application/CEA", mr[0].Value)
}
func TestParseMediaRanges_defaults_quality_if_not_explicit(t *testing.T) {
a := accept("text/plain")
mr := a.ParseMediaRanges()
assert.Equal(t, 1, len(mr))
assert.Equal(t, TypeSubtypeMediaRangeWeight, mr[0].Weight)
}
func TestParseMediaRanges_should_parse_quality(t *testing.T) {
a := accept("application/json;q=0.9")
mr := a.ParseMediaRanges()
assert.Equal(t, 1, len(mr))
assert.Equal(t, "application/json", mr[0].Value)
assert.Equal(t, 0.9, mr[0].Weight)
}
func TestParseMediaRanges_should_parse_multi_qualities(t *testing.T) {
a := accept("application/xml;q=1, application/json;q=0.9")
mr := a.ParseMediaRanges()
assert.Equal(t, 2, len(mr))
assert.Equal(t, "application/xml", mr[0].Value)
assert.Equal(t, 1.0, mr[0].Weight)
assert.Equal(t, "application/json", mr[1].Value)
assert.Equal(t, 0.9, mr[1].Weight)
}
func TestParseMediaRanges_reorders_by_quality_decending(t *testing.T) {
a := accept("application/json;q=0.8, application/xml")
mr := a.ParseMediaRanges()
assert.Equal(t, 2, len(mr))
assert.Equal(t, "application/xml", mr[0].Value)
assert.Equal(t, TypeSubtypeMediaRangeWeight, mr[0].Weight)
assert.Equal(t, "application/json", mr[1].Value)
assert.Equal(t, 0.8, mr[1].Weight)
}
func TestMediaRanges_should_ignore_invalid_quality(t *testing.T) {
a := accept("text/html;q=blah")
mr := a.ParseMediaRanges()
assert.Equal(t, 1, len(mr))
assert.Equal(t, "text/html", mr[0].Value)
assert.Equal(t, ParameteredMediaRangeWeight, mr[0].Weight)
}
func TestMediaRanges_should_not_remove_accept_extension(t *testing.T) {
a := accept("text/html;q=0.5;a=1;b=2")
mr := a.ParseMediaRanges()
assert.Equal(t, 1, len(mr))
assert.Equal(t, "text/html;a=1;b=2", mr[0].Value)
assert.Equal(t, 0.5, mr[0].Weight)
}
func TestMediaRanges_should_handle_precedence(t *testing.T) {
a := accept("text/*, text/html, text/html;level=1, */*")
mr := a.ParseMediaRanges()
assert.Equal(t, "text/html;level=1", mr[0].Value)
assert.Equal(t, "text/html", mr[1].Value)
assert.Equal(t, "text/*", mr[2].Value)
assert.Equal(t, "*/*", mr[3].Value)
}
func TestMediaRanges_should_handle_precedence2(t *testing.T) {
a := accept("text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5")
mr := a.ParseMediaRanges()
assert.Equal(t, 5, len(mr))
assert.Equal(t, "text/html;level=1", mr[0].Value)
assert.Equal(t, 1.0, mr[0].Weight)
assert.Equal(t, "text/html", mr[1].Value)
assert.Equal(t, 0.7, mr[1].Weight)
assert.Equal(t, "*/*", mr[2].Value)
assert.Equal(t, 0.5, mr[2].Weight)
assert.Equal(t, "text/html;level=2", mr[3].Value)
assert.Equal(t, 0.4, mr[3].Weight)
assert.Equal(t, "text/*", mr[4].Value)
assert.Equal(t, 0.3, mr[4].Weight)
}
func TestMediaRanges_should_handle_precedence3(t *testing.T) {
// from http://tools.ietf.org/html/rfc7231#section-5.3.2
a := accept("text/*, text/plain, text/plain;format=flowed, */*")
mr := a.ParseMediaRanges()
assert.Equal(t, 4, len(mr))
assert.Equal(t, "text/plain;format=flowed", mr[0].Value)
assert.Equal(t, 1.0, mr[0].Weight)
assert.Equal(t, "text/plain", mr[1].Value)
assert.Equal(t, 0.9, mr[1].Weight)
assert.Equal(t, "text/*", mr[2].Value)
assert.Equal(t, 0.8, mr[2].Weight)
assert.Equal(t, "*/*", mr[3].Value)
assert.Equal(t, 0.7, mr[3].Weight)
}
func TestMediaRanges_should_handle_precedence4(t *testing.T) {
// from http://tools.ietf.org/html/rfc7231#section-5.3.1
// and http://tools.ietf.org/html/rfc7231#section-5.3.2
a := accept("text/* ; q=0.3, text/html ; Q=0.7, text/html;level=1, text/html;level=2; q=0.4, */*; q=0.5")
mr := a.ParseMediaRanges()
assert.Equal(t, 5, len(mr))
assert.Equal(t, "text/html;level=1", mr[0].Value)
assert.Equal(t, 1.0, mr[0].Weight)
assert.Equal(t, "text/html", mr[1].Value)
assert.Equal(t, 0.7, mr[1].Weight)
assert.Equal(t, "*/*", mr[2].Value)
assert.Equal(t, 0.5, mr[2].Weight)
assert.Equal(t, "text/html;level=2", mr[3].Value)
assert.Equal(t, 0.4, mr[3].Weight)
assert.Equal(t, "text/*", mr[4].Value)
assert.Equal(t, 0.3, mr[4].Weight)
}