@@ -11,7 +11,7 @@ module Servant.API.ContentTypesSpec where
11import Prelude ()
11import Prelude ()
12import Prelude.Compat
12import Prelude.Compat
14−import Data.Aeson
14+import Data.Aeson.Compat
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
17import qualified Data.ByteString.Lazy.Char8 as BSL8
17import qualified Data.ByteString.Lazy.Char8 as BSL8
@@ -24,9 +24,9 @@ import Data.Proxy
24import Data.String (IsString (..))
24import Data.String (IsString (..))
25import Data.String.Conversions (cs)
25import Data.String.Conversions (cs)
26import qualified Data.Text as TextS
26import qualified Data.Text as TextS
27+import qualified Data.Text.Encoding as TextSE
27import qualified Data.Text.Lazy as TextL
28import qualified Data.Text.Lazy as TextL
28import GHC.Generics
29import GHC.Generics
29−import qualified Network.HTTP.Media as M
30import Test.Hspec
30import Test.Hspec
31import Test.QuickCheck
31import Test.QuickCheck
32import Text.Read (readMaybe)
32import Text.Read (readMaybe)
@@ -179,6 +179,15 @@ spec = describe "Servant.API.ContentTypes" $ do
179 handleCTypeH (Proxy :: Proxy '[JSONorText]) "image/jpeg"
179 handleCTypeH (Proxy :: Proxy '[JSONorText]) "image/jpeg"
180 "42" `shouldBe` (Nothing :: Maybe (Either String Int))
180 "42" `shouldBe` (Nothing :: Maybe (Either String Int))
182+ it "passes content-type to mimeUnrenderWithType" $ do
183+ let val = "foobar" :: TextS.Text
184+ handleCTypeH (Proxy :: Proxy '[JSONorText]) "application/json"
185+ "\"foobar\"" `shouldBe` Just (Right val)
186+ handleCTypeH (Proxy :: Proxy '[JSONorText]) "text/plain"
187+ "foobar" `shouldBe` Just (Right val)
188+ handleCTypeH (Proxy :: Proxy '[JSONorText]) "image/jpeg"
189+ "foobar" `shouldBe` (Nothing :: Maybe (Either String Int))
182#if MIN_VERSION_aeson(0,9,0)
191#if MIN_VERSION_aeson(0,9,0)
183 -- aeson >= 0.9 decodes top-level strings
192 -- aeson >= 0.9 decodes top-level strings
184 describe "eitherDecodeLenient" $ do
193 describe "eitherDecodeLenient" $ do
@@ -226,7 +235,7 @@ instance IsString AcceptHeader where
226data JSONorText
235data JSONorText
228instance Accept JSONorText where
237instance Accept JSONorText where
229− contentTypes _ = "text" M.// "plain" NE.:| [ "application" M.// "json" ]
238+ contentTypes _ = "text/plain" NE.:| [ "application/json" ]
231instance MimeRender JSONorText Int where
240instance MimeRender JSONorText Int where
232 mimeRender _ = cs . show
241 mimeRender _ = cs . show
@@ -234,6 +243,11 @@ instance MimeRender JSONorText Int where
234instance MimeUnrender JSONorText Int where
243instance MimeUnrender JSONorText Int where
235 mimeUnrender _ = maybe (Left "") Right . readMaybe . BSL8.unpack
244 mimeUnrender _ = maybe (Left "") Right . readMaybe . BSL8.unpack
246+instance MimeUnrender JSONorText TextS.Text where
247+ mimeUnrenderWithType _ mt
248+ | mt == "application/json" = maybe (Left "") Right . decode
249+ | otherwise = Right . TextSE.decodeUtf8 . BSL.toStrict
237addToAccept :: Accept a => Proxy a -> ZeroToOne -> AcceptHeader -> AcceptHeader
251addToAccept :: Accept a => Proxy a -> ZeroToOne -> AcceptHeader -> AcceptHeader
238addToAccept p (ZeroToOne f) (AcceptHeader h) = AcceptHeader (cont h)
252addToAccept p (ZeroToOne f) (AcceptHeader h) = AcceptHeader (cont h)
239 where new = cs (show $ contentType p) `append` "; q=" `append` pack (show f)
253 where new = cs (show $ contentType p) `append` "; q=" `append` pack (show f)