-
-
Notifications
You must be signed in to change notification settings - Fork 583
Expand file tree
/
Copy pathrequest_binder_test.go
More file actions
71 lines (54 loc) · 1.61 KB
/
request_binder_test.go
File metadata and controls
71 lines (54 loc) · 1.61 KB
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
package binding
import (
"errors"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_RequestBinder_Exec(t *testing.T) {
r := require.New(t)
var used bool
BaseRequestBinder.Register("paganotoni/test", func(*http.Request, any) error {
used = true
return nil
})
req, err := http.NewRequest("GET", "/home", strings.NewReader(""))
req.Header.Add("content-type", "paganotoni/test")
r.NoError(err)
data := &struct{}{}
r.NoError(BaseRequestBinder.Exec(req, data))
r.True(used)
}
func Test_RequestBinder_Exec_BlankContentType(t *testing.T) {
r := require.New(t)
req, err := http.NewRequest("GET", "/home", strings.NewReader(""))
r.NoError(err)
data := &struct{}{}
r.Equal(BaseRequestBinder.Exec(req, data), errBlankContentType)
}
func Test_RequestBinder_Exec_Bindable(t *testing.T) {
r := require.New(t)
BaseRequestBinder.Register("paganotoni/orbison", func(req *http.Request, val any) error {
switch v := val.(type) {
case orbison:
v.bound = false
}
return errors.New("this should not be called")
})
req, err := http.NewRequest("GET", "/home", strings.NewReader(""))
req.Header.Add("content-type", "paganotoni/orbison")
r.NoError(err)
data := &orbison{}
r.NoError(BaseRequestBinder.Exec(req, data))
r.True(data.bound)
}
func Test_RequestBinder_Exec_NoBinder(t *testing.T) {
r := require.New(t)
req, err := http.NewRequest("GET", "/home", strings.NewReader(""))
req.Header.Add("content-type", "paganotoni/other")
r.NoError(err)
err = BaseRequestBinder.Exec(req, &struct{}{})
r.Error(err)
r.Equal(err.Error(), "could not find a binder for paganotoni/other")
}