-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_helper.go
62 lines (55 loc) 路 1.2 KB
/
test_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package controllers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
)
func writeErrorMsg(err error) string {
return err.Error() + "\n"
}
func parseBody(w *httptest.ResponseRecorder) Props {
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
props := Props{}
_ = json.Unmarshal(body, &props)
return props
}
func createHttpReq(method string, endpoint string, body *bytes.Buffer) (w *httptest.ResponseRecorder, req *http.Request) {
if body == nil {
body = bytes.NewBuffer(make([]byte, 512))
}
req = httptest.NewRequest(method, endpoint, body)
w = httptest.NewRecorder()
req.Form = url.Values{}
return
}
func fileUploadRequest() (body *bytes.Buffer, contentType string) {
file, err := os.Open("./test.png")
if err != nil {
fmt.Println(err)
}
defer file.Close()
body = new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", "test")
if err != nil {
fmt.Println(err)
}
_, err = io.Copy(part, file)
if err != nil {
fmt.Println(err)
}
err = writer.Close()
if err != nil {
fmt.Println(err)
}
return body, writer.FormDataContentType()
}