/
helper.go
42 lines (32 loc) · 1012 Bytes
/
helper.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
package test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)
// CreateContext creates a fake Echo HttpContext for tests
func CreateContext() (echo.Context, *http.Request, *httptest.ResponseRecorder) {
e := echo.New()
req := new(http.Request)
req.Header = make(map[string][]string)
rec := httptest.NewRecorder()
c := e.NewContext(standard.NewRequest(req, e.Logger()), standard.NewResponse(rec, e.Logger()))
return c, req, rec
}
// CreateJSONContext creates a fake Echo HttpContext that accepts and responds json
func CreateJSONContext(data interface{}) (echo.Context, *httptest.ResponseRecorder, error) {
c, _, res := CreateContext()
b, err := json.Marshal(data)
c.Request().SetBody(bytes.NewReader(b))
c.Request().Header().Add(echo.HeaderContentType, echo.MIMEApplicationJSON)
return c, res, err
}
// PanicErr panics if err is not nil
func PanicErr(err error) {
if err != nil {
panic(err)
}
}