@@ -14,9 +14,11 @@ import Prelude.Compat
14import Data.Aeson
14import Data.Aeson
15import Data.ByteString.Char8 (ByteString, append, pack)
15import Data.ByteString.Char8 (ByteString, append, pack)
16import qualified Data.ByteString.Lazy as BSL
16import qualified Data.ByteString.Lazy as BSL
17+import qualified Data.ByteString.Lazy.Char8 as BSL8
17import Data.Either
18import Data.Either
18import Data.Function (on)
19import Data.Function (on)
19import Data.List (maximumBy)
20import Data.List (maximumBy)
21+import qualified Data.List.NonEmpty as NE
20import Data.Maybe (fromJust, isJust, isNothing)
22import Data.Maybe (fromJust, isJust, isNothing)
21import Data.Proxy
23import Data.Proxy
22import Data.String (IsString (..))
24import Data.String (IsString (..))
@@ -24,8 +26,10 @@ import Data.String.Conversions (cs)
24import qualified Data.Text as TextS
26import qualified Data.Text as TextS
25import qualified Data.Text.Lazy as TextL
27import qualified Data.Text.Lazy as TextL
26import GHC.Generics
28import GHC.Generics
29+import qualified Network.HTTP.Media as M
27import Test.Hspec
30import Test.Hspec
28import Test.QuickCheck
31import Test.QuickCheck
32+import Text.Read (readMaybe)
29import "quickcheck-instances" Test.QuickCheck.Instances ()
33import "quickcheck-instances" Test.QuickCheck.Instances ()
31import Servant.API.ContentTypes
35import Servant.API.ContentTypes
@@ -101,6 +105,14 @@ spec = describe "Servant.API.ContentTypes" $ do
101 "application/octet-stream" ("content" :: ByteString)
105 "application/octet-stream" ("content" :: ByteString)
102 `shouldSatisfy` isJust
106 `shouldSatisfy` isJust
108+ it "returns Just if the 'Accept' header matches, with multiple mime types" $ do
109+ handleAcceptH (Proxy :: Proxy '[JSONorText]) "application/json" (3 :: Int)
110+ `shouldSatisfy` isJust
111+ handleAcceptH (Proxy :: Proxy '[JSONorText]) "text/plain" (3 :: Int)
112+ `shouldSatisfy` isJust
113+ handleAcceptH (Proxy :: Proxy '[JSONorText]) "image/jpeg" (3 :: Int)
104 it "returns the Content-Type as the first element of the tuple" $ do
116 it "returns the Content-Type as the first element of the tuple" $ do
105 handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
117 handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
106 `shouldSatisfy` ((== "application/json") . fst . fromJust)
118 `shouldSatisfy` ((== "application/json") . fst . fromJust)
@@ -158,6 +170,15 @@ spec = describe "Servant.API.ContentTypes" $ do
158 (encode val)
170 (encode val)
159 `shouldBe` Just (Right val)
171 `shouldBe` Just (Right val)
173+ it "returns Just (Right val) if the decoding succeeds for either of multiple mime-types" $ do
175+ handleCTypeH (Proxy :: Proxy '[JSONorText]) "application/json"
176+ "42" `shouldBe` Just (Right val)
177+ handleCTypeH (Proxy :: Proxy '[JSONorText]) "text/plain"
178+ "42" `shouldBe` Just (Right val)
179+ handleCTypeH (Proxy :: Proxy '[JSONorText]) "image/jpeg"
180+ "42" `shouldBe` (Nothing :: Maybe (Either String Int))
161#if MIN_VERSION_aeson(0,9,0)
182#if MIN_VERSION_aeson(0,9,0)
162 -- aeson >= 0.9 decodes top-level strings
183 -- aeson >= 0.9 decodes top-level strings
163 describe "eitherDecodeLenient" $ do
184 describe "eitherDecodeLenient" $ do
@@ -201,6 +222,18 @@ instance ToJSON ByteString where
201instance IsString AcceptHeader where
222instance IsString AcceptHeader where
202 fromString = AcceptHeader . fromString
223 fromString = AcceptHeader . fromString
225+-- To test multiple content types
228+instance Accept JSONorText where
229+ contentTypes _ = "text" M.// "plain" NE.:| [ "application" M.// "json" ]
231+instance MimeRender JSONorText Int where
232+ mimeRender _ = cs . show
234+instance MimeUnrender JSONorText Int where
235+ mimeUnrender _ = maybe (Left "") Right . readMaybe . BSL8.unpack
204addToAccept :: Accept a => Proxy a -> ZeroToOne -> AcceptHeader -> AcceptHeader
237addToAccept :: Accept a => Proxy a -> ZeroToOne -> AcceptHeader -> AcceptHeader
205addToAccept p (ZeroToOne f) (AcceptHeader h) = AcceptHeader (cont h)
238addToAccept p (ZeroToOne f) (AcceptHeader h) = AcceptHeader (cont h)
206 where new = cs (show $ contentType p) `append` "; q=" `append` pack (show f)
239 where new = cs (show $ contentType p) `append` "; q=" `append` pack (show f)