-
Notifications
You must be signed in to change notification settings - Fork 21
/
http.go
59 lines (55 loc) · 1.37 KB
/
http.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
package openmock
import (
"fmt"
"io/ioutil"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
)
func (om *OpenMock) startHTTP() {
e := echo.New()
e.HideBanner = true
e.Use(middleware.Logger())
e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
logrus.WithFields(logrus.Fields{
"http_path": c.Path(),
"http_method": c.Request().Method,
"http_host": c.Request().Host,
"http_req": string(reqBody),
"http_res": string(resBody),
}).Info()
}))
if om.CorsEnabled {
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowCredentials: true,
AllowHeaders: []string{"*"},
AllowMethods: []string{"*"},
}))
}
mocks := om.repo.HTTPMocks
for h, ms := range mocks {
func(h ExpectHTTP, ms MocksArray) {
e.Match(
[]string{h.Method},
h.Path,
func(ec echo.Context) error {
body, _ := ioutil.ReadAll(ec.Request().Body)
c := Context{
HTTPContext: ec,
HTTPHeader: ec.Request().Header,
HTTPBody: string(body),
HTTPPath: ec.Request().URL.String(),
HTTPQueryString: ec.QueryString(),
om: om,
}
ms.DoActions(c)
return nil
},
)
}(h, ms)
}
e.Logger.Fatal(
e.Start(fmt.Sprintf("%s:%d", om.HTTPHost, om.HTTPPort)),
)
}