-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbody_test.go
193 lines (165 loc) · 5.63 KB
/
body_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package handlertest
import (
"fmt"
"github.com/krzysztofmadejski/handlertest/internal"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"reflect"
"sort"
"strings"
"testing"
)
var expectBody = func(t *testing.T, expectedBody string, contentType string) http.HandlerFunc {
at := internal.CallerInfo()[1]
return func(w http.ResponseWriter, r *http.Request) {
expectHeader(t, "Content-Type", contentType)(w, r)
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Could not read body because %v", err)
} else {
bodyString := string(bodyBytes)
if expectedBody != bodyString {
t.Errorf("Expected body to be different at %s. Expected '%s', got '%s'", at, expectedBody, bodyString)
}
}
}
}
func TestJsonBody(t *testing.T) {
json := `{"f": 1}`
Call(expectBody(t, json, ContentTypeJSON)).
JSON(json).
Assert(new(testing.T))
}
var expectForm = func(t *testing.T, expectedValues url.Values, is_multipart bool, method string, numFiles int) http.HandlerFunc {
at := internal.CallerInfo()[1]
return func(w http.ResponseWriter, r *http.Request) {
if is_multipart {
cType := r.Header.Get("Content-Type")
if !strings.HasPrefix(cType, ContentTypeMultipartFormDataPrefix) {
t.Errorf("Expected Content-Type to start with '%s', but received '%s'", ContentTypeMultipartFormDataPrefix, cType)
} else {
handle(t, r.ParseMultipartForm(1>>24))
}
} else { // encoded
expectHeader(t, "Content-Type", ContentTypeFormURLEncoded)(w, r)
handle(t, r.ParseForm())
}
if !reflect.DeepEqual(expectedValues, r.Form) {
t.Errorf("Expected request.Form to be %+v. but got %+v at %s", expectedValues, r.Form, at)
}
if !reflect.DeepEqual(expectedValues, r.PostForm) {
t.Errorf("Expected request.PostForm to be %+v. but got %+v at %s", expectedValues, r.Form, at)
}
if numFiles == 0 {
return
}
if r.MultipartForm == nil || r.MultipartForm.File == nil {
t.Errorf("Expected MultipartForm to be set and have files")
return
}
for _, fheaders := range r.MultipartForm.File {
sort.Slice(fheaders, func(i, j int) bool {
return fheaders[i].Filename < fheaders[j].Filename
})
for i, fh := range fheaders {
fileName := fmt.Sprintf("file%d.txt", i+1)
if fileName != fh.Filename {
t.Errorf("Expected file %s at position %d", fileName, i)
}
f, err := fh.Open()
if err != nil {
t.Error(err)
}
defer func(f multipart.File) {
handle(t, f.Close())
}(f)
bytes, err := ioutil.ReadAll(f)
if err != nil {
t.Error(err)
}
actualContents := string(bytes)
expectedContents := fmt.Sprintf("contents%d", i+1)
if expectedContents != actualContents {
t.Errorf("Expected content '%s', but got '%s' at %d", expectedContents, actualContents, i)
}
}
}
}
}
func TestFormUrlEncoded(t *testing.T) {
values := url.Values{"field": []string{"val1", "val2"}}
Call(expectForm(t, values, false, "POST", 0)).
FormURLEncoded(values).
Assert(new(testing.T))
}
// TestFormUrlEncodedSetOtherMethod tests other methods than POST
// POST is set as a default method for sending forms
// but you might set another one
func TestFormUrlEncodedSetOtherMethod(t *testing.T) {
values := url.Values{"field": []string{"val1", "val2"}}
Call(expectForm(t, values, false, "PUT", 0)).
Method("PUT").FormURLEncoded(values).
Assert(new(testing.T))
Call(expectForm(t, values, false, "PUT", 0)).
FormURLEncoded(values).Method("PUT").
Assert(new(testing.T))
}
func TestFormUrlEncodedMap(t *testing.T) {
values := url.Values{"field": []string{"value"}}
Call(expectForm(t, values, false, "POST", 0)).
FormURLEncodedMap(map[string]string{"field": "value"}).
Assert(new(testing.T))
}
func TestFormMultipartOneField(t *testing.T) {
values := url.Values{"field": []string{"val1"}}
Call(expectForm(t, values, true, "POST", 0)).
FormMultipart(values).
Assert(new(testing.T))
}
func TestFormMultipartOnlyFields(t *testing.T) {
values := url.Values{"field": []string{"val1", "val2"}}
Call(expectForm(t, values, true, "POST", 0)).
FormMultipart(values).
Assert(new(testing.T))
}
func TestFormMultipartMapOnlyFields(t *testing.T) {
values := url.Values{"field": []string{"value"}}
Call(expectForm(t, values, true, "POST", 0)).
FormMultipartMap(map[string]string{"field": "value"}).
Assert(new(testing.T))
}
func TestFormMultipartOneFile(t *testing.T) {
Call(expectForm(t, url.Values{}, true, "POST", 1)).
Files(map[string]map[string]string{"files[]": {"file1.txt": "contents1"}}).
Assert(new(testing.T))
}
func TestFormMultipartMultipleFiles(t *testing.T) {
Call(expectForm(t, url.Values{}, true, "POST", 2)).
Files(map[string]map[string]string{"files[]": {"file1.txt": "contents1", "file2.txt": "contents2"}}).
Assert(new(testing.T))
}
func TestFormMultipartMultipleFileReaders(t *testing.T) {
Call(expectForm(t, url.Values{}, true, "POST", 2)).
FileReaders(map[string]map[string]io.Reader{"files[]": {"file1.txt": strings.NewReader("contents1"), "file2.txt": strings.NewReader("contents2")}}).
Assert(new(testing.T))
}
func TestFormMultipartAddFileReader(t *testing.T) {
Call(expectForm(t, url.Values{}, true, "POST", 1)).
FileReader("files[]", "file1.txt", strings.NewReader("contents1")).
Assert(new(testing.T))
}
func TestFormMultipartAddFile(t *testing.T) {
Call(expectForm(t, url.Values{}, true, "POST", 1)).
File("files[]", "file1.txt", "contents1").
Assert(new(testing.T))
}
func TestFormMultipartAddFileMultiple(t *testing.T) {
Call(expectForm(t, url.Values{}, true, "POST", 2)).
File("files[]", "file1.txt", "contents1").
File("files[]", "file2.txt", "contents2").
Assert(new(testing.T))
}
// TODO test joining url values and form values