Debian Codegraph @

test/Web/Internal/FormUrlEncodedSpec.hs

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