-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_test.go
95 lines (90 loc) · 1.89 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
87
88
89
90
91
92
93
94
95
package hyper
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"testing"
)
var (
ErrMissingRequestBody = errors.New("missing request body")
)
type TestRequest struct {
Name string `json:"name"`
Age int `json:"age"`
}
func (tr TestRequest) Compare(t *testing.T, req TestRequest) {
if tr.Name != req.Name {
t.Errorf("Request name should be %s, instead of %s", tr.Name, req.Name)
}
if tr.Age != req.Age {
t.Errorf("Request age should be %d, instead of %d", tr.Age, req.Age)
}
}
func TestBind(t *testing.T) {
var scenarios = []struct {
name string
in string
expected TestRequest
errExpected bool
err error
}{
{
name: "fully filled request",
in: `{"name":"John","age":44}`,
expected: TestRequest{
Name: "John",
Age: 44,
},
errExpected: false,
},
{
name: "missing number field",
in: `{"name":"John"}`,
expected: TestRequest{
Name: "John",
Age: 0,
},
errExpected: false,
},
{
name: "empty object",
in: `{}`,
expected: TestRequest{
Name: "",
Age: 0,
},
errExpected: false,
},
{
name: "null",
in: `null`,
errExpected: false,
},
{
name: "missing body",
in: "",
errExpected: true,
err: ErrMissingRequestBody,
},
}
ctx := context.Background()
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
var reqBody io.Reader
if scenario.in != "" {
reqBody = bytes.NewBufferString(scenario.in)
}
req, err := http.NewRequest(http.MethodPost, "http://0.0.0.0:9999", reqBody)
if err != nil {
t.Fatal(err)
}
var body TestRequest
if err := Bind(ctx, req, &body); (err != nil) != scenario.errExpected && !errors.Is(err, scenario.err) {
t.Errorf("Expected error is %v but we got %v", scenario.errExpected, err)
}
scenario.expected.Compare(t, body)
})
}
}