-
Notifications
You must be signed in to change notification settings - Fork 19
/
inejctor_test.go
174 lines (134 loc) · 3.47 KB
/
inejctor_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package gongular
import (
"database/sql"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type injectionDirectHandler struct {
Param struct {
UserID uint
}
Database *sql.DB
}
func (i *injectionDirectHandler) Handle(c *Context) error {
c.SetBody(fmt.Sprintf("%p:%d", i.Database, i.Param.UserID))
return nil
}
func TestInjectDirect(t *testing.T) {
e := newEngineTest()
db := new(sql.DB)
e.Provide(db)
e.GetRouter().GET("/my/db/interaction/:UserID", &injectionDirectHandler{})
resp, content := get(t, e, "/my/db/interaction/5")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, fmt.Sprintf(`"%p:5"`, db), content)
}
type injectKey struct {
Val1 int `inject:"val1"`
Val2 int `inject:"val2"`
}
func (i *injectKey) Handle(c *Context) error {
c.SetBody(i.Val1 * i.Val2)
return nil
}
func TestInjectKey(t *testing.T) {
e := newEngineTest()
e.ProvideWithKey("val1", 71)
e.ProvideWithKey("val2", 97)
e.GetRouter().GET("/", &injectKey{})
resp, content := get(t, e, "/")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `6887`, content)
}
type injectCustom struct {
DB *sql.DB
}
func (i *injectCustom) Handle(c *Context) error {
c.SetBody(fmt.Sprintf("%p", i.DB))
return nil
}
func TestInjectCustom(t *testing.T) {
e := newEngineTest()
var d *sql.DB
e.CustomProvide(&sql.DB{}, func(c *Context) (interface{}, error) {
d = new(sql.DB)
return d, nil
})
e.GetRouter().GET("/", &injectCustom{})
resp1, content1 := get(t, e, "/")
assert.Equal(t, http.StatusOK, resp1.Code)
assert.Equal(t, fmt.Sprintf(`"%p"`, d), content1)
// Again
resp2, content2 := get(t, e, "/")
assert.Equal(t, http.StatusOK, resp2.Code)
assert.Equal(t, fmt.Sprintf(`"%p"`, d), content2)
}
type injectCustomCache1 struct {
DB *sql.DB
}
type injectCustomCache2 struct {
DB *sql.DB
}
func (i *injectCustomCache1) Handle(c *Context) error {
c.logger.Printf("%p", i.DB)
return nil
}
func (i *injectCustomCache2) Handle(c *Context) error {
c.SetBody(fmt.Sprintf("%p", i.DB))
return nil
}
func TestInjectCustomCache(t *testing.T) {
e := newEngineTest()
var d *sql.DB
callCount := 0
e.CustomProvide(&sql.DB{}, func(c *Context) (interface{}, error) {
d = new(sql.DB)
callCount++
return d, nil
})
e.GetRouter().GET("/", &injectCustomCache1{}, &injectCustomCache2{})
resp1, content1 := get(t, e, "/")
assert.Equal(t, http.StatusOK, resp1.Code)
assert.Equal(t, fmt.Sprintf(`"%p"`, d), content1)
assert.Equal(t, 1, callCount)
// Again
resp2, content2 := get(t, e, "/")
assert.Equal(t, http.StatusOK, resp2.Code)
assert.Equal(t, fmt.Sprintf(`"%p"`, d), content2)
assert.Equal(t, 2, callCount)
}
type dummyInterface interface {
getId() int
}
type dummyInterfaceImpl struct {
id int
}
func (d dummyInterfaceImpl) getId() int {
return d.id * 5
}
type injectionInterfaceHandler struct {
Param struct {
UserID uint
}
Dummy dummyInterface `inject:"key1"`
}
func getDummyInterface() dummyInterface {
return dummyInterfaceImpl{
id: 5,
}
}
func (i *injectionInterfaceHandler) Handle(c *Context) error {
c.SetBody(fmt.Sprintf("%d:%d", i.Dummy.getId(), i.Param.UserID))
return nil
}
func TestInjectInterface(t *testing.T) {
e := newEngineTest()
e.errorHandler = defaultErrorHandler
e.ProvideUnsafe("key1", getDummyInterface())
e.GetRouter().GET("/my/interface/interaction/:UserID", &injectionInterfaceHandler{})
resp, content := get(t, e, "/my/interface/interaction/5")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `"25:5"`, content)
}