-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.go
251 lines (211 loc) · 6.28 KB
/
utils.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package e2e
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/anz-bank/sysl-go/config"
"github.com/anz-bank/sysl-go/core"
"google.golang.org/grpc"
)
var (
errDefault = fmt.Errorf("")
)
func makeHeader(vals map[string]string) http.Header {
head := make(http.Header)
for k, v := range vals {
head.Add(k, v)
}
return head
}
func equalFold(key string, actualVal, expectedVal []string) string {
actualStr := strings.Join(actualVal, ", ")
expectedStr := strings.Join(expectedVal, ", ")
if !strings.EqualFold(expectedStr, actualStr) {
return fmt.Sprintf("%s: '%s'!='%s'", key, actualStr, expectedStr)
}
return ""
}
// IgnoreHeaders is a list of headers that should be ignored during testing.
var IgnoreHeaders = []string{}
func shouldSkipTestingHeader(hdr string) bool {
for _, v := range IgnoreHeaders {
if strings.EqualFold(v, hdr) {
return true
}
}
return false
}
func iterateHeaders(actualHeader, expectedHeaders http.Header) (extra, missing, valMismatch []string) {
for k, v := range actualHeader {
if shouldSkipTestingHeader(k) {
continue
}
expectedVal, ok := expectedHeaders[k]
if !ok {
extra = append(extra, k)
continue
}
mismatch := equalFold(k, v, expectedVal)
if mismatch != "" {
valMismatch = append(valMismatch, mismatch)
}
}
for k := range expectedHeaders {
if shouldSkipTestingHeader(k) {
continue
}
_, ok := actualHeader[k]
if !ok {
missing = append(missing, k)
continue
}
}
return extra, missing, valMismatch
}
func verifyHeaders(expected http.Header, actual http.Header, checkForExtra ...bool) error {
extra, missing, valMismatch := iterateHeaders(actual, expected)
errorStr := ""
if len(checkForExtra) > 0 && checkForExtra[0] && extra != nil {
errorStr += fmt.Sprintf("the following header fields were not expected: '%v'\n", extra)
}
if missing != nil {
errorStr += fmt.Sprintf("the following header fields were expected but missing: '%v'\n", missing)
}
if valMismatch != nil {
errorStr += fmt.Sprintf("the following header fields were received with incorrect values: '%v'\n", valMismatch)
}
if errorStr != "" {
return fmt.Errorf("%s %w", errorStr, errDefault)
}
return nil
}
func expectHeadersExistImp(headers []string, actual http.Header) error {
var missing []string
for _, h := range headers {
if _, exists := actual[http.CanonicalHeaderKey(h)]; !exists {
missing = append(missing, h)
}
}
if len(missing) > 0 {
return fmt.Errorf("Expected headers were missing: %s", missing)
}
return nil
}
func expectHeadersDoNotExistImp(headers []string, actual http.Header) error {
var extra []string
for _, h := range headers {
if _, exists := actual[http.CanonicalHeaderKey(h)]; exists {
extra = append(extra, h)
}
}
if len(extra) > 0 {
return fmt.Errorf("Headers were expected to be missing: %s", extra)
}
return nil
}
func expectHeadersExistExactlyImp(headers []string, actual http.Header) (missingError, extraError error) {
var extra, missing []string
m := map[string]interface{}{}
for _, h := range headers {
can := http.CanonicalHeaderKey(h)
m[can] = nil
if _, exists := actual[can]; !exists {
missing = append(missing, h)
}
}
for h := range actual {
if _, exists := m[h]; !exists {
extra = append(extra, h)
}
}
if len(missing) > 0 {
missingError = fmt.Errorf("Expected headers were missing: %s", missing)
}
if len(extra) > 0 {
extraError = fmt.Errorf("Extra headers were found: %s", extra)
}
return missingError, extraError
}
func GetResponseBodyAndClose(b io.ReadCloser) []byte {
if b == nil {
return nil
}
var buf bytes.Buffer
defer func() { _ = b.Close() }()
if _, err := buf.ReadFrom(b); err != nil {
panic(err)
}
return buf.Bytes()
}
func GetTestLine() string {
for i := 1; i < 10; i++ {
if _, file, line, ok := runtime.Caller(i); ok {
base := filepath.Base(file)
if strings.HasSuffix(base, "_test.go") {
return fmt.Sprintf("Expectation At: %s:%d", base, line)
}
} else {
return ""
}
}
return ""
}
// CreateServiceWithTestHooksPatched will return a function with the same signature as createService but will patch
// the test hooks into the result (we don't know the config type at this point so use reflection).
func CreateServiceWithTestHooksPatched(createService interface{}, testHooks *core.Hooks) interface{} {
return reflect.MakeFunc(reflect.TypeOf(createService), func(args []reflect.Value) (results []reflect.Value) {
// Call createService
createServiceResult := reflect.ValueOf(createService).Call(args)
if err := createServiceResult[2].Interface(); err != nil {
return createServiceResult
}
// Patch in the test hooks
h := createServiceResult[1].Interface().(*core.Hooks)
if h == nil {
h = testHooks
} else {
h.ExperimentalTemporalClientBuilder = testHooks.ExperimentalTemporalClientBuilder
h.ExperimentalTemporalWorkerBuilder = testHooks.ExperimentalTemporalWorkerBuilder
h.HTTPClientBuilder = testHooks.HTTPClientBuilder
h.StoppableServerBuilder = testHooks.StoppableServerBuilder
if testHooks.OverrideGrpcDialOptions != nil {
if h.AdditionalGrpcDialOptions == nil {
h.OverrideGrpcDialOptions = testHooks.OverrideGrpcDialOptions
} else {
additionalGrpcDialOptions := h.AdditionalGrpcDialOptions
h.AdditionalGrpcDialOptions = nil
h.OverrideGrpcDialOptions = func(serviceName string, cfg *config.CommonGRPCDownstreamData) ([]grpc.DialOption, error) {
options, err := testHooks.OverrideGrpcDialOptions(serviceName, cfg)
if err != nil {
return nil, err
}
options = append(options, additionalGrpcDialOptions...)
return options, nil
}
}
}
h.StoppableGrpcServerBuilder = testHooks.StoppableGrpcServerBuilder
if h.ValidateConfig == nil {
h.ValidateConfig = testHooks.ValidateConfig
} else {
usersValidateConfig := h.ValidateConfig
h.ValidateConfig = func(ctx context.Context, cfg *config.DefaultConfig) error {
_ = testHooks.ValidateConfig(ctx, cfg)
return usersValidateConfig(ctx, cfg)
}
}
// Prefer the app set func over the test default
if h.ShouldSetGrpcGlobalLogger == nil {
h.ShouldSetGrpcGlobalLogger = testHooks.ShouldSetGrpcGlobalLogger
}
}
createServiceResult[1] = reflect.ValueOf(h)
return createServiceResult
}).Interface()
}