-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.go
75 lines (65 loc) · 2.1 KB
/
tests.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
package vcago
import (
"net/http"
"net/http/httptest"
"strings"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
)
type Test struct {
Server Server
}
func NewTest(server *Server) *Test {
return &Test{
Server: *server,
}
}
func (i *Test) POSTContext(data string, rec *httptest.ResponseRecorder, token *jwt.Token) Context {
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(data))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
cc := i.Server.NewContext(req, rec)
if token != nil {
cc.Set("token", token)
}
return Context{Model: "test", Context: cc}
}
func (i *Test) GETByIDContext(data string, rec *httptest.ResponseRecorder, token *jwt.Token) Context {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
cc := i.Server.NewContext(req, rec)
if token != nil {
cc.Set("token", token)
}
cc.SetParamNames("id")
cc.SetParamValues(data)
return Context{Model: "test", Context: cc}
}
func (i *Test) PUTContext(data string, rec *httptest.ResponseRecorder, token *jwt.Token) Context {
req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(data))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
cc := i.Server.NewContext(req, rec)
if token != nil {
cc.Set("token", token)
}
return Context{Model: "test", Context: cc}
}
func (i *Test) GETContext(data string, rec *httptest.ResponseRecorder, token *jwt.Token) Context {
req := httptest.NewRequest(http.MethodGet, "/"+data, nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
cc := i.Server.NewContext(req, rec)
if token != nil {
cc.Set("token", token)
}
return Context{Model: "test", Context: cc}
}
func (i *Test) DELETEContext(data string, rec *httptest.ResponseRecorder, token *jwt.Token) Context {
req := httptest.NewRequest(http.MethodDelete, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
cc := i.Server.NewContext(req, rec)
if token != nil {
cc.Set("token", token)
}
cc.SetParamNames("id")
cc.SetParamValues(data)
return Context{Model: "test", Context: cc}
}