| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package binding |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"mime/multipart" |
| 21 |
"net/http" |
| 22 |
"net/http/httptest" |
| 23 |
"testing" |
| 24 |
|
| 25 |
chi "github.com/go-chi/chi/v5" |
| 26 |
"github.com/stretchr/testify/assert" |
| 27 |
) |
| 28 |
|
| 29 |
var fileTestCases = []fileTestCase{ |
| 30 |
{ |
| 31 |
description: "Single file", |
| 32 |
singleFile: &fileInfo{ |
| 33 |
fileName: "message.txt", |
| 34 |
data: "All your binding are belong to us", |
| 35 |
}, |
| 36 |
}, |
| 37 |
{ |
| 38 |
description: "Multiple files", |
| 39 |
multipleFiles: []*fileInfo{ |
| 40 |
{ |
| 41 |
fileName: "cool-gopher-fact.txt", |
| 42 |
data: "Did you know? https://plus.google.com/+MatthewHolt/posts/GmVfd6TPJ51", |
| 43 |
}, |
| 44 |
{ |
| 45 |
fileName: "gophercon2014.txt", |
| 46 |
data: "@bradfitz has a Go time machine: https://twitter.com/mholt6/status/459463953395875840", |
| 47 |
}, |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
description: "Single file and multiple files", |
| 52 |
singleFile: &fileInfo{ |
| 53 |
fileName: "social media.txt", |
| 54 |
data: "Hey, you should follow @mholt6 (Twitter) or +MatthewHolt (Google+)", |
| 55 |
}, |
| 56 |
multipleFiles: []*fileInfo{ |
| 57 |
{ |
| 58 |
fileName: "thank you!", |
| 59 |
data: "Also, thanks to all the contributors of this package!", |
| 60 |
}, |
| 61 |
{ |
| 62 |
fileName: "btw...", |
| 63 |
data: "This tool translates JSON into Go structs: http://mholt.github.io/json-to-go/", |
| 64 |
}, |
| 65 |
}, |
| 66 |
}, |
| 67 |
} |
| 68 |
|
| 69 |
func Test_FileUploads(t *testing.T) { |
| 70 |
for _, testCase := range fileTestCases { |
| 71 |
performFileTest(t, MultipartForm, testCase) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func performFileTest(t *testing.T, binder handlerFunc, testCase fileTestCase) { |
| 76 |
httpRecorder := httptest.NewRecorder() |
| 77 |
c := chi.NewRouter() |
| 78 |
|
| 79 |
fileTestHandler := func(actual BlogPost, _ Errors) { |
| 80 |
assertFileAsExpected(t, actual.HeaderImage, testCase.singleFile) |
| 81 |
assert.Len(t, actual.Pictures, len(testCase.multipleFiles)) |
| 82 |
|
| 83 |
for i, expectedFile := range testCase.multipleFiles { |
| 84 |
if i >= len(actual.Pictures) { |
| 85 |
break |
| 86 |
} |
| 87 |
assertFileAsExpected(t, actual.Pictures[i], expectedFile) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
c.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) { |
| 92 |
var actual BlogPost |
| 93 |
errs := binder(req, &actual) |
| 94 |
fileTestHandler(actual, errs) |
| 95 |
}) |
| 96 |
|
| 97 |
c.ServeHTTP(httpRecorder, buildRequestWithFile(testCase)) |
| 98 |
|
| 99 |
switch httpRecorder.Code { |
| 100 |
case http.StatusNotFound: |
| 101 |
panic("Routing is messed up in test fixture (got 404): check methods and paths") |
| 102 |
case http.StatusInternalServerError: |
| 103 |
panic("Something bad happened on '" + testCase.description + "'") |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func assertFileAsExpected(t *testing.T, actual *multipart.FileHeader, expected *fileInfo) { |
| 108 |
if expected == nil && actual == nil { |
| 109 |
return |
| 110 |
} |
| 111 |
|
| 112 |
if expected != nil && actual == nil { |
| 113 |
assert.NotNil(t, actual) |
| 114 |
return |
| 115 |
} else if expected == nil && actual != nil { |
| 116 |
assert.Nil(t, actual) |
| 117 |
return |
| 118 |
} |
| 119 |
|
| 120 |
assert.Equal(t, expected.fileName, actual.Filename) |
| 121 |
assert.Equal(t, expected.data, unpackFileHeaderData(actual)) |
| 122 |
} |
| 123 |
|
| 124 |
func buildRequestWithFile(testCase fileTestCase) *http.Request { |
| 125 |
b := &bytes.Buffer{} |
| 126 |
w := multipart.NewWriter(b) |
| 127 |
|
| 128 |
if testCase.singleFile != nil { |
| 129 |
formFileSingle, err := w.CreateFormFile("header_image", testCase.singleFile.fileName) |
| 130 |
if err != nil { |
| 131 |
panic("Could not create FormFile (single file): " + err.Error()) |
| 132 |
} |
| 133 |
_, _ = formFileSingle.Write([]byte(testCase.singleFile.data)) |
| 134 |
} |
| 135 |
|
| 136 |
for _, file := range testCase.multipleFiles { |
| 137 |
formFileMultiple, err := w.CreateFormFile("picture", file.fileName) |
| 138 |
if err != nil { |
| 139 |
panic("Could not create FormFile (multiple files): " + err.Error()) |
| 140 |
} |
| 141 |
_, _ = formFileMultiple.Write([]byte(file.data)) |
| 142 |
} |
| 143 |
|
| 144 |
err := w.Close() |
| 145 |
if err != nil { |
| 146 |
panic("Could not close multipart writer: " + err.Error()) |
| 147 |
} |
| 148 |
|
| 149 |
req, err := http.NewRequest("POST", testRoute, b) |
| 150 |
if err != nil { |
| 151 |
panic("Could not create file upload request: " + err.Error()) |
| 152 |
} |
| 153 |
|
| 154 |
req.Header.Set("Content-Type", w.FormDataContentType()) |
| 155 |
|
| 156 |
return req |
| 157 |
} |
| 158 |
|
| 159 |
func unpackFileHeaderData(fh *multipart.FileHeader) string { |
| 160 |
if fh == nil { |
| 161 |
return "" |
| 162 |
} |
| 163 |
|
| 164 |
f, err := fh.Open() |
| 165 |
if err != nil { |
| 166 |
panic("Could not open file header:" + err.Error()) |
| 167 |
} |
| 168 |
defer f.Close() |
| 169 |
|
| 170 |
var fb bytes.Buffer |
| 171 |
_, err = fb.ReadFrom(f) |
| 172 |
if err != nil { |
| 173 |
panic("Could not read from file header:" + err.Error()) |
| 174 |
} |
| 175 |
|
| 176 |
return fb.String() |
| 177 |
} |
| 178 |
|
| 179 |
type ( |
| 180 |
fileTestCase struct { |
| 181 |
description string |
| 182 |
singleFile *fileInfo |
| 183 |
multipleFiles []*fileInfo |
| 184 |
} |
| 185 |
|
| 186 |
fileInfo struct { |
| 187 |
fileName string |
| 188 |
data string |
| 189 |
} |
| 190 |
) |