-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_handlers_test.go
140 lines (118 loc) · 3.72 KB
/
method_handlers_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package chioas
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetMethodHandlerBuilder(t *testing.T) {
mhb := getMethodHandlerBuilder(nil)
assert.Equal(t, defaultMethodHandlerBuilder, mhb)
dummy := &dummyMethodHandlerBuilder{}
mhb = getMethodHandlerBuilder(dummy)
assert.Equal(t, dummy, mhb)
}
type dummyMethodHandlerBuilder struct {
}
func (d *dummyMethodHandlerBuilder) BuildHandler(path string, method string, mdef Method, thisApi any) (http.HandlerFunc, error) {
return nil, nil
}
func TestDefaultMethodHandlerBuilder_Build_NoHandlerOrMethodNameSet(t *testing.T) {
m := Method{}
_, err := defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, nil)
assert.Error(t, err)
}
func TestDefaultMethodHandlerBuilder_Build_WithGetHandler(t *testing.T) {
m := Method{
Handler: func(path string, method string, thisApi any) (http.HandlerFunc, error) {
return nil, errors.New("foo")
},
}
mh, err := defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, nil)
assert.Error(t, err)
assert.Nil(t, mh)
}
func TestDefaultMethodHandlerBuilder_Build_WithHandlerSet(t *testing.T) {
m := Method{
Handler: func(writer http.ResponseWriter, request *http.Request) {},
}
mh, err := defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, nil)
assert.NoError(t, err)
assert.NotNil(t, mh)
var hf http.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) {}
m = Method{
Handler: hf,
}
mh, err = defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, nil)
assert.NoError(t, err)
assert.NotNil(t, mh)
}
func TestDefaultMethodHandlerBuilder_Build_WithMethodNameSet(t *testing.T) {
m := Method{
Handler: "Foo",
}
_, err := defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, nil)
assert.Error(t, err)
type dummyStruct struct{}
_, err = defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, &dummyStruct{})
assert.Error(t, err)
dummy := &dummyWithMethod{}
mh, err := defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, dummy)
assert.NoError(t, err)
assert.NotNil(t, mh)
assert.False(t, dummy.called)
req, err := http.NewRequest(http.MethodGet, "/example", nil)
require.NoError(t, err)
res := httptest.NewRecorder()
mh.ServeHTTP(res, req)
assert.True(t, dummy.called)
m = Method{
Handler: "Bar",
}
_, err = defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, dummy)
assert.Error(t, err)
m = Method{
Handler: false,
}
_, err = defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, dummy)
assert.Error(t, err)
}
type dummyWithMethod struct {
called bool
}
func (d *dummyWithMethod) Foo(writer http.ResponseWriter, request *http.Request) {
d.called = true
}
func (d *dummyWithMethod) foo(writer http.ResponseWriter, request *http.Request) {
d.called = true
}
func (d *dummyWithMethod) Bar() {
d.called = true
}
func TestDefaultMethodHandlerBuilder_Build_WithMethodExpressions(t *testing.T) {
dummy := &dummyWithMethod{}
m := Method{
Handler: (*dummyWithMethod).Foo,
}
mh, err := defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, dummy)
assert.NoError(t, err)
assert.NotNil(t, mh)
m = Method{
Handler: (*dummyWithMethod).foo,
}
_, err = defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, dummy)
assert.Error(t, err)
assert.False(t, dummy.called)
req, err := http.NewRequest(http.MethodGet, "/example", nil)
require.NoError(t, err)
res := httptest.NewRecorder()
mh.ServeHTTP(res, req)
assert.True(t, dummy.called)
m = Method{
Handler: (*dummyWithMethod).Bar,
}
_, err = defaultMethodHandlerBuilder.BuildHandler(root, http.MethodGet, m, dummy)
assert.Error(t, err)
}