forked from hungdv136/rio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
424 lines (348 loc) · 10.7 KB
/
handler.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package rio
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"path"
"strings"
"time"
"github.com/PaesslerAG/jsonpath"
"github.com/manabie-com/rio/internal/log"
fs "github.com/manabie-com/rio/internal/storage"
)
// Handler handles mocking for http request
type Handler struct {
fileStorage fs.FileStorage
stubStore StubStore
namespace string
basePath string
// If the number of bytes of an incomming request's body is larger than this threshold
// then body won't be saved to DB to avoid hurting DB performance when uploading with a file
// If set to zero, then body is always saved to database
bodyStoreThreshold int
}
// NewHandler handles request
func NewHandler(stubStore StubStore, fileStorage fs.FileStorage) *Handler {
return &Handler{
stubStore: stubStore,
fileStorage: fileStorage,
basePath: "/echo/",
bodyStoreThreshold: 1 << 20, // Default 1MB is a lot of text
}
}
// WithNamespace sets namespace
func (h *Handler) WithNamespace(namespace string) *Handler {
h.namespace = namespace
return h
}
func (h *Handler) WithBodyStoreThreshold(v int) *Handler {
h.bodyStoreThreshold = v
return h
}
// Handle handles http request
func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
incomeRequest := Capture(r, h.bodyStoreThreshold).WithNamespace(h.namespace)
defer h.stubStore.CreateIncomingRequest(ctx, incomeRequest)
stubs, err := h.stubStore.GetAll(ctx, h.namespace)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(stubs) == 0 {
log.Info(ctx, "no stubs found")
w.WriteHeader(http.StatusNotFound)
return
}
matchedStubs := make([]*Stub, 0, len(stubs))
for _, stub := range stubs {
matched, err := matchHTTPRequest(ctx, stub, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if matched {
matchedStubs = append(matchedStubs, stub)
}
}
if len(matchedStubs) == 0 {
log.Info(ctx, "no matched stub found")
w.WriteHeader(http.StatusNotFound)
return
}
stub := SelectStubs(matchedStubs)
incomeRequest.StubID = stub.ID
incomeRequest.Tag = stub.Tag
log.Info(ctx, "matched stub", stub.ID, stub.Description, "nb stubs", len(stubs), "in", h.namespace)
if stub.Settings.DeactivateWhenMatched {
log.Info(ctx, "remove used stub", stub.ID)
if err := h.stubStore.Delete(ctx, stub.ID); err != nil {
log.Error(ctx, "cannot delete stub, ignore error")
}
}
if stub.Settings.DelayDuration > 0 {
log.Info(ctx, "delay response", stub.Settings.DelayDuration)
time.Sleep(stub.Settings.DelayDuration)
}
if stub.IsReversed() {
if err := h.reverse(w, r, stub); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
if err := h.processResponse(ctx, r, stub); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := stub.Response.WriteTo(ctx, w); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (h *Handler) processResponse(ctx context.Context, r *http.Request, stub *Stub) error {
if len(stub.Response.BodyFile) > 0 {
if err := stub.Response.LoadBodyFromFile(ctx, h.fileStorage); err != nil {
return err
}
}
if stub.HasTemplate() {
if err := stub.Response.LoadBodyFromTemplate(ctx, &TemplateData{Request: r}); err != nil {
return err
}
}
return nil
}
func (h *Handler) reverse(w http.ResponseWriter, r *http.Request, stub *Stub) error {
target, err := url.Parse(stub.Proxy.TargetURL)
if err != nil {
log.Error(r.Context(), "cannot parse target url", stub.Proxy.TargetURL, err)
return err
}
newReq := r.Clone(r.Context())
newReq.Host = target.Host
if len(stub.Proxy.TargetPath) > 0 {
newReq.URL.Path = stub.Proxy.TargetPath
} else {
newReq.URL.Path = h.rewritePath(r.URL.Path)
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // nolint:gosec
}
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
log.Error(r.Context(), "cannot forward request to", newReq.URL.String(), err)
rw.WriteHeader(http.StatusBadGateway)
}
if stub.Proxy.EnableRecord {
proxy.ModifyResponse = h.proxyRecorder(stub) //nolint
log.Info(r.Context(), "enabled response recording")
} else {
proxy.ModifyResponse = func(res *http.Response) error {
log.Info(r.Context(), "forwarded with status code", res.StatusCode, res.Request.Method, res.Request.URL.String())
return nil
}
}
log.Info(r.Context(), "forward to", r.Method, stub.Proxy.TargetURL+newReq.URL.String())
proxy.ServeHTTP(w, newReq)
return nil
}
// The url path contains /echo as prefix of mock service
// This should be removed before forwarding to real service
func (h *Handler) rewritePath(urlPath string) string {
if len(h.namespace) > 0 {
return path.Join("/", strings.TrimPrefix(urlPath, "/"+h.namespace+h.basePath))
}
return path.Join("/", strings.TrimPrefix(urlPath, h.basePath))
}
func (h *Handler) proxyRecorder(stub *Stub) func(*http.Response) error {
return func(res *http.Response) error {
ctx := res.Request.Context()
clonedStub := stub.Clone()
clonedStub.ID = 0
clonedStub.Description = fmt.Sprintf("Proxy record from stub id %d", stub.ID)
clonedStub.Proxy = &Proxy{}
clonedStub.Active = false
clonedStub.Response = NewResponseFromHTTP(res)
clonedStub.Tag = TagRecordedStub
// TODO: Consider to support large body payload using file store
if res.Body != nil {
b := bytes.NewBuffer(make([]byte, 0))
reader := io.TeeReader(res.Body, b)
body, err := io.ReadAll(reader)
if err != nil {
log.Error(ctx, "cannot parse body", err)
return err
}
if err := res.Body.Close(); err != nil {
log.Error(ctx, "cannot close body", err)
}
res.Body = io.NopCloser(b)
clonedStub.Response.Body = body
log.Info(ctx, "parsed body", len(body))
}
if err := h.stubStore.Create(ctx, clonedStub); err != nil {
return err
}
log.Info(ctx, "recording has been created in stub id", clonedStub.ID)
return nil
}
}
// matchHTTPRequest matches a stub with incoming http request
func matchHTTPRequest(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
if s.Request == nil {
return false, nil
}
if len(s.Request.Method) > 0 && !strings.EqualFold(s.Request.Method, r.Method) {
return false, nil
}
if matched, err := matchURL(ctx, s, r); err != nil || !matched {
return false, err
}
if matched, err := matchHeader(ctx, s, r); err != nil || !matched {
return false, err
}
if matched, err := matchCookies(ctx, s, r); err != nil || !matched {
return false, err
}
if matched, err := matchQuery(ctx, s, r); err != nil || !matched {
return false, err
}
if matched, err := matchBody(ctx, s, r); err != nil || !matched {
return false, err
}
return true, nil
}
func matchURL(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
for _, op := range s.Request.URL {
if matched, err := Match(ctx, op, r.URL.String()); err != nil || !matched {
return false, err
}
}
return true, nil
}
func matchHeader(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
for _, op := range s.Request.Header {
if matched, err := Match(ctx, op.Operator, r.Header.Get(op.FieldName)); err != nil || !matched {
return false, err
}
}
return true, nil
}
func matchQuery(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
query := r.URL.Query()
for _, op := range s.Request.Query {
if matched, err := Match(ctx, op.Operator, query.Get(op.FieldName)); err != nil || !matched {
return false, err
}
}
return true, nil
}
func matchCookies(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
for _, op := range s.Request.Cookie {
cookie, err := r.Cookie(op.FieldName)
if err != nil {
if !errors.Is(err, http.ErrNoCookie) {
return false, err
}
// If cookie not found, then lets the operator decides the output
if matched, err := Match(ctx, op.Operator, ""); err != nil || !matched {
return false, err
}
continue
}
if matched, err := Match(ctx, op.Operator, cookie.Value); err != nil || !matched {
return false, err
}
}
return true, nil
}
func matchBody(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
if len(s.Request.Body) == 0 {
return true, nil
}
if r.Body == nil {
err := errors.New("missing body")
log.Error(ctx, err)
return false, err
}
contentType := r.Header.Get(HeaderContentType)
if err := validateBodyOperator(ctx, s, contentType); err != nil {
return false, err
}
if strings.HasPrefix(contentType, ContentTypeJSON) {
return matchJSONBody(ctx, s, r)
}
if strings.HasPrefix(contentType, ContentTypeMultipart) {
return matchMultiplePart(ctx, s, r)
}
if strings.HasPrefix(contentType, ContentTypeForm) {
return matchURLEncodedBody(ctx, s, r)
}
err := fmt.Errorf("unsupported content type %s", contentType)
log.Error(ctx, err)
return false, err
}
func matchJSONBody(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
dataMap := map[string]interface{}{}
decoder := json.NewDecoder(readRequestBody(r))
decoder.UseNumber()
if err := decoder.Decode(&dataMap); err != nil && !errors.Is(err, io.EOF) {
log.Error(ctx, "cannot decode json", err)
return false, err
}
for _, op := range s.Request.Body {
val, err := jsonpath.Get(op.KeyPath, dataMap)
if err != nil {
if !strings.Contains(err.Error(), "unknown key") {
log.Error(ctx, "error when executing json path", err)
return false, err
}
}
if matched, err := Match(ctx, op.Operator, val); err != nil || !matched {
return false, err
}
}
return true, nil
}
func matchMultiplePart(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
if err := r.ParseMultipartForm(1024 * 1024 * 20 << 20); err != nil {
log.Error(ctx, err)
return false, err
}
for _, op := range s.Request.Body {
val := r.FormValue(op.KeyPath)
if matched, err := Match(ctx, op.Operator, val); err != nil || !matched {
return false, err
}
}
return true, nil
}
func matchURLEncodedBody(ctx context.Context, s *Stub, r *http.Request) (bool, error) {
for _, op := range s.Request.Body {
val := r.FormValue(op.KeyPath)
if matched, err := Match(ctx, op.Operator, val); err != nil || !matched {
return false, err
}
}
return true, nil
}
// Each body operator is applied for a specific content type
// This is to validate whether request content type is matched with content type of all operators
func validateBodyOperator(ctx context.Context, s *Stub, contentType string) error {
for _, op := range s.Request.Body {
if !strings.HasPrefix(contentType, op.ContentType) {
err := fmt.Errorf("mismatch request and operator content type %s - %s", contentType, op.ContentType)
log.Error(ctx, err)
return err
}
}
return nil
}