| 1 |
// Copyright (c) 2021, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package tdhttp_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"io" |
| 12 |
"mime/multipart" |
| 13 |
"net/http" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/maxatome/go-testdeep/helpers/tdhttp" |
| 20 |
"github.com/maxatome/go-testdeep/td" |
| 21 |
) |
| 22 |
|
| 23 |
func TestMultipartPart(t *testing.T) { |
| 24 |
assert, require := td.AssertRequire(t) |
| 25 |
|
| 26 |
check := func(part *tdhttp.MultipartPart, expected string) { |
| 27 |
t.Helper() |
| 28 |
var final bytes.Buffer |
| 29 |
// Read in 2 times to be sure Read() can be called several times |
| 30 |
_, err := io.CopyN(&final, part, 5) |
| 31 |
if assert.CmpNoError(err) { |
| 32 |
_, err := io.Copy(&final, part) |
| 33 |
if assert.CmpNoError(err) { |
| 34 |
assert.Cmp(final.String(), strings.ReplaceAll(expected, "%CR", "\r")) |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Full empty |
| 40 |
b, err := io.ReadAll(&tdhttp.MultipartPart{}) |
| 41 |
assert.CmpNoError(err) |
| 42 |
assert.Len(b, 0) |
| 43 |
|
| 44 |
// Without name |
| 45 |
part := tdhttp.MultipartPart{ |
| 46 |
Content: strings.NewReader("hey!\nyo!"), |
| 47 |
} |
| 48 |
check(&part, `Content-Type: text/plain; charset=utf-8%CR |
| 49 |
%CR |
| 50 |
hey! |
| 51 |
yo!`) |
| 52 |
|
| 53 |
// Without body |
| 54 |
part = tdhttp.MultipartPart{ |
| 55 |
Name: "nobody", |
| 56 |
} |
| 57 |
check(&part, `Content-Disposition: form-data; name="nobody"%CR |
| 58 |
`) |
| 59 |
|
| 60 |
// Without header |
| 61 |
part = tdhttp.MultipartPart{ |
| 62 |
Name: "pipo", |
| 63 |
Content: strings.NewReader("hey!\nyo!"), |
| 64 |
} |
| 65 |
check(&part, `Content-Disposition: form-data; name="pipo"%CR |
| 66 |
Content-Type: text/plain; charset=utf-8%CR |
| 67 |
%CR |
| 68 |
hey! |
| 69 |
yo!`) |
| 70 |
|
| 71 |
// With header |
| 72 |
part = tdhttp.MultipartPart{ |
| 73 |
Name: "pipo", |
| 74 |
Content: strings.NewReader("hey!\nyo!"), |
| 75 |
Header: http.Header{ |
| 76 |
"Pipo": []string{"bingo"}, |
| 77 |
"Content-Type": []string{"text/rococo; charset=utf-8"}, |
| 78 |
}, |
| 79 |
} |
| 80 |
check(&part, `Content-Disposition: form-data; name="pipo"%CR |
| 81 |
Content-Type: text/rococo; charset=utf-8%CR |
| 82 |
Pipo: bingo%CR |
| 83 |
%CR |
| 84 |
hey! |
| 85 |
yo!`) |
| 86 |
|
| 87 |
// Without name & body, but with header |
| 88 |
part = tdhttp.MultipartPart{ |
| 89 |
Header: http.Header{ |
| 90 |
"Pipo": []string{"bingo"}, |
| 91 |
}, |
| 92 |
} |
| 93 |
check(&part, `Pipo: bingo%CR |
| 94 |
`) |
| 95 |
|
| 96 |
// io.Reader |
| 97 |
check(tdhttp.NewMultipartPart("io", strings.NewReader("hey!\nyo!")), |
| 98 |
`Content-Disposition: form-data; name="io"%CR |
| 99 |
Content-Type: text/plain; charset=utf-8%CR |
| 100 |
%CR |
| 101 |
hey! |
| 102 |
yo!`) |
| 103 |
|
| 104 |
// io.Reader + Content-Type |
| 105 |
check(tdhttp.NewMultipartPart("io", strings.NewReader("hey!\nyo!"), "text/rococo; charset=utf-8"), |
| 106 |
`Content-Disposition: form-data; name="io"%CR |
| 107 |
Content-Type: text/rococo; charset=utf-8%CR |
| 108 |
%CR |
| 109 |
hey! |
| 110 |
yo!`) |
| 111 |
|
| 112 |
// String |
| 113 |
check(tdhttp.NewMultipartPartString("pipo", "hey!\nyo!"), |
| 114 |
`Content-Disposition: form-data; name="pipo"%CR |
| 115 |
Content-Type: text/plain; charset=utf-8%CR |
| 116 |
%CR |
| 117 |
hey! |
| 118 |
yo!`) |
| 119 |
|
| 120 |
// String + Content-Type |
| 121 |
check(tdhttp.NewMultipartPartString("pipo", "hey!\nyo!", "text/rococo; charset=utf-8"), |
| 122 |
`Content-Disposition: form-data; name="pipo"%CR |
| 123 |
Content-Type: text/rococo; charset=utf-8%CR |
| 124 |
%CR |
| 125 |
hey! |
| 126 |
yo!`) |
| 127 |
|
| 128 |
// Bytes |
| 129 |
check(tdhttp.NewMultipartPartBytes("pipo", []byte("hey!\nyo!")), |
| 130 |
`Content-Disposition: form-data; name="pipo"%CR |
| 131 |
Content-Type: text/plain; charset=utf-8%CR |
| 132 |
%CR |
| 133 |
hey! |
| 134 |
yo!`) |
| 135 |
|
| 136 |
// Bytes + Content-Type |
| 137 |
check(tdhttp.NewMultipartPartBytes("pipo", []byte("hey!\nyo!"), "text/rococo; charset=utf-8"), |
| 138 |
`Content-Disposition: form-data; name="pipo"%CR |
| 139 |
Content-Type: text/rococo; charset=utf-8%CR |
| 140 |
%CR |
| 141 |
hey! |
| 142 |
yo!`) |
| 143 |
|
| 144 |
// With file name |
| 145 |
dir, err := os.MkdirTemp("", "multipart") |
| 146 |
require.CmpNoError(err) |
| 147 |
defer os.RemoveAll(dir) //nolint: errcheck |
| 148 |
filePath := filepath.Join(dir, "body.txt") |
| 149 |
require.CmpNoError(os.WriteFile(filePath, []byte("hey!\nyo!"), 0666)) |
| 150 |
|
| 151 |
check(tdhttp.NewMultipartPartFile("pipo", filePath), |
| 152 |
`Content-Disposition: form-data; name="pipo"; filename="body.txt"%CR |
| 153 |
Content-Type: text/plain; charset=utf-8%CR |
| 154 |
%CR |
| 155 |
hey! |
| 156 |
yo!`) |
| 157 |
|
| 158 |
// With file name + Content-Type |
| 159 |
check(tdhttp.NewMultipartPartFile("pipo", filePath, "text/rococo; charset=utf-8"), |
| 160 |
`Content-Disposition: form-data; name="pipo"; filename="body.txt"%CR |
| 161 |
Content-Type: text/rococo; charset=utf-8%CR |
| 162 |
%CR |
| 163 |
hey! |
| 164 |
yo!`) |
| 165 |
|
| 166 |
// Error during os.Open |
| 167 |
_, err = io.ReadAll( |
| 168 |
tdhttp.NewMultipartPartFile("pipo", filepath.Join(dir, "unknown.xxx")), |
| 169 |
) |
| 170 |
assert.CmpError(err) |
| 171 |
} |
| 172 |
|
| 173 |
func TestMultipartBody(t *testing.T) { |
| 174 |
assert, require := td.AssertRequire(t) |
| 175 |
|
| 176 |
dir, err := os.MkdirTemp("", "multipart") |
| 177 |
require.CmpNoError(err) |
| 178 |
defer os.RemoveAll(dir) //nolint: errcheck |
| 179 |
filePath := filepath.Join(dir, "body.txt") |
| 180 |
require.CmpNoError(os.WriteFile(filePath, []byte("hey!\nyo!"), 0666)) |
| 181 |
|
| 182 |
for _, boundary := range []struct{ in, out string }{ |
| 183 |
{in: "", out: "go-testdeep-42"}, |
| 184 |
{in: "BoUnDaRy", out: "BoUnDaRy"}, |
| 185 |
} { |
| 186 |
multi := tdhttp.MultipartBody{ |
| 187 |
Boundary: boundary.in, |
| 188 |
Parts: []*tdhttp.MultipartPart{ |
| 189 |
{ |
| 190 |
Name: "pipo", |
| 191 |
Content: strings.NewReader("pipo!\nbingo!"), |
| 192 |
}, |
| 193 |
tdhttp.NewMultipartPartFile("file", filePath), |
| 194 |
tdhttp.NewMultipartPartString("string", "zip!\nzap!"), |
| 195 |
tdhttp.NewMultipartPartBytes("bytes", []byte(`{"ola":"hello"}`), "application/json"), |
| 196 |
tdhttp.NewMultipartPart("io", nil), |
| 197 |
tdhttp.NewMultipartPart("", nil), |
| 198 |
}, |
| 199 |
} |
| 200 |
|
| 201 |
expected := `--` + boundary.out + `%CR |
| 202 |
Content-Disposition: form-data; name="pipo"%CR |
| 203 |
Content-Type: text/plain; charset=utf-8%CR |
| 204 |
%CR |
| 205 |
pipo! |
| 206 |
bingo!%CR |
| 207 |
--` + boundary.out + `%CR |
| 208 |
Content-Disposition: form-data; name="file"; filename="body.txt"%CR |
| 209 |
Content-Type: text/plain; charset=utf-8%CR |
| 210 |
%CR |
| 211 |
hey! |
| 212 |
yo!%CR |
| 213 |
--` + boundary.out + `%CR |
| 214 |
Content-Disposition: form-data; name="string"%CR |
| 215 |
Content-Type: text/plain; charset=utf-8%CR |
| 216 |
%CR |
| 217 |
zip! |
| 218 |
zap!%CR |
| 219 |
--` + boundary.out + `%CR |
| 220 |
Content-Disposition: form-data; name="bytes"%CR |
| 221 |
Content-Type: application/json%CR |
| 222 |
%CR |
| 223 |
{"ola":"hello"}%CR |
| 224 |
--` + boundary.out + `%CR |
| 225 |
Content-Disposition: form-data; name="io"%CR |
| 226 |
%CR |
| 227 |
--` + boundary.out + `%CR |
| 228 |
%CR |
| 229 |
--` + boundary.out + `--%CR |
| 230 |
` |
| 231 |
|
| 232 |
var final bytes.Buffer |
| 233 |
// Read in 2 times to be sure Read() can be called several times |
| 234 |
_, err = io.CopyN(&final, &multi, 10) |
| 235 |
if !assert.CmpNoError(err) { |
| 236 |
continue |
| 237 |
} |
| 238 |
_, err := io.Copy(&final, &multi) |
| 239 |
if !assert.CmpNoError(err) { |
| 240 |
continue |
| 241 |
} |
| 242 |
if !assert.Cmp(final.String(), strings.ReplaceAll(expected, "%CR", "\r")) { |
| 243 |
continue |
| 244 |
} |
| 245 |
|
| 246 |
rd := multipart.NewReader(&final, boundary.out) |
| 247 |
|
| 248 |
// 0 |
| 249 |
part, err := rd.NextPart() |
| 250 |
if assert.CmpNoError(err) { |
| 251 |
assert.Cmp(part.FormName(), "pipo") |
| 252 |
assert.Cmp(part.FileName(), "") |
| 253 |
assert.Smuggle(part, io.ReadAll, td.String("pipo!\nbingo!")) |
| 254 |
} |
| 255 |
|
| 256 |
// 1 |
| 257 |
part, err = rd.NextPart() |
| 258 |
if assert.CmpNoError(err) { |
| 259 |
assert.Cmp(part.FormName(), "file") |
| 260 |
assert.Cmp(part.FileName(), "body.txt") |
| 261 |
assert.Smuggle(part, io.ReadAll, td.String("hey!\nyo!")) |
| 262 |
} |
| 263 |
|
| 264 |
// 2 |
| 265 |
part, err = rd.NextPart() |
| 266 |
if assert.CmpNoError(err) { |
| 267 |
assert.Cmp(part.FormName(), "string") |
| 268 |
assert.Cmp(part.FileName(), "") |
| 269 |
assert.Smuggle(part, io.ReadAll, td.String("zip!\nzap!")) |
| 270 |
} |
| 271 |
|
| 272 |
// 3 |
| 273 |
part, err = rd.NextPart() |
| 274 |
if assert.CmpNoError(err) { |
| 275 |
assert.Cmp(part.FormName(), "bytes") |
| 276 |
assert.Cmp(part.FileName(), "") |
| 277 |
assert.Smuggle(part, io.ReadAll, td.String(`{"ola":"hello"}`)) |
| 278 |
} |
| 279 |
|
| 280 |
// 4 |
| 281 |
part, err = rd.NextPart() |
| 282 |
if assert.CmpNoError(err) { |
| 283 |
assert.Cmp(part.FormName(), "io") |
| 284 |
assert.Cmp(part.FileName(), "") |
| 285 |
assert.Smuggle(part, io.ReadAll, td.String("")) |
| 286 |
} |
| 287 |
|
| 288 |
// 5 |
| 289 |
part, err = rd.NextPart() |
| 290 |
if assert.CmpNoError(err) { |
| 291 |
assert.Cmp(part.FormName(), "") |
| 292 |
assert.Cmp(part.FileName(), "") |
| 293 |
assert.Smuggle(part, io.ReadAll, td.String("")) |
| 294 |
} |
| 295 |
|
| 296 |
// EOF |
| 297 |
_, err = rd.NextPart() |
| 298 |
assert.Cmp(err, io.EOF) |
| 299 |
} |
| 300 |
|
| 301 |
multi := tdhttp.MultipartBody{} |
| 302 |
td.Cmp(t, multi.ContentType(), `multipart/form-data; boundary="go-testdeep-42"`) |
| 303 |
td.Cmp(t, multi.Boundary, "go-testdeep-42", |
| 304 |
"Boundary field set with default value") |
| 305 |
td.CmpEmpty(t, multi.MediaType, "MediaType field NOT set") |
| 306 |
|
| 307 |
multi.Boundary = "BoUnDaRy" |
| 308 |
td.Cmp(t, multi.ContentType(), `multipart/form-data; boundary="BoUnDaRy"`) |
| 309 |
|
| 310 |
multi.MediaType = "multipart/mixed" |
| 311 |
td.Cmp(t, multi.ContentType(), `multipart/mixed; boundary="BoUnDaRy"`) |
| 312 |
} |