-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_multipart_test.go
More file actions
142 lines (125 loc) · 4.46 KB
/
Copy pathrequest_multipart_test.go
File metadata and controls
142 lines (125 loc) · 4.46 KB
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// ============ multipart tag tests ============
type multipartStruct struct {
Reader *multipart.Reader `multipart:""`
}
func TestMultipartTag_BasicReader(t *testing.T) {
captured, rec := doRequest[multipartStruct](t, captureHandler[multipartStruct],
http.MethodPost, "/", withMultipartBody(t, func(w *multipart.Writer) {
_ = w.WriteField("field1", "value1")
_ = w.WriteField("field2", "value2")
}))
assert.Equal(t, http.StatusOK, rec.Code)
if captured.request.Reader == nil {
t.Fatal("Reader is nil")
}
part, err := captured.request.Reader.NextPart()
if err != nil {
t.Fatalf("NextPart failed: %v", err)
}
assert.Equal(t, "field1", part.FormName(), "first part form name")
value, _ := io.ReadAll(part)
assert.Equal(t, "value1", string(value), "first part value")
}
func TestMultipartTag_MissingBoundary_400(t *testing.T) {
req, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader("garbage"))
req.Header.Set("Content-Type", "multipart/form-data")
req.ContentLength = int64(len("garbage"))
rec := httptest.NewRecorder()
asTestHTTPHandler(RequestParser(captureHandler[multipartStruct])).ServeHTTP(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code)
}
func TestMultipartTag_WrongContentType_415(t *testing.T) {
_, rec := doRequest[multipartStruct](t, captureHandler[multipartStruct],
http.MethodPost, "/", withRawBody("text/plain", []byte("data")))
assert.Equal(t, http.StatusUnsupportedMediaType, rec.Code)
}
type multipartNonEmptyStruct struct {
Reader *multipart.Reader `multipart:"value"`
}
func TestMultipartTag_NonEmptyValue_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[multipartNonEmptyStruct])
})
}
type multipartWrongTypeStruct struct {
Reader string `multipart:""`
}
func TestMultipartTag_WrongType_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[multipartWrongTypeStruct])
})
}
type multipartMultipleStruct struct {
R1 *multipart.Reader `multipart:""`
R2 *multipart.Reader `multipart:""`
}
func TestMultipartTag_MultipleTags_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[multipartMultipleStruct])
})
}
func TestMultipartTag_FileUpload(t *testing.T) {
captured, rec := doRequest[multipartStruct](t, captureHandler[multipartStruct],
http.MethodPost, "/", withMultipartBody(t, func(w *multipart.Writer) {
writer, err := w.CreateFormFile("upload", "test.txt")
if err != nil {
t.Fatalf("CreateFormFile failed: %v", err)
}
_, _ = writer.Write([]byte("file content"))
}))
assert.Equal(t, http.StatusOK, rec.Code)
if captured.request.Reader == nil {
t.Fatal("Reader is nil")
}
part, err := captured.request.Reader.NextPart()
if err != nil {
t.Fatalf("NextPart failed: %v", err)
}
assert.Equal(t, "test.txt", part.FileName(), "file name")
content, _ := io.ReadAll(part)
assert.Equal(t, "file content", string(content), "content")
}
// TestMultipartTag_ChunkedNoContentLength_Binds pins the deliberate asymmetry
// in body binding: the form and JSON binders reject an unknown Content-Length
// with 411 Length Required, but the multipart binder streams and therefore
// accepts chunked requests (Content-Length -1) without complaint.
func TestMultipartTag_ChunkedNoContentLength_Binds(t *testing.T) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
require.NoError(t, writer.WriteField("field1", "value1"))
require.NoError(t, writer.Close())
var captured multipartStruct
handler := RequestParser(func(ctx *Context, req multipartStruct) {
captured = req
ctx.NewResponse(http.StatusOK)
})
req, _ := http.NewRequest(http.MethodPost, "/", &body)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.ContentLength = -1 // chunked: length unknown
rec := httptest.NewRecorder()
asTestHTTPHandler(handler).ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
require.NotNil(t, captured.Reader)
part, err := captured.Reader.NextPart()
require.NoError(t, err)
assert.Equal(t, "field1", part.FormName(), "form name")
value, _ := io.ReadAll(part)
assert.Equal(t, "value1", string(value), "value")
}