Skip to content

Commit

Permalink
add tests, which currently fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Zocca committed Feb 27, 2024
1 parent f1b9cdc commit 85417b2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ files = do
bs <- liftIO $ BL.readFile (W.fileContent f)
pure (fname, f{ W.fileContent = bs})
)
{-# DEPRECATED files "This function is retained for backward compatibility, but loading all file contents in memory is not a good idea, please use filesOpts instead" #-}
{-# WARNING files "This function is retained for backward compatibility, but loading all file contents in memory is not a good idea, please use filesOpts instead" #-}

-- | Get list of uploaded temp files and form parameters decoded from multipart payloads.
--
Expand Down
4 changes: 3 additions & 1 deletion scotty.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Library
test-suite spec
main-is: Spec.hs
other-modules: Web.ScottySpec
Test.Hspec.Wai.Extra
type: exitcode-stdio-1.0
default-language: Haskell2010
hs-source-dirs: test
Expand All @@ -121,7 +122,8 @@ test-suite spec
scotty,
text,
time,
wai
wai,
wai-extra
build-tool-depends: hspec-discover:hspec-discover == 2.*
GHC-options: -Wall -threaded -fno-warn-orphans

Expand Down
21 changes: 19 additions & 2 deletions test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
module Web.ScottySpec (main, spec) where

import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai (with, request, get, post, put, patch, delete, options, (<:>), shouldRespondWith, postHtmlForm, matchHeaders, matchBody, matchStatus)
import Test.Hspec.Wai.Extra (postMultipartForm, FileMeta(..))

import Control.Applicative
import Control.Monad
Expand Down Expand Up @@ -65,7 +66,7 @@ spec = do
makeRequest "/:paramName" `shouldRespondWith` ":paramName"
it ("captures route parameters for " ++ method ++ " requests with url encoded '/' in path") $ do
makeRequest "/a%2Fb" `shouldRespondWith` "a/b"

describe "addroute" $ do
forM_ availableMethods $ \method -> do
withApp (addroute method "/scotty" $ html "") $ do
Expand Down Expand Up @@ -319,6 +320,22 @@ spec = do
it "responds with 200 OK if the parameter is not found" $ do
get "/b/potato" `shouldRespondWith` 200

describe "files" $ do
withApp (Scotty.post "/files" $ do
body >>= liftIO . print -- XXX DEBUG
-- "Content-Type: multipart/form-data; boundary=ABC123\n\n--ABC123\nContent-Disposition: form-data; name=\"first_file\"; filename=\"file1.txt\"\nContent-Type: text/plain\n\n{file-contents}\n--ABC123--"
fs <- files
text $ TL.pack $ show $ length fs) $ do
it "loads uploaded files in memory" $ do
postMultipartForm "/files" "ABC123" [(FMFile "file1.txt", "text/plain", "first_file", "{file-contents}")] `shouldRespondWith` 200 { matchBody = "1"}

describe "filesOpts" $ do
withApp (Scotty.post "/files" $ do
filesOpts defaultParseRequestBodyOptions $ \_ fs -> do
text $ TL.pack $ show $ length fs) $ do
it "loads uploaded files in memory" $ do
postMultipartForm "/files" "ABC123" [(FMFile "file1.txt", "text/plain", "first_file", "{file-contents}")] `shouldRespondWith` 200 { matchBody = "1"}


describe "text" $ do
let modernGreekText :: IsString a => a
Expand Down

0 comments on commit 85417b2

Please sign in to comment.