/
test_helpers.go
77 lines (69 loc) · 1.77 KB
/
test_helpers.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
package api_v1
import (
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"testing"
)
type group interface {
GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes
}
type mockHandler struct {
// NOT here for actual usage. Solely here to satisfy the interface.
gin.IRoutes
handlers map[string][]gin.HandlerFunc
method map[string]string
}
func (m *mockHandler) set(method string, path string, handlers []gin.HandlerFunc) gin.IRoutes {
if m.method == nil {
m.method = map[string]string{}
}
m.method[path] = method
if m.handlers == nil {
m.handlers = map[string][]gin.HandlerFunc{}
}
m.handlers[path] = handlers
return m
}
func (m *mockHandler) GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return m.set("GET", relativePath, handlers)
}
func mockGroupMultiHn(t *testing.T, paths []string, methods map[string]string, f func(group)) map[string]gin.HandlerFunc {
t.Helper()
m := mockHandler{}
f(&m)
if len(m.handlers) != len(paths) {
t.Error(len(m.handlers), "routes set")
return nil
} else {
newMap := map[string]gin.HandlerFunc{}
for _, path := range paths {
method := methods[path]
resultMethod, ok := m.method[path]
if ok {
assert.Equal(t, method, resultMethod)
hn := m.handlers[path]
switch len(hn) {
case 0:
t.Error("no handlers set")
return nil
case 1:
newMap[path] = hn[0]
default:
t.Error("more than 1 handler is not supported")
return nil
}
} else {
t.Error("no handler set for", path)
}
}
return newMap
}
}
func mockGroupSingleHn(t *testing.T, method, path string, f func(group)) gin.HandlerFunc {
t.Helper()
m := mockGroupMultiHn(t, []string{path}, map[string]string{path: method}, f)
if m == nil {
return nil
}
return m[path]
}