add more tests for multipart parsing
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Dec 28, 2015 at 11:14 UTC
19bc5fe8458ff55fa1f3283fd2a58d9ccd0e4741
1 file changed
+54
commands/files/file_test.go
+54
@@ -91,6 +91,16 @@ beep
91
Content-Type: application/x-directory
92
Content-Disposition: file; filename="dir"
93
94
+--Boundary!
95
+Content-Type: text/plain
96
+Content-Disposition: file; filename="dir/nested"
97
+
98
+some content
99
+--Boundary!
100
+Content-Type: application/symlink
101
+Content-Disposition: file; filename="dir/simlynk"
102
+
103
+anotherfile
104
--Boundary!--
105
106
`
@@ -146,4 +156,48 @@ Content-Disposition: file; filename="dir"
156
t.Fatal("Shouldn't be able to call `Close` on a directory")
157
}
158
159
+ // test properties of file created from third part (nested file)
160
+ part, err = mpReader.NextPart()
161
+ if part == nil || err != nil {
162
+ t.Fatal("Expected non-nil part, nil error")
163
+ }
164
+ mpf, err = NewFileFromPart(part)
165
+ if mpf == nil || err != nil {
166
+ t.Fatal("Expected non-nil MultipartFile, nil error")
167
+ }
168
+ if mpf.IsDirectory() {
169
+ t.Fatal("Expected file, got directory")
170
+ }
171
+ if mpf.FileName() != "dir/nested" {
172
+ t.Fatalf("Expected filename to be \"nested\", got %s", mpf.FileName())
173
+ }
174
+ if n, err := mpf.Read(buf); n != 12 || err != nil {
175
+ t.Fatalf("expected to be able to read 12 bytes from file: %s (got %d)", err, n)
176
+ }
177
+ if err := mpf.Close(); err != nil {
178
+ t.Fatal("should be able to close file: %s", err)
179
+ }
180
+
181
+ // test properties of symlink created from fourth part (symlink)
182
+ part, err = mpReader.NextPart()
183
+ if part == nil || err != nil {
184
+ t.Fatal("Expected non-nil part, nil error")
185
+ }
186
+ mpf, err = NewFileFromPart(part)
187
+ if mpf == nil || err != nil {
188
+ t.Fatal("Expected non-nil MultipartFile, nil error")
189
+ }
190
+ if mpf.IsDirectory() {
191
+ t.Fatal("Expected file to be a symlink")
192
+ }
193
+ if mpf.FileName() != "dir/simlynk" {
194
+ t.Fatal("Expected filename to be \"dir/simlynk\"")
195
+ }
196
+ slink, ok := mpf.(*Symlink)
197
+ if !ok {
198
+ t.Fatalf("expected file to be a symlink")
199
+ }
200
+ if slink.Target != "anotherfile" {
201
+ t.Fatal("expected link to point to anotherfile")
202
+ }
203
}