Skip to content

Commit

Permalink
Add test for multipart form data
Browse files Browse the repository at this point in the history
  • Loading branch information
istathar committed Aug 21, 2021
1 parent 584c722 commit b3aa832
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
38 changes: 34 additions & 4 deletions tests/TestSuite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ suite = do
testEstablishConnection
testParsingJson1
testParsingJson2
testPostWithSimple
testPostWithJson
testPutWithSimple
testPutWithJson
testMultipartUpload

describe "Corner cases in protocol compliance" $ do
testSendBodyFor PUT
Expand Down Expand Up @@ -787,7 +788,7 @@ instance ToJSON GrossDomesticProduct where
, "data" .= d
]

testPostWithSimple =
testPutWithSimple =
it "PUT with static data" $ do
let url = S.concat ["http://", localhost, "/resource/y98"]

Expand All @@ -801,7 +802,7 @@ testPostWithSimple =
b' :: ByteString
b' = S.pack "Hello"

testPostWithJson =
testPutWithJson =
it "PUT with json data" $ do
let url = S.concat ["http://", localhost, "/resource/y99"]

Expand All @@ -821,3 +822,32 @@ testPostWithJson =

obj' :: ByteString
obj' = L.toStrict (encode obj)

testMultipartUpload =
it "PUT with json data" $ do
let url = S.concat ["http://", localhost, "/postbox"]

let boundary = packBoundary "bEacHV0113YB@ll"

let q = buildRequest1 $ do
http POST "/postbox"
setContentMultipart boundary

let parts =
[ simplePart "first" Nothing "Old guys in the box."
, filePart "second" (Just "text/plain") "tests/hello.txt"
]

c <- openConnection "localhost" localPort

sendRequest c q (multipartFormBody boundary parts)

result <- receiveResponse c simpleHandler

closeConnection c

expected <- S.readFile "tests/multipart.bin"
assertEqual
"Multipart form data was not encoded as expected"
expected
result
12 changes: 12 additions & 0 deletions tests/multipart.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

--bEacHV0113YB@ll
Content-Disposition: form-data; name="first"

Old guys in the box.
--bEacHV0113YB@ll
Content-Disposition: form-data; name="second"; filename="hello.txt"
Content-Type: text/plain

Hello World

--bEacHV0113YB@ll--

0 comments on commit b3aa832

Please sign in to comment.