-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_case.go
64 lines (51 loc) · 1.42 KB
/
test_case.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
package helpers
import (
"encoding/json"
"net/http/httptest"
"strings"
"github.com/labstack/echo/v4"
"github.com/rachadiannovansyah/go-echo-boilerplate-ipj/server"
mocket "github.com/selvatico/go-mocket"
)
const UserId = 1
type TestCase struct {
TestName string
Request Request
RequestBody interface{}
HandlerFunc func(s *server.Server, c echo.Context) error
QueryMock *QueryMock
Expected ExpectedResponse
}
type PathParam struct {
Name string
Value string
}
type Request struct {
Method string
Url string
PathParam *PathParam
}
type MockReply []map[string]interface{}
type QueryMock struct {
Query string
Reply MockReply
}
type ExpectedResponse struct {
StatusCode int
BodyPart string
}
func PrepareContextFromTestCase(s *server.Server, test TestCase) (c echo.Context, recorder *httptest.ResponseRecorder) {
requestJson, _ := json.Marshal(test.RequestBody)
request := httptest.NewRequest(test.Request.Method, test.Request.Url, strings.NewReader(string(requestJson)))
request.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
recorder = httptest.NewRecorder()
c = s.Echo.NewContext(request, recorder)
if test.Request.PathParam != nil {
c.SetParamNames(test.Request.PathParam.Name)
c.SetParamValues(test.Request.PathParam.Value)
}
if test.QueryMock != nil {
mocket.Catcher.Reset().NewMock().WithQuery(test.QueryMock.Query).WithReply(test.QueryMock.Reply)
}
return
}