-
Notifications
You must be signed in to change notification settings - Fork 4
/
testutils.go
147 lines (127 loc) · 3.36 KB
/
testutils.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
package testutils
import (
"bytes"
"encoding/json"
"fmt"
"net/http/httptest"
"net/url"
"os"
"reflect"
"github.com/gin-gonic/gin"
)
// UnmarshallResponse unmarshalls the reponse in res and stores it in out
func UnmarshallResponse(res *bytes.Buffer, out interface{}) error {
actualResStr, err := res.ReadString('\x00')
if err.Error() != "EOF" {
return err
}
err = json.Unmarshal([]byte(actualResStr), out)
return nil
}
// AddRequestWithFormParamsToCtx attaches a request with given method and form params to the context
func AddRequestWithFormParamsToCtx(ctx *gin.Context, method string, params map[string]string) {
data := url.Values{}
for key, val := range params {
data.Add(key, val)
}
req := httptest.NewRequest(method, "/test", bytes.NewBufferString(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
ctx.Request = req
}
// AddUrlParamsToCtx attaches a request with given method and url params to the context
func AddUrlParamsToCtx(ctx *gin.Context, params map[string]string) {
p := gin.Params{}
for key, val := range params {
p = append(p, gin.Param{
Key: key,
Value: val,
})
}
ctx.Params = p
}
// SetEnvVars sets given environment variables and provides a callback function to restore the variables to their initial values
func SetEnvVars(vars map[string]string) (restoreVars func()) {
initialValues := map[string]string{}
unsetVars := map[string]bool{}
for name, value := range vars {
initialValue, exists := os.LookupEnv(name)
if exists {
initialValues[name] = initialValue
} else {
unsetVars[name] = true
}
err := os.Setenv(name, value)
if err != nil {
panic(err)
}
}
return func() {
for name, value := range initialValues {
err := os.Setenv(name, value)
if err != nil {
panic(err)
}
}
for name := range unsetVars {
err := os.Unsetenv(name)
if err != nil {
panic(err)
}
}
}
}
// UnsetVars unsets given environment variables and provides a callback function to restore the variables to their initial values
func UnsetVars(vars ...string) (restoreVars func()) {
initialValues := map[string]string{}
for _, name := range vars {
initialValue, exists := os.LookupEnv(name)
if exists {
initialValues[name] = initialValue
}
err := os.Unsetenv(name)
if err != nil {
panic(err)
}
}
return func() {
for name, value := range initialValues {
err := os.Setenv(name, value)
if err != nil {
panic(err)
}
}
}
}
// RouterGroupMatcher matches gin router groups with given path
type RouterGroupMatcher struct {
// Path is the base path of the router groups to match
Path string
}
// Matches implements the gomock.Matcher interface
func (r RouterGroupMatcher) Matches(x interface{}) bool {
val := reflect.ValueOf(x)
if reflect.Zero(reflect.TypeOf(x)) == val {
return false
}
// fetching reference to BasePath
basePathMethod := val.MethodByName("BasePath")
if reflect.Zero(reflect.TypeOf(basePathMethod)) == basePathMethod {
return false
}
// calling BasePath
values := basePathMethod.Call([]reflect.Value{})
if len(values) != 1 {
return false
}
// checking if value is string
if !values[0].Type().AssignableTo(reflect.TypeOf("")) {
return false
}
if values[0].String() != r.Path {
return false
}
return true
}
func (r RouterGroupMatcher) String() string {
return fmt.Sprintf("router group's base path is %s", r.Path)
}