@@ -13,7 +13,7 @@ import Data.Proxy
1313import Data.ByteString.Char8
1414import qualified Data.ByteString.Lazy as BSL
1515import Data.List (maximumBy)
16−import Data.Maybe (fromJust, isJust)
16+import Data.Maybe (fromJust, isJust, isNothing)
1717import Data.String (IsString (..))
1818import Data.String.Conversions (cs)
1919import qualified Data.Text as TextS
@@ -61,6 +61,10 @@ spec = describe "Servant.API.ContentTypes" $ do
6161
6262 describe "handleAcceptH" $ do
6363
64+ it "returns Nothing if the 'Accept' header doesn't match" $ do
65+ handleAcceptH (Proxy :: Proxy '[JSON]) "text/plain" (3 :: Int)
66+ `shouldSatisfy` isNothing
67+
6468 it "returns Just if the 'Accept' header matches" $ do
6569 handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
6670 `shouldSatisfy` isJust
@@ -72,30 +76,55 @@ spec = describe "Servant.API.ContentTypes" $ do
7276
7377 it "returns the Content-Type as the first element of the tuple" $ do
7478 handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
75− `shouldSatisfy` ((== "application/json;charset=utf-8") . fst . fromJust)
79+ `shouldSatisfy` ((== "application/json") . fst . fromJust)
7680 handleAcceptH (Proxy :: Proxy '[PlainText, JSON]) "application/json" (3 :: Int)
77− `shouldSatisfy` ((== "application/json;charset=utf-8") . fst . fromJust)
81+ `shouldSatisfy` ((== "application/json") . fst . fromJust)
7882 handleAcceptH (Proxy :: Proxy '[PlainText, JSON, OctetStream])
7983 "application/octet-stream" ("content" :: ByteString)
8084 `shouldSatisfy` ((== "application/octet-stream") . fst . fromJust)
8185
8286 it "returns the appropriately serialized representation" $ do
8387 property $ \x -> handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (x :: SomeData)
84− == Just ("application/json;charset=utf-8", encode x)
85−
86− it "respects the Accept spec ordering" $
87− property $ \a b c i -> fst (fromJust $ val a b c i) == (fst $ highest a b c)
88− where
89− highest a b c = maximumBy (compare `on` snd)
90− [ ("application/octet-stream", a)
91− , ("application/json;charset=utf-8", b)
92− , ("text/plain;charset=utf-8", c)
93− ]
94− acceptH a b c = addToAccept (Proxy :: Proxy OctetStream) a $
95− addToAccept (Proxy :: Proxy JSON) b $
96− addToAccept (Proxy :: Proxy PlainText ) c ""
97− val a b c i = handleAcceptH (Proxy :: Proxy '[OctetStream, JSON, PlainText])
88+ == Just ("application/json", encode x)
89+
90+ it "respects the Accept spec ordering" $ do
91+ let highest a b c = maximumBy (compare `on` snd)
92+ [ ("application/octet-stream", a)
93+ , ("application/json", b)
94+ , ("text/plain;charset=utf-8", c)
95+ ]
96+ let acceptH a b c = addToAccept (Proxy :: Proxy OctetStream) a $
97+ addToAccept (Proxy :: Proxy JSON) b $
98+ addToAccept (Proxy :: Proxy PlainText ) c ""
99+ let val a b c i = handleAcceptH (Proxy :: Proxy '[OctetStream, JSON, PlainText])
98100 (acceptH a b c) (i :: Int)
101+ property $ \a b c i -> fst (fromJust $ val a b c i) == fst (highest a b c)
102+
103+ describe "handleCTypeH" $ do
104+
105+ it "returns Nothing if the 'Content-Type' header doesn't match" $ do
106+ handleCTypeH (Proxy :: Proxy '[JSON]) "text/plain" "𝓽𝓱𝓮 𝓽𝓲𝓶𝓮 𝓱𝓪𝓼 𝓬𝓸𝓶𝓮, 𝓽𝓱𝓮 𝔀𝓪𝓵𝓻𝓾𝓼 𝓼𝓪𝓲𝓭 "
107+ `shouldBe` (Nothing :: Maybe (Either String Value))
108+
109+ context "the 'Content-Type' header matches" $ do
110+ it "returns Just if the parameter matches" $ do
111+ handleCTypeH (Proxy :: Proxy '[JSON]) "application/json"
112+ "𝕥𝕠 𝕥𝕒𝕝𝕜 𝕠𝕗 𝕞𝕒𝕟𝕪 𝕥𝕙𝕚𝕟𝕘𝕤 "
113+ `shouldSatisfy` (isJust :: Maybe (Either String Value) -> Bool)
114+
115+ it "returns Just if there is no parameter" $ do
116+ handleCTypeH (Proxy :: Proxy '[JSON]) "application/json"
117+ "𝕥𝕠 𝕥𝕒𝕝𝕜 𝕠𝕗 𝕞𝕒𝕟𝕪 𝕥𝕙𝕚𝕟𝕘𝕤 "
118+ `shouldSatisfy` (isJust :: Maybe (Either String Value) -> Bool)
119+
120+ it "returns Just Left if the decoding fails" $ do
121+ let isJustLeft :: Maybe (Either String Value) -> Bool
122+ isJustLeft (Just (Left _)) = True
123+ isJustLeft _ = False
124+ handleCTypeH (Proxy :: Proxy '[JSON]) "application/json"
125+ "𝕺𝖋 𝖘𝖍𝖔𝖊𝖘--𝖆𝖓𝖉 𝖘𝖍𝖎𝖕𝖘--𝖆𝖓𝖉 𝖘𝖊𝖆𝖑𝖎𝖓𝖌-𝖜𝖆𝖝-- "
126+ `shouldSatisfy` isJustLeft
127+
99128
100129data SomeData = SomeData { record1 :: String, record2 :: Int }
101130 deriving (Generic, Eq, Show)