Skip to content

Commit

Permalink
Add test for url-encoding in formData
Browse files Browse the repository at this point in the history
Also move postForm helper function to top level
  • Loading branch information
pbrinkmeier committed Mar 31, 2024
1 parent 46cc0c1 commit 7e587ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Binary file added test/Web/.ScottySpec.hs.swp
Binary file not shown.
22 changes: 14 additions & 8 deletions test/Web/ScottySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module Web.ScottySpec (main, spec) where

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

import Control.Applicative
Expand All @@ -21,6 +21,7 @@ import GHC.Generics (Generic)
import Network.HTTP.Types
import Network.Wai (Application, Request(queryString), responseLBS)
import Network.Wai.Parse (defaultParseRequestBodyOptions)
import Network.Wai.Test (SResponse)
import qualified Control.Exception.Lifted as EL
import qualified Control.Exception as E

Expand All @@ -34,6 +35,7 @@ import Control.Concurrent.Async (withAsync)
import Control.Exception (bracketOnError)
import qualified Data.ByteString as BS
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
import Network.Socket (Family(..), SockAddr(..), Socket, SocketOption(..), SocketType(..), bind, close, connect, listen, maxListenQueue, setSocketOption, socket)
import Network.Socket.ByteString (send, recv)
import System.Directory (removeFile)
Expand All @@ -52,6 +54,9 @@ data SearchForm = SearchForm

instance FromForm SearchForm where

postForm :: ByteString -> LBS.ByteString -> WaiSession st SResponse
postForm p = request "POST" p [("Content-Type","application/x-www-form-urlencoded")]

spec :: Spec
spec = do
let withApp = with . scottyApp
Expand Down Expand Up @@ -285,14 +290,15 @@ spec = do
describe "formData" $ do
withApp (Scotty.post "/search" $ formData >>= (text . sfQuery)) $ do
it "decodes the form" $ do
postHtmlForm "/search" [("sfQuery", "Haskell"), ("sfYear", "2024")] `shouldRespondWith` "Haskell"
postForm "/search" "sfQuery=Haskell&sfYear=2024" `shouldRespondWith` "Haskell"

it "decodes URL-encoding" $ do
postForm "/search" "sfQuery=Kurf%C3%BCrstendamm&sfYear=2024" `shouldRespondWith` "Kurfürstendamm"

it "returns 400 when the form can't is malformed" $ do
postHtmlForm "/search" [("sfQuery", "Haskell")] `shouldRespondWith` 400
postForm "/search" "sfQuery=Haskell" `shouldRespondWith` 400

describe "formParam" $ do
let
postForm p bdy = request "POST" p [("Content-Type","application/x-www-form-urlencoded")] bdy
withApp (Scotty.post "/search" $ formParam "query" >>= text) $ do
it "returns form parameter with given name" $ do
postForm "/search" "query=haskell" `shouldRespondWith` "haskell"
Expand Down Expand Up @@ -378,7 +384,7 @@ spec = do

describe "filesOpts" $ do
let
postForm = postMultipartForm "/files" "ABC123" [
postMpForm = postMultipartForm "/files" "ABC123" [
(FMFile "file1.txt", "text/plain;charset=UTF-8", "first_file", "xxx"),
(FMFile "file2.txt", "text/plain;charset=UTF-8", "second_file", "yyy")
]
Expand All @@ -388,13 +394,13 @@ spec = do
withApp (Scotty.post "/files" processForm
) $ do
it "loads uploaded files in memory" $ do
postForm `shouldRespondWith` 200 { matchBody = "2"}
postMpForm `shouldRespondWith` 200 { matchBody = "2"}
context "preserves the body of a POST request even after 'next' (#147)" $ do
withApp (do
Scotty.post "/files" next
Scotty.post "/files" processForm) $ do
it "loads uploaded files in memory" $ do
postForm `shouldRespondWith` 200 { matchBody = "2"}
postMpForm `shouldRespondWith` 200 { matchBody = "2"}


describe "text" $ do
Expand Down

0 comments on commit 7e587ca

Please sign in to comment.