Debian Codegraph @

test/Web/Internal/HttpApiDataSpec.hs

raw
1 {-# LANGUAGE ScopedTypeVariables #-}
2 module Web.Internal.HttpApiDataSpec (spec) where
3
4
5 import qualified Data.ByteString as BS
6 import Data.ByteString.Builder (toLazyByteString)
7 import Data.Char
8 import qualified Data.Fixed as F
9 import Data.Int
10 import qualified Data.Text as T
11 import qualified Data.Text.Encoding as TE
12 import qualified Data.Text.Lazy as L
13 import Data.Time.Compat
14 import Data.Time.Calendar.Month.Compat (Month)
15 import Data.Time.Calendar.Quarter.Compat (Quarter, QuarterOfYear (..))
16 import qualified Data.UUID.Types as UUID
17 import Data.Version
18 import Data.Word
19 import Web.Cookie (SetCookie, defaultSetCookie,
20 setCookieName, setCookieValue)
21
22 import Data.Proxy
23
24 import Numeric.Natural
25
26 import Test.Hspec
27 import Test.Hspec.QuickCheck (prop)
28 import Test.QuickCheck
29
30 import Web.Internal.HttpApiData
31
32 import Web.Internal.TestInstances
33
34 (<=>) :: forall a b. (Show a, Show b, Eq a) => (a -> b) -> (b -> Either T.Text a) -> a -> Property
35 (f <=> g) x = counterexample
36 (show lhs' ++ " : " ++ show lhs ++ " /= " ++ show rhs)
37 (lhs == rhs)
38 where
39 lhs' = f x
40 lhs = g lhs' :: Either T.Text a
41 rhs = Right x :: Either T.Text a
42
43 encodedUrlPieceProp :: ToHttpApiData a => a -> Property
44 encodedUrlPieceProp x = toLazyByteString (toEncodedUrlPiece (toUrlPiece x)) === toLazyByteString (toEncodedUrlPiece x)
45
46 encodedQueryParamProp :: ToHttpApiData a => a -> Property
47 encodedQueryParamProp x = toLazyByteString (toEncodedQueryParam (toQueryParam x)) === toLazyByteString (toEncodedQueryParam x)
48
49 -- | Check 'ToHttpApiData' and 'FromHttpApiData' compatibility
50 checkUrlPiece :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
51 checkUrlPiece _ = checkUrlPiece' (arbitrary :: Gen a)
52
53 checkUrlPiece' :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a) => Gen a -> String -> Spec
54 checkUrlPiece' gen name = describe name $ do
55 prop "toUrlPiece <=> parseUrlPiece" $ forAll gen (toUrlPiece <=> parseUrlPiece :: a -> Property)
56 prop "toQueryParam <=> parseQueryParam" $ forAll gen (toQueryParam <=> parseQueryParam :: a -> Property)
57 prop "toHeader <=> parseHeader" $ forAll gen (toHeader <=> parseHeader :: a -> Property)
58 prop "toEncodedUrlPiece encodes correctly" $ forAll gen encodedUrlPieceProp
59 prop "toEncodedQueryParam encodes correctly" $ forAll gen encodedQueryParamProp
60
61 -- | Check case insensitivity for @parseUrlPiece@.
62 checkUrlPieceI :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Arbitrary a) => Proxy a -> String -> Spec
63 checkUrlPieceI _ = checkUrlPiece (Proxy :: Proxy (RandomCase a))
64
65 spec :: Spec
66 spec = do
67 describe "Instances" $ do
68 checkUrlPiece (Proxy :: Proxy ()) "()"
69 checkUrlPiece (Proxy :: Proxy Char) "Char"
70 checkUrlPieceI (Proxy :: Proxy Bool) "Bool"
71 checkUrlPieceI (Proxy :: Proxy Ordering) "Ordering"
72 checkUrlPiece (Proxy :: Proxy Int) "Int"
73 checkUrlPiece (Proxy :: Proxy Int8) "Int8"
74 checkUrlPiece (Proxy :: Proxy Int16) "Int16"
75 checkUrlPiece (Proxy :: Proxy Int32) "Int32"
76 checkUrlPiece (Proxy :: Proxy Int64) "Int64"
77 checkUrlPiece (Proxy :: Proxy Integer) "Integer"
78 checkUrlPiece (Proxy :: Proxy Word) "Word"
79 checkUrlPiece (Proxy :: Proxy Word8) "Word8"
80 checkUrlPiece (Proxy :: Proxy Word16) "Word16"
81 checkUrlPiece (Proxy :: Proxy Word32) "Word32"
82 checkUrlPiece (Proxy :: Proxy Word64) "Word64"
83 checkUrlPiece (Proxy :: Proxy String) "String"
84 checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
85 checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
86 checkUrlPiece (Proxy :: Proxy Day) "Day"
87 checkUrlPiece (Proxy :: Proxy TimeOfDay) "TimeOfDay"
88 checkUrlPiece (Proxy :: Proxy LocalTime) "LocalTime"
89 checkUrlPiece (Proxy :: Proxy ZonedTime) "ZonedTime"
90 checkUrlPiece (Proxy :: Proxy UTCTime) "UTCTime"
91 checkUrlPiece (Proxy :: Proxy NominalDiffTime) "NominalDiffTime"
92 checkUrlPiece (Proxy :: Proxy DayOfWeek) "DayOfWeek"
93 checkUrlPiece (Proxy :: Proxy Quarter) "Quarter"
94 checkUrlPiece (Proxy :: Proxy QuarterOfYear) "QuarterOfYear"
95 checkUrlPiece (Proxy :: Proxy Month) "Month"
96 checkUrlPiece (Proxy :: Proxy DayOfWeek) "DayOfWeek"
97 checkUrlPiece (Proxy :: Proxy Version) "Version"
98 checkUrlPiece (Proxy :: Proxy UUID.UUID) "UUID"
99 checkUrlPiece' setCookieGen "Cookie"
100
101 checkUrlPiece (Proxy :: Proxy F.Uni) "Uni"
102 checkUrlPiece (Proxy :: Proxy F.Deci) "Deci"
103 checkUrlPiece (Proxy :: Proxy F.Centi) "Centi"
104 checkUrlPiece (Proxy :: Proxy F.Milli) "Milli"
105 checkUrlPiece (Proxy :: Proxy F.Micro) "Micro"
106 checkUrlPiece (Proxy :: Proxy F.Nano) "Nano"
107 checkUrlPiece (Proxy :: Proxy F.Pico) "Pico"
108
109 checkUrlPiece (Proxy :: Proxy (Maybe String)) "Maybe String"
110 checkUrlPieceI (Proxy :: Proxy (Maybe Integer)) "Maybe Integer"
111 checkUrlPiece (Proxy :: Proxy (Either Integer T.Text)) "Either Integer Text"
112 checkUrlPieceI (Proxy :: Proxy (Either Version Day)) "Either Version Day"
113
114 checkUrlPiece (Proxy :: Proxy Natural) "Natural"
115
116 it "bad integers are rejected" $ do
117 parseUrlPieceMaybe (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
118
119 it "bounds checking works" $ do
120 parseUrlPieceMaybe (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
121 parseUrlPieceMaybe (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
122
123 it "invalid utf8 is handled" $ do
124 parseHeaderMaybe (BS.pack [128]) `shouldBe` (Nothing :: Maybe T.Text)
125
126 setCookieGen :: Gen SetCookie
127 setCookieGen = do
128 n <- TE.encodeUtf8 . T.pack . filter isAlphaNum <$> arbitrary
129 v <- TE.encodeUtf8 . T.pack . filter isAlphaNum <$> arbitrary
130 return $ defaultSetCookie { setCookieName = n, setCookieValue = v }