-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_test.go
107 lines (91 loc) · 2.9 KB
/
registry_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
package negotiator
import (
"reflect"
"testing"
)
const (
appJSON = "application/json"
appXML = "application/xml"
)
type testGeneric struct {
X int
}
type testSpecific struct {
X int
Y int
}
func TestRegistryNegotiate(t *testing.T) {
testReg := NewRegistry()
testReg.Register("application/json", testGeneric{})
testReg.Register("application/vnd.dyn.zone+json", &testSpecific{})
testReg.Register("application/xhtml+xml", &testSpecific{})
testReg.Register("*/*", &testGeneric{})
testio := []struct {
inp string
expected interface{}
err string
}{
{"application/json", testGeneric{}, ""},
{"application/xml", nil, ErrNoContentType.Error()},
{"application/json;foo", nil, ErrInvalidAcceptParam.Error()},
{"*/*,application/json,application/vnd.dyn.zone+json;format=foo,application/*",
testSpecific{}, ""},
{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8",
testSpecific{}, ""},
{"text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, /;q=0.8",
testSpecific{}, ""},
{"image/jpeg, image/webp, */*",
testGeneric{}, ""},
}
var i interface{}
var err error
for _, test := range testio {
i, _, err = testReg.Negotiate(test.inp)
if i != nil && test.expected == nil {
t.Errorf("Expected %s to return nil. Got %+v instead", test.expected, i)
}
if i != test.expected {
seen := reflect.TypeOf(i)
expected := reflect.TypeOf(test.expected)
t.Errorf("Expected type %s, got %s instead. Header (%s)", expected, seen, test.inp)
}
if len(test.err) > 0 && err != nil && test.err != err.Error() {
t.Errorf("Expected error %q, got %q instead", test.err, err.Error())
} else if len(test.err) > 0 && err == nil {
t.Errorf("Expected an error, but got success from header %s", test.inp)
}
}
}
func TestRegistryContentType(t *testing.T) {
testReg := NewRegistry()
testReg.Register("application/json", testGeneric{})
testReg.Register("application/vnd.dyn.zone+json", &testSpecific{})
testio := []struct {
inp string
expected interface{}
err string
}{
{"application/json", testGeneric{}, ""},
{"application/xml", nil, ErrNoContentType.Error()},
{"application/xml/json/ foobar", nil,
"mime: unexpected content after media subtype"},
}
var i interface{}
var err error
for _, test := range testio {
i, _, err = testReg.ContentType(test.inp)
if i != nil && test.expected == nil {
t.Errorf("Expected %s to return nil. Got %+v instead", test.expected, i)
}
if i != test.expected {
seen := reflect.TypeOf(i)
expected := reflect.TypeOf(test.expected)
t.Errorf("Expected type %s, got %s instead. Header (%s)", expected, seen, test.inp)
}
if len(test.err) > 0 && err != nil && test.err != err.Error() {
t.Errorf("Expected error %q, got %q instead", test.err, err.Error())
} else if len(test.err) > 0 && err == nil {
t.Errorf("Expected an error, but got success from header %s", test.inp)
}
}
}