-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipartstream_test.go
129 lines (100 loc) · 2.79 KB
/
multipartstream_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
package multipartstream
import (
"bytes"
"fmt"
"io"
"regexp"
"strings"
"testing"
)
const defaultErrFormat = "expected = %v, actual = %v"
func assertPanics(t *testing.T, f func()) {
defer func() {
if err := recover(); err == nil {
t.FailNow()
}
}()
f()
}
func Test_generateBoundary(t *testing.T) {
ms := NewMultipartStream()
re := regexp.MustCompile(`WebkitFormBoundary[0-9a-f]{20}`)
boundary := ms.generateBoundary()
if result := re.MatchString(boundary); !result {
t.Errorf(defaultErrFormat, true, result)
}
}
func TestBoundary(t *testing.T) {
re := regexp.MustCompile(`WebkitFormBoundary[0-9a-f]{20}`)
ms := NewMultipartStream()
if ms.Boundary() == "" {
t.Errorf(defaultErrFormat, "not-empty", ms.Boundary())
}
match := re.MatchString(ms.Boundary())
if !match {
t.Errorf(defaultErrFormat, true, match)
}
}
func TestContentType(t *testing.T) {
ms := NewMultipartStream()
contentType := ms.ContentType()
const expected = "multipart/form-data;"
if !strings.Contains(contentType, expected) {
t.Errorf(defaultErrFormat, expected, contentType)
}
}
func TestFormField(t *testing.T) {
ms := NewMultipartStream()
assertPanics(t, func() {
ms.FormField("some-field", nil)
})
ms.FormField("some-field", strings.NewReader("ok"))
var expected = fmt.Sprintf("--%s\nContent-Disposition: form-data; name=\"some-field\"\n\nok\n",
ms.Boundary(),
)
buf, _ := io.ReadAll(ms)
if !bytes.Equal(buf, []byte(expected)) {
t.Errorf(defaultErrFormat, expected, string(buf))
}
}
func TestFormFile(t *testing.T) {
ms := NewMultipartStream()
// panics cause of nil reader
assertPanics(t, func() {
ms.FormFile("file", "example.txt", nil)
})
// error -> could not determine mimetype
err := ms.FormFile("file", "example", strings.NewReader("some file contents..."))
if err == nil {
t.Errorf(defaultErrFormat, "not-nil", err)
}
// asserting error message: not extension
var expected = "no extension in filename"
if err.Error() != expected {
t.Errorf(defaultErrFormat, expected, err.Error())
}
err = ms.FormFile("file", "example.asdioieor", strings.NewReader("some file contents"))
if err == nil {
t.Errorf(defaultErrFormat, "not-nil", err)
}
// asserting error message: failure parsing mimetype
expected = "could not parse mimetype"
if err.Error() != expected {
t.Errorf(defaultErrFormat, expected, err.Error())
}
/// success
err = ms.FormFile("file", "example.txt", strings.NewReader("some file contents..."))
if err != nil {
t.Errorf(defaultErrFormat, nil, err)
}
}
func TestDone(t *testing.T) {
ms := NewMultipartStream()
ms.FormFile("file", "hello.txt", strings.NewReader("world!"))
ms.Done()
expected := []byte(ms.Boundary() + "--")
buf, _ := io.ReadAll(ms)
if !bytes.Contains(buf, expected) {
t.Errorf(defaultErrFormat, string(expected), string(buf))
}
}