1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Web.Internal.FormUrlEncodedSpec (spec) where
import Control.Monad ((<=<))
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text, unpack)
import Test.Hspec
import Test.QuickCheck
import GHC.Exts (fromList)
import Web.Internal.FormUrlEncoded
import Web.Internal.HttpApiData
import Web.Internal.TestInstances
spec :: Spec
spec = do
genericSpec
urlEncoding
genericSpec :: Spec
genericSpec = describe "Default (generic) instances" $ do
context "ToForm" $ do
it "contains the record names" $ property $ \(x :: SimpleRec) -> do
let f = unForm $ toForm x
HashMap.member "rec1" f `shouldBe` True
HashMap.member "rec2" f `shouldBe` True
it "contains the correct record values" $ property $ \(x :: SimpleRec) -> do
let f = unForm $ toForm x
HashMap.lookup "rec1" f `shouldBe` Just [rec1 x]
(parseQueryParams <$> HashMap.lookup "rec2" f) `shouldBe` Just (Right [rec2 x])
context "FromForm" $ do
it "is the right inverse of ToForm" $ property $ \x (y :: Int) -> do
let f1 = fromList [("rec1", x), ("rec2", toQueryParam y)]
Right r1 = fromForm f1 :: Either Text SimpleRec
toForm r1 `shouldBe` f1
it "returns the underlying error" $ do
let f = fromList [("rec1", "anything"), ("rec2", "bad")]
Left e = fromForm f :: Either Text SimpleRec
unpack e `shouldContain` "input does not start with a digit"
urlEncoding :: Spec
urlEncoding = describe "urlEncoding" $ do
it "urlDecodeForm (urlEncodeForm x) == Right x" $ property $ \(NoEmptyKeyForm x) -> do
urlDecodeForm (urlEncodeForm x) `shouldBe` Right x
it "urlDecodeAsForm == (fromForm <=< urlDecodeForm)" $ property $ \(x :: BSL.ByteString) -> do
(urlDecodeAsForm x :: Either Text Form) `shouldBe` (fromForm <=< urlDecodeForm) x
it "urlEncodeAsForm == urlEncodeForm . toForm" $ property $ \(x :: Form) -> do
urlEncodeAsForm x `shouldBe` (urlEncodeForm . toForm) x
it "urlDecodeForm \"\" == Right mempty" $ do
urlDecodeForm "" `shouldBe` Right mempty
|