/
tests.go
101 lines (93 loc) · 2.04 KB
/
tests.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
package form
import (
"bytes"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
)
type testForm struct {
Form
Roles Field[[]string]
Email Field[string]
Name Field[string]
Quantity Field[int]
Amount Field[float64]
Checked Field[bool]
Test Field[Multipart]
}
type testModel struct {
Roles []string
Name string
Quantity int
Amount float64
Checked bool
}
const (
testAction = "/test"
testName = "test"
testNameValue = "Test"
testQuantityValue = 5
testAmountValue = 999.99
testCheckedValue = true
testRole = "owner"
)
func testGetRequest() *http.Request {
req := httptest.NewRequest(
http.MethodGet,
"/test",
strings.NewReader(""),
)
req.Header.Set(contentType, contentTypeHtml)
return req
}
func testCreateFormRequest() *http.Request {
req := httptest.NewRequest(
http.MethodPost,
"/test",
strings.NewReader(
fmt.Sprintf(
"name=%s&quantity=%d&amount=%.2f&checked=%t&roles=%s",
testNameValue, testQuantityValue, testAmountValue, testCheckedValue, testRole,
),
),
)
req.Header.Set(contentType, contentTypeForm)
return req
}
func testCreateMultipartRequest() ([]byte, *http.Request, error) {
fileBytes := bytes.Repeat([]byte("test"), 1<<8)
bodyBuf := new(bytes.Buffer)
bodyWriter := multipart.NewWriter(bodyBuf)
if err := bodyWriter.WriteField("name", testNameValue); err != nil {
return fileBytes, nil, err
}
testFile, err := bodyWriter.CreateFormFile("test", "test.txt")
if err != nil {
return fileBytes, nil, err
}
_, err = testFile.Write(fileBytes)
if err != nil {
return fileBytes, nil, err
}
if err := bodyWriter.Close(); err != nil {
return fileBytes, nil, err
}
req := httptest.NewRequest(
http.MethodPost,
"/test",
bodyBuf,
)
req.Header.Set(contentType, bodyWriter.FormDataContentType())
return fileBytes, req, nil
}
func testCreateEmptyBuildRequest() *http.Request {
req := httptest.NewRequest(
http.MethodPost,
"/test",
strings.NewReader(""),
)
req.Header.Set(contentType, contentTypeForm)
return req
}