-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocked_response.go
More file actions
366 lines (291 loc) · 8.85 KB
/
mocked_response.go
File metadata and controls
366 lines (291 loc) · 8.85 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package awsmocker
import (
"encoding/xml"
"net/http"
"reflect"
"strconv"
"strings"
"github.com/aws/smithy-go/document"
"github.com/clbanning/mxj"
)
const (
ContentTypeXML = "text/xml"
ContentTypeJSON = "application/x-amz-json-1.1"
ContentTypeText = "text/plain"
)
var (
byteArrayType = reflect.SliceOf(reflect.TypeOf((*byte)(nil)).Elem())
rrType = reflect.TypeFor[*ReceivedRequest]()
errType = reflect.TypeFor[error]()
)
// type used to generate
type directTypeFunc = func(*ReceivedRequest) (any, error)
type MockedResponse struct {
// modify the status code. default is 200
StatusCode int
// force the content type. default will be determined by request content type
ContentType string
Encoding ResponseEncoding
// a string, struct or map that will be encoded as the response
//
// Also accepts a function that is of the following signatures:
// func(*ReceivedRequest) (string) = string payload (with 200 OK, inferred content type)
// func(*ReceivedRequest) (string, int) = string payload, <int> status code (with inferred content type)
// func(*ReceivedRequest) (string, int, string) = string payload, <int> status code, content type
// func(*ReceivedRequest) (*service.ACTIONOutput, error) = return the result type directly, or error
// func(*ReceivedRequest, *service.ACTIONInput) (*service.ACTIONOutput, error) = return the result type directly, or error
// func(*service.ACTIONInput) (*service.ACTIONOutput, error) = return the result type directly, or error
Body any
// Do not wrap the xml response in ACTIONResponse>ACTIONResult
DoNotWrap bool
RootTag string
// If provided, then all other fields are ignored, and the user
// is responsible for building an HTTP response themselves
Handler MockedRequestHandler
rawBody string
action string
}
type wrapperStruct struct {
XMLName xml.Name `xml:"_ACTION_NAME_HERE_Response"`
Result any `xml:"_ACTION_NAME_HERE_Result"`
RequestId string `xml:"ResponseMetadata>RequestId"`
}
func (m *MockedResponse) prep() {
if m.StatusCode == 0 {
m.StatusCode = http.StatusOK
}
}
func (m *MockedResponse) getResponse(rr *ReceivedRequest) *httpResponse {
if m.Handler != nil {
// user wants to do it all themselves
return &httpResponse{
forcedHttpResponse: m.Handler(rr),
}
}
if m.rawBody != "" && m.ContentType != "" {
return &httpResponse{
Body: m.rawBody,
StatusCode: m.StatusCode,
contentType: m.ContentType,
}
}
if dir := m.processDirectRequest(rr); dir != nil {
return dir
}
actionName := m.action
if actionName == "" {
actionName = rr.Action
}
rBody := reflect.Indirect(reflect.ValueOf(m.Body))
bodyKind := rBody.Kind()
switch bodyKind {
case reflect.Func:
switch rBody.Interface().(type) {
case func(*ReceivedRequest) string:
case func(*ReceivedRequest) (string, int):
case func(*ReceivedRequest) (string, int, string):
// valid function
default:
return generateErrorStruct(0, "InvalidBodyFunc", "the function you gave for the body has the wrong signature").getResponse(rr)
}
respRet := rBody.Call([]reflect.Value{reflect.ValueOf(rr)})
respBody := respRet[0].String()
respStatus := http.StatusOK
var respContentType string
if len(respRet) > 1 {
respStatus = int(respRet[1].Int())
}
if len(respRet) > 2 {
respContentType = respRet[2].String()
} else {
respContentType = inferContentType(respBody)
}
return &httpResponse{
Body: respBody,
contentType: respContentType,
StatusCode: respStatus,
}
case reflect.String:
m.rawBody = rBody.String()
// m.ContentType = ContentTypeText
if m.ContentType == "" && len(m.rawBody) > 1 {
m.ContentType = inferContentType(m.rawBody)
}
return &httpResponse{
Body: m.rawBody,
StatusCode: m.StatusCode,
contentType: m.ContentType,
}
case reflect.Map, reflect.Array, reflect.Slice, reflect.Struct:
switch {
case m.Encoding == ResponseEncodingJSON:
fallthrough
case m.Encoding == ResponseEncodingDefault && rr.AssumedResponseType == ContentTypeJSON:
return &httpResponse{
Body: EncodeAsJson(m.Body),
StatusCode: m.StatusCode,
contentType: ContentTypeJSON,
}
case m.Encoding == ResponseEncodingXML:
fallthrough
case m.Encoding == ResponseEncodingDefault && rr.AssumedResponseType == ContentTypeXML:
if m.DoNotWrap {
if m.RootTag == "" {
m.RootTag = "" + actionName + "Response"
}
xmlout, err := mxj.AnyXmlIndent(m.Body, "", " ", m.RootTag, "")
if err != nil {
return generateErrorStruct(0, "BadMockBody", "Could not serialize body to XML: %s", err).getResponse(rr)
}
return &httpResponse{
bodyRaw: xmlout,
StatusCode: m.StatusCode,
contentType: ContentTypeXML,
}
} else if bodyKind == reflect.Struct {
wrappedObj := wrapperStruct{
Result: m.Body,
RequestId: "01234567-89ab-cdef-0123-456789abcdef",
}
xmlout, err := mxj.AnyXmlIndent(wrappedObj, "", " ", ""+actionName+"Response")
if err != nil {
return generateErrorStruct(0, "BadMockBody", "Could not serialize body to XML: %s", err).getResponse(rr)
}
return &httpResponse{
Body: strings.ReplaceAll(string(xmlout), "_ACTION_NAME_HERE_", actionName),
StatusCode: m.StatusCode,
contentType: ContentTypeXML,
}
}
resultName := "" + actionName + "Result"
wrappedMap := map[string]any{
resultName: m.Body,
"ResponseMetadata": map[string]string{
"RequestId": "01234567-89ab-cdef-0123-456789abcdef",
},
}
xmlout, err := mxj.AnyXmlIndent(wrappedMap, "", " ", ""+actionName+"Response")
if err != nil {
return generateErrorStruct(0, "BadMockBody", "Could not serialize body to XML: %s", err).getResponse(rr)
}
return &httpResponse{
bodyRaw: xmlout,
StatusCode: m.StatusCode,
contentType: ContentTypeXML,
}
case bodyKind == reflect.Slice && rBody.Type() == byteArrayType:
cType := m.ContentType
if cType == "" {
cType = http.DetectContentType(m.Body.([]byte))
}
return &httpResponse{
bodyRaw: m.Body.([]byte),
StatusCode: m.StatusCode,
contentType: cType,
}
}
}
return generateErrorStruct(0, "BadMockResponse", "Don't know how to encode a kind=%v using content type=%s", bodyKind, m.ContentType).getResponse(rr)
}
func (m *MockedResponse) processDirectRequest(rr *ReceivedRequest) *httpResponse {
if m.Body == nil {
return nil
}
body := m.Body
var err error
if rr.HttpRequest == nil || len(rr.HttpRequest.Header) == 0 {
return nil
}
reqId, perr := strconv.ParseUint(rr.HttpRequest.Header.Get(mwHeaderRequestId), 10, 64)
if perr != nil {
return generateErrorStruct(0, "BadMockBody", "Failed to get direct mocker: %s", perr.Error()).getResponse(rr)
}
mkr := rr.mocker
if mkr == nil {
return generateErrorStruct(0, "BadMockBody", "Failed to get direct mocker").getResponse(rr)
}
entry, ok := mkr.requestLog.Load(reqId)
if !ok {
return generateErrorStruct(0, "BadMockBody", "Failed to find mock in DB??").getResponse(rr)
}
rec := entry.(mwDBEntry)
if !document.IsNoSerde(body) {
// check if fancy func
if fn, ok := body.(directTypeFunc); ok {
body, err = fn(rr)
} else {
body, err = processDirectRequestFunc(rec, rr, reflect.Indirect(reflect.ValueOf(body)))
}
if body != nil && !document.IsNoSerde(body) {
return nil
}
}
if body == nil && err == nil {
return nil
}
if reflect.TypeOf(body).Kind() == reflect.Struct {
val := reflect.ValueOf(body)
vp := reflect.New(val.Type())
vp.Elem().Set(val)
body = vp.Interface()
}
rec.Error = err
rec.Response = body
mkr.requestLog.Store(reqId, rec)
return &httpResponse{
StatusCode: http.StatusOK,
Body: "",
contentType: ContentTypeJSON,
extraHeaders: map[string]string{
mwHeaderUseDB: "true",
},
}
}
func processDirectRequestFunc(entry mwDBEntry, rr *ReceivedRequest, fnv reflect.Value) (any, error) {
typ := fnv.Type()
if typ.Kind() != reflect.Func {
return nil, nil
}
params := entry.Parameters
paramT := reflect.TypeOf(params)
inputs := make([]reflect.Value, 0, 2)
if typ.NumIn() == 1 {
in1 := typ.In(0)
if in1 == rrType {
inputs = append(inputs, reflect.ValueOf(rr))
} else if in1 == paramT {
inputs = append(inputs, reflect.ValueOf(params))
} else {
return nil, nil
}
} else if typ.NumIn() == 2 {
if in1 := typ.In(0); in1 == rrType {
inputs = append(inputs, reflect.ValueOf(rr))
} else {
return nil, nil
}
if in2 := typ.In(1); in2 == paramT {
inputs = append(inputs, reflect.ValueOf(params))
} else {
return nil, nil
}
} else {
// invalid signature
return nil, nil
}
if typ.NumOut() != 2 {
return nil, nil
}
if out2 := typ.Out(1); out2 != errType {
// 2nd return must be error
return nil, nil
}
outputs := fnv.Call(inputs)
ret := outputs[0].Interface()
if typ.NumOut() == 2 {
if err := outputs[1].Interface(); err != nil {
return ret, err.(error)
}
}
return ret, nil
}