-
Notifications
You must be signed in to change notification settings - Fork 3
/
accept_header_negotiate_test.go
116 lines (105 loc) · 3.77 KB
/
accept_header_negotiate_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
package mimeheader_test
import (
"fmt"
"testing"
"github.com/aohorodnyk/mimeheader"
)
func ExampleAcceptHeader_Negotiate() {
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"))
// Output:
// image/* image/tiff true
// image/png image/png true
// image/* image/svg true
// text/javascript false
}
func TestAcceptHeader_Negotiate(t *testing.T) {
t.Parallel()
for _, prov := range providerAcceptHeaderNegotiate() {
prov := prov
t.Run(prov.name, func(t *testing.T) {
t.Parallel()
actHeader, actMType, actMatched := prov.ah.Negotiate(prov.ctypes, prov.dtype)
if actHeader.String() != prov.expHeader.String() {
t.Errorf("Wrong header matched.\nExpected: %v\nActual: %v", prov.expHeader, actHeader)
}
if actMType != prov.expMType {
t.Errorf("Wrong mime type returned.\nExpected: %s\nActual: %s", prov.expMType, actMType)
}
if actMatched != prov.expMatched {
t.Errorf("Unepected match result.\nExpected: %t\nActual: %t", prov.expMatched, actMatched)
}
})
}
}
type acceptHeaderNegotiate struct {
name string
ah mimeheader.AcceptHeader
ctypes []string
dtype string
expHeader mimeheader.MimeHeader
expMType string
expMatched bool
}
func providerAcceptHeaderNegotiate() []acceptHeaderNegotiate {
return []acceptHeaderNegotiate{
{
name: "Empty ctypes",
ah: mimeheader.ParseAcceptHeader(""),
ctypes: []string{},
dtype: "text/plain",
expHeader: mimeheader.MimeHeader{},
expMType: "text/plain",
expMatched: false,
},
{
name: "Wrong ctypes",
ah: mimeheader.ParseAcceptHeader("*/*"),
ctypes: []string{"application/json;param="},
dtype: "text/plain",
expHeader: mimeheader.MimeHeader{},
expMType: "text/plain",
expMatched: false,
},
{
name: "Wildcard ctypes",
ah: mimeheader.ParseAcceptHeader("*/*"),
ctypes: []string{"application/json;param=1"},
dtype: "text/plain",
expHeader: mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "*", Subtype: "*"}},
expMType: "application/json",
expMatched: true,
},
{
name: "Sorted list of types with the same structure image/png",
ah: mimeheader.ParseAcceptHeader("application/json;q=1.0,*/*;q=1.0; param=wild,image/png;q=1.0;param=test"),
ctypes: []string{"application/json;param=1", "image/png"},
dtype: "text/plain",
expHeader: mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "image", Subtype: "png"}},
expMType: "image/png",
expMatched: true,
},
{
name: "Sorted list of types with the same structure */*",
ah: mimeheader.ParseAcceptHeader("application/json;q=1.0,*/*;q=1.0; param=wild,image/png;q=1.0;param=test"),
ctypes: []string{"application/xml;param=1", "text/plain"},
dtype: "text/javascript",
expHeader: mimeheader.MimeHeader{MimeType: mimeheader.MimeType{Type: "*", Subtype: "*"}},
expMType: "application/xml",
expMatched: true,
},
{
name: "Sorted list of types with the same structure",
ah: mimeheader.ParseAcceptHeader("application/json;q=1.0,image/*;q=1.0;param=test"),
ctypes: []string{"test/xml;param=1", "text/plain"},
dtype: "text/javascript",
expHeader: mimeheader.MimeHeader{},
expMType: "text/javascript",
expMatched: false,
},
}
}