@@ -63,6 +63,7 @@ frameworkSpec app =
63
63
actionSpec
64
64
headerTest
65
65
cookieTest
66
+ fileTest
66
67
67
68
routingSpec :: SpecWith (st , Wai. Application )
68
69
routingSpec =
@@ -233,3 +234,76 @@ matchCookie name val =
233
234
then Nothing
234
235
else loop xs
235
236
in loop relevantHeaders
237
+
238
+ fileTest :: SpecWith (st , Wai. Application )
239
+ fileTest =
240
+ describe " " $
241
+ do
242
+ it " receives a single file" $
243
+ do
244
+ request methodPost " file/upload" headers bodySingle `shouldRespondWith` " 1" {matchStatus = 200 }
245
+ it " receives multiple files with different names" $
246
+ do
247
+ request methodPost " file/upload" headers bodyUnique `shouldRespondWith` " 2" {matchStatus = 200 }
248
+ it " receives multiple files with similar names" $
249
+ do
250
+ request methodPost " file/upload/multi" headers bodyMulti `shouldRespondWith` " 2" {matchStatus = 200 }
251
+ where
252
+ bodySingle = BSLC. pack $
253
+ boundary <> crlf
254
+ <> " Content-Disposition: form-data; name=\" name\" " <> crlf <> crlf
255
+ <> " file1.pdf" <> crlf
256
+ <> boundary <> crlf
257
+ <> " Content-Disposition: form-data; name=\" file\" ; filename=\" file1.pdf\" " <> crlf
258
+ <> " Content-Type: application/pdf" <> crlf
259
+ <> " Content-Transfer-Encoding: base64" <> crlf <> crlf
260
+ <> " aGFza2VsbA==" <> crlf
261
+ <> boundary <> " --" <> crlf
262
+
263
+ bodyUnique = BSLC. pack $
264
+ boundary <> crlf
265
+ <> " Content-Disposition: form-data; name=\" names\" " <> crlf <> crlf
266
+ <> " file1.pdf; file2.pdf" <> crlf
267
+ <> boundary <> crlf
268
+ <> " Content-Disposition: form-data; name=\" file1\" ; filename=\" file1.pdf\" " <> crlf
269
+ <> " Content-Type: application/pdf" <> crlf
270
+ <> " Content-Transfer-Encoding: base64" <> crlf <> crlf
271
+ <> " aGFza2VsbA==" <> crlf
272
+ <> boundary <> crlf
273
+ <> " Content-Disposition: form-data; name=\" file2\" ; filename=\" file2.pdf\" " <> crlf
274
+ <> " Content-Type: application/pdf" <> crlf
275
+ <> " Content-Transfer-Encoding: base64" <> crlf <> crlf
276
+ <> " c3BvY2s=" <> crlf
277
+ <> boundary <> " --" <> crlf
278
+
279
+ bodyMulti = BSLC. pack $
280
+ boundary <> crlf
281
+ <> " Content-Disposition: form-data; name=\" name1\" " <> crlf <> crlf
282
+ <> " file1.pdf" <> crlf
283
+ <> " Content-Disposition: form-data; name=\" name2\" " <> crlf <> crlf
284
+ <> " file2.pdf" <> crlf
285
+ <> boundary <> crlf
286
+ <> " Content-Disposition: form-data; name=\" file\" ; filename=\" file1.pdf\" " <> crlf
287
+ <> " Content-Type: application/pdf" <> crlf
288
+ <> " Content-Transfer-Encoding: base64" <> crlf <> crlf
289
+ <> " aGFza2VsbA==" <> crlf
290
+ <> boundary <> crlf
291
+ <> " Content-Disposition: form-data; name=\" file\" ; filename=\" file2.pdf\" " <> crlf
292
+ <> " Content-Type: application/pdf" <> crlf
293
+ <> " Content-Transfer-Encoding: base64" <> crlf <> crlf
294
+ <> " c3BvY2s=" <> crlf
295
+ <> boundary <> " --" <> crlf
296
+
297
+ boundary :: String
298
+ boundary = " --__boundary"
299
+
300
+ crlf :: String
301
+ crlf = " \r\n "
302
+
303
+ headers :: [Header ]
304
+ headers = [ mkHeader " Content-Type" " multipart/form-data; boundary=__boundary"
305
+ , mkHeader " Accept-Encoding" " gzip"
306
+ ]
307
+
308
+ mkHeader :: HeaderName -> BS. ByteString -> Header
309
+ mkHeader key val = (key, val)
0 commit comments