-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_test.go
86 lines (68 loc) · 1.56 KB
/
bind_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
package httpheader
import (
"net/http"
"testing"
)
func createHeader(omit ...string) http.Header {
headers := map[string][]string{
"Foo": {"Bar"},
"Bar": {"Foo", "42"},
}
for _, headerToOmit := range omit {
delete(headers, headerToOmit)
}
return headers
}
func TestBind_nil(t *testing.T) {
err := Bind(http.Header{}, nil)
checkForInvalidBindError(t, err)
}
func TestBind_nonPointerType(t *testing.T) {
err := Bind(http.Header{}, "")
checkForInvalidBindError(t, err)
}
func TestBind_String(t *testing.T) {
type Test struct {
Foo string `header:"Foo"`
}
var test Test
err := Bind(createHeader(), &test)
if err != nil {
t.Error(err)
return
}
expected := "Bar"
actual := test.Foo
if actual != expected {
t.Errorf("Expected field to have value '%s', but got '%s'...", expected, actual)
}
}
func TestBind_StringSlice(t *testing.T) {
type Test struct {
Bar []string `header:"Bar"`
}
var test Test
err := Bind(createHeader(), &test)
if err != nil {
t.Error(err)
return
}
if len(test.Bar) != 2 {
t.Errorf("Expected two values, but got %d: %s", len(test.Bar), test.Bar)
return
}
if test.Bar[0] != "Foo" {
t.Errorf("Expected first element to be 'Foo', but was '%s'...", test.Bar[0])
}
if test.Bar[1] != "42" {
t.Errorf("Expected first element to be '42', but was '%s'...", test.Bar[1])
}
}
func checkForInvalidBindError(t *testing.T, err error) {
if err == nil {
t.Errorf("Expected an error but got nil.")
}
if _, ok := err.(*InvalidBindError); !ok {
t.Error("Expected InvalidBindError, but got something different")
}
}