on master help sign in
Remove ContentTypes.hs
commit0789682cf80971e359b49e419bbd519cdc755757 authorJulian K. Arni <jkarni@gmail.com> dateSat 21 Feb 2015, 18:05 (12 years ago) parente9f3341
5 files changed−187
servant-server.cabal−2 src/Servant.hs−2 src/Servant/Server.hs−1 src/Servant/Server/ContentTypes.hs−51 test/Servant/Server/ContentTypesSpec.hs−131
servant-server.cabal−2
@@ -31,7 +31,6 @@ library
3131 exposed-modules:
3232 Servant
3333 Servant.Server
34− Servant.Server.ContentTypes
3534 Servant.Server.Internal
3635 Servant.Utils.StaticFiles
3736 build-depends:
@@ -40,7 +39,6 @@ library
4039 , attoparsec >= 0.12 && < 0.13
4140 , bytestring >= 0.10 && < 0.11
4241 , either >= 4.3 && < 4.4
43− , http-media >= 0.4 && < 0.5
4442 , http-types >= 0.8 && < 0.9
4543 , network-uri >= 2.6 && < 2.7
4644 , safe >= 0.3 && < 0.4
src/Servant.hs−2
@@ -8,7 +8,6 @@ module Servant (
88 -- | Using your types in request paths and query string parameters
99 module Servant.Common.Text,
1010 -- | Utilities on top of the servant core
11− module Servant.QQ,
1211 module Servant.Utils.Links,
1312 module Servant.Utils.StaticFiles,
1413 -- | Useful re-exports
@@ -19,6 +18,5 @@ import Data.Proxy
1918import Servant.API
2019import Servant.Common.Text
2120import Servant.Server
22−import Servant.QQ
2321import Servant.Utils.Links
2422import Servant.Utils.StaticFiles
src/Servant/Server.hs−1
@@ -16,7 +16,6 @@ import Data.Proxy (Proxy)
1616import Network.Wai (Application)
1717
1818import Servant.Server.Internal
19−import Servant.Server.ContentTypes ()
2019
2120
2221-- * Implementing Servers
src/Servant/Server/ContentTypes.hsdeleted−51
@@ -1,51 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE ConstraintKinds #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE PolyKinds #-}
7−{-# LANGUAGE ScopedTypeVariables #-}
8−{-# LANGUAGE TypeFamilies #-}
9−{-# LANGUAGE TypeOperators #-}
10−{-# LANGUAGE UndecidableInstances #-}
11−module Servant.Server.ContentTypes where
12−
13−import Control.Monad (join)
14−import Control.Arrow (left)
15−import Data.Aeson (ToJSON(..), FromJSON(..), encode, eitherDecode)
16−import Data.ByteString.Lazy (ByteString)
17−import qualified Data.ByteString as BS
18−import Data.Proxy (Proxy(..))
19−import Data.String.Conversions (cs)
20−import qualified Data.Text.Lazy.Encoding as Text
21−import qualified Data.Text.Lazy as Text
22−import GHC.Exts (Constraint)
23−import qualified Network.HTTP.Media as M
24−
25−
26−import Servant.API ( XML, HTML, JSON, JavaScript, CSS, PlainText
27− , OctetStream, MimeRender(..), MimeUnrender(..) )
28−
29−
30−--------------------------------------------------------------------------
31−-- * MimeRender Instances
32−
33−-- | @encode@
34−instance ToJSON a => MimeRender JSON a where
35− toByteString _ = encode
36−
37−-- | @encodeUtf8@
38−instance MimeRender PlainText Text.Text where
39− toByteString _ = Text.encodeUtf8
40−
41−--------------------------------------------------------------------------
42−-- * MimeUnrender Instances
43−
44−-- | @decode@
45−instance FromJSON a => MimeUnrender JSON a where
46− fromByteString _ = eitherDecode
47−
48−-- | @Text.decodeUtf8'@
49−instance MimeUnrender PlainText Text.Text where
50− fromByteString _ = left show . Text.decodeUtf8'
51−
test/Servant/Server/ContentTypesSpec.hsdeleted−131
@@ -1,131 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE FlexibleInstances #-}
3−{-# LANGUAGE MultiParamTypeClasses #-}
4−{-# LANGUAGE OverloadedStrings #-}
5−{-# LANGUAGE TypeOperators #-}
6−{-# OPTIONS_GHC -fno-warn-orphans #-}
7−module Servant.Server.ContentTypesSpec where
8−
9−import Control.Applicative
10−import Data.Aeson (encode)
11−import Data.ByteString.Char8
12−import Data.Function (on)
13−import Data.Maybe (isJust, fromJust)
14−import Data.List (maximumBy)
15−import Data.Proxy (Proxy(..))
16−import qualified Data.Text.Lazy as T
17−import Data.String (IsString(..))
18−import Data.String.Conversions (cs)
19−import Network.HTTP.Types (hAccept)
20−import Network.Wai (pathInfo, requestHeaders)
21−import Network.Wai.Test ( runSession, request, defaultRequest
22− , assertContentType, assertStatus )
23−import Test.Hspec
24−import Test.QuickCheck
25−
26−import Servant.API
27−import Servant.API.ContentTypes
28−import Servant.Server
29−import Servant.Server.ContentTypes ()
30−
31−
32−spec :: Spec
33−spec = describe "Servant.Server.ContentTypes" $ do
34− handleAcceptHSpec
35− contentTypeSpec
36−
37−handleAcceptHSpec :: Spec
38−handleAcceptHSpec = describe "handleAcceptH" $ do
39−
40− it "should return Just if the 'Accept' header matches" $ do
41− handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
42− `shouldSatisfy` isJust
43− handleAcceptH (Proxy :: Proxy '[XML, JSON]) "application/json" (3 :: Int)
44− `shouldSatisfy` isJust
45− handleAcceptH (Proxy :: Proxy '[XML, JSON, HTML]) "text/html" (3 :: Int)
46− `shouldSatisfy` isJust
47−
48− it "should return the Content-Type as the first element of the tuple" $ do
49− handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (3 :: Int)
50− `shouldSatisfy` ((== "application/json;charset=utf-8") . fst . fromJust)
51− handleAcceptH (Proxy :: Proxy '[XML, JSON]) "application/json" (3 :: Int)
52− `shouldSatisfy` ((== "application/json;charset=utf-8") . fst . fromJust)
53− handleAcceptH (Proxy :: Proxy '[XML, JSON, HTML]) "text/html" (3 :: Int)
54− `shouldSatisfy` ((== "text/html;charset=utf-8") . fst . fromJust)
55−
56− it "should return the appropriately serialized representation" $ do
57− property $ \x -> handleAcceptH (Proxy :: Proxy '[JSON]) "*/*" (x :: Int)
58− == Just ("application/json;charset=utf-8", encode x)
59−
60− it "respects the Accept spec ordering" $
61− property $ \a b c i -> fst (fromJust $ val a b c i) == (fst $ highest a b c)
62− where
63− highest a b c = maximumBy (compare `on` snd) [ ("text/html;charset=utf-8", a)
64− , ("application/json;charset=utf-8", b)
65− , ("application/xml;charset=utf-8", c)
66− ]
67− acceptH a b c = addToAccept (Proxy :: Proxy HTML) a $
68− addToAccept (Proxy :: Proxy JSON) b $
69− addToAccept (Proxy :: Proxy XML ) c ""
70− val a b c i = handleAcceptH (Proxy :: Proxy '[HTML, JSON, XML])
71− (acceptH a b c) (i :: Int)
72−
73−type ContentTypeApi = "foo" :> Get '[JSON] Int
74− :<|> "bar" :> Get '[JSON, PlainText] T.Text
75−
76−contentTypeApi :: Proxy ContentTypeApi
77−contentTypeApi = Proxy
78−
79−contentTypeServer :: Server ContentTypeApi
80−contentTypeServer = return 5 :<|> return "hi"
81−
82−contentTypeSpec :: Spec
83−contentTypeSpec = do
84− describe "Accept Headers" $ do
85−
86− it "uses the highest quality possible in the header" $
87− flip runSession (serve contentTypeApi contentTypeServer) $ do
88− let acceptH = "text/plain; q=0.9, application/json; q=0.8"
89− response <- Network.Wai.Test.request defaultRequest{
90− requestHeaders = [(hAccept, acceptH)] ,
91− pathInfo = ["bar"]
92− }
93− assertContentType "text/plain;charset=utf8" response
94−
95− it "returns the first content-type if the Accept header is missing" $
96− flip runSession (serve contentTypeApi contentTypeServer) $ do
97− response <- Network.Wai.Test.request defaultRequest{
98− pathInfo = ["bar"]
99− }
100− assertContentType "application/json;charset=utf8" response
101−
102− it "returns 406 if it can't serve the requested content-type" $
103− flip runSession (serve contentTypeApi contentTypeServer) $ do
104− let acceptH = "text/css"
105− response <- Network.Wai.Test.request defaultRequest{
106− requestHeaders = [(hAccept, acceptH)] ,
107− pathInfo = ["bar"]
108− }
109− assertStatus 406 response
110−
111−
112−instance Show a => MimeRender HTML a where
113− toByteString _ = cs . show
114−
115−instance Show a => MimeRender XML a where
116− toByteString _ = cs . show
117−
118−instance IsString AcceptHeader where
119− fromString = AcceptHeader . fromString
120−
121−addToAccept :: Accept a => Proxy a -> ZeroToOne -> AcceptHeader -> AcceptHeader
122−addToAccept p (ZeroToOne f) (AcceptHeader h) = AcceptHeader (cont h)
123− where new = cs (show $ contentType p) `append` "; q=" `append` pack (show f)
124− cont "" = new
125− cont old = old `append` ", " `append` new
126−
127−newtype ZeroToOne = ZeroToOne Float
128− deriving (Eq, Show, Ord)
129−
130−instance Arbitrary ZeroToOne where
131− arbitrary = ZeroToOne <$> elements [ x / 10 | x <- [1..10]]