generated from calmato/project-template
/
http.go
105 lines (86 loc) · 2.31 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/textproto"
"os"
"path/filepath"
"strings"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
/**
* NewHTTPRequest - HTTP Request(application/json)を生成
*/
func NewHTTPRequest(t *testing.T, method, path string, body interface{}) *http.Request {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var buf []byte
if body != nil {
var err error
buf, err = json.Marshal(body)
require.NoError(t, err, err)
}
req, err := http.NewRequest(method, path, bytes.NewReader(buf))
require.NoError(t, err, err)
req.Header.Add("Content-Type", "application/json")
return req
}
/**
* NewMultipartRequset - HTTP Request(multipart/form-data)を生成
*/
func NewMultipartRequest(t *testing.T, method, path, field string) *http.Request {
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
defer writer.Close()
filepath := getFilepath(t)
file, err := os.Open(filepath)
require.NoError(t, err, err)
defer file.Close()
header := textproto.MIMEHeader{}
header.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, field, filename))
header.Set("Content-Type", "multipart/form-data")
part, err := writer.CreatePart(header)
require.NoError(t, err, err)
_, err = io.Copy(part, file)
require.NoError(t, err, err)
req, err := http.NewRequest(method, path, buf)
require.NoError(t, err, err)
req.Header.Add("Content-Type", writer.FormDataContentType())
return req
}
/**
* TestHTTP - HTTP Responseの検証
*/
//nolint:revive
func TestHTTP(t *testing.T, expect *HTTPResponse, res *httptest.ResponseRecorder) {
require.Equal(t, expect.Code, res.Code)
if isError(res) || expect.Body == nil {
return
}
body, err := json.Marshal(expect.Body)
require.NoError(t, err, err)
require.Equal(t, string(body), res.Body.String())
}
/**
* Private Methods
*/
func getFilepath(t *testing.T) string {
dir, err := os.Getwd()
assert.NoError(t, err)
strs := strings.Split(dir, "api/internal/gateway")
if len(strs) == 0 {
t.Fatal("test: invalid file path")
}
return filepath.Join(strs[0], "/api/pkg/test", filename)
}
func isError(res *httptest.ResponseRecorder) bool {
return res.Code < 200 || 300 <= res.Code
}