This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
echotest.go
70 lines (61 loc) · 2.67 KB
/
echotest.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
package echotest
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/require"
)
// Do request against the handler
func Do(handler echo.HandlerFunc, req *http.Request, urlParams map[string]string) (rec *httptest.ResponseRecorder, err error) {
rec = httptest.NewRecorder()
e := echo.New()
ctx := e.NewContext(req, rec)
if urlParams != nil {
var keys []string
var values []string
for key, value := range urlParams {
keys = append(keys, key)
values = append(values, value)
}
ctx.SetParamNames(keys...)
ctx.SetParamValues(values...)
}
err = handler(ctx)
return
}
// EqualResp expect response
func EqualResp(t *testing.T, expected Response, rec *httptest.ResponseRecorder) {
require.Equal(t, expected.Code, rec.Code)
require.Equal(t, expected.Body, rec.Body.String())
require.Equal(t, expected.Header, rec.HeaderMap)
}
// DoGET return recorder and error for GET API (deprecated)
func DoGET(handler echo.HandlerFunc, url string, urlParams map[string]string) (rec *httptest.ResponseRecorder, err error) {
req := httptest.NewRequest(http.MethodGet, url, nil)
return Do(handler, req, urlParams)
}
// DoPOST return echo.Context and httptest.ResponseRecorder for POST Request (deprecated)
func DoPOST(handler echo.HandlerFunc, url string, json string, urlParams map[string]string) (rec *httptest.ResponseRecorder, err error) {
req := httptest.NewRequest(http.MethodPost, url, strings.NewReader(json))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
return Do(handler, req, urlParams)
}
// DoPUT return echo.Context and httptest.ResponseRecorder for PUT Request (deprecated)
func DoPUT(handler echo.HandlerFunc, url string, json string, urlParams map[string]string) (rec *httptest.ResponseRecorder, err error) {
req := httptest.NewRequest(http.MethodPut, url, strings.NewReader(json))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
return Do(handler, req, urlParams)
}
// DoPATCH return echo.Context and httptest.ResponseRecorder for PUT Request (deprecated)
func DoPATCH(handler echo.HandlerFunc, url string, json string, urlParams map[string]string) (rec *httptest.ResponseRecorder, err error) {
req := httptest.NewRequest(http.MethodPatch, url, strings.NewReader(json))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
return Do(handler, req, urlParams)
}
// DoDELETE return echo.Context and httptest.ResponseRecorder for DELETE Request (deprecated)
func DoDELETE(handler echo.HandlerFunc, url string, urlParams map[string]string) (rec *httptest.ResponseRecorder, err error) {
req := httptest.NewRequest(http.MethodDelete, url, nil)
return Do(handler, req, urlParams)
}