-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
49 lines (45 loc) · 1.25 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
package testing
import (
"bytes"
"encoding/json"
"github.com/labstack/echo"
"net/http"
"net/http/httptest"
"strings"
)
func NewPostRequestWithBody(body interface{}) (*http.Request, *httptest.ResponseRecorder, echo.Context) {
e := echo.New()
var req *http.Request
switch t := body.(type) {
case []byte:
req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(t))
break
case string:
req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(t))
break
default:
bodyBytes, err := json.Marshal(t)
if err != nil {
panic(err)
}
req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(bodyBytes))
}
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
return req, rec, c
}
func NewGetRequest() (*http.Request, *httptest.ResponseRecorder, echo.Context) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
return req, rec, c
}
func NewDeleteRequest() (*http.Request, *httptest.ResponseRecorder, echo.Context) {
e := echo.New()
req := httptest.NewRequest(http.MethodDelete, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
return req, rec, c
}