on master help sign in
Pay down some coverage debt
b96a2d2Julian K. Arni
test/Servant/ServerSpec.hs+34−11
@@ -8,6 +8,7 @@
8module Servant.ServerSpec where
8module Servant.ServerSpec where
9
9
10
10
11+import Control.Monad (when)
11import Control.Monad.Trans.Either (EitherT, left)
12import Control.Monad.Trans.Either (EitherT, left)
12import Data.Aeson (ToJSON, FromJSON, encode, decode')
13import Data.Aeson (ToJSON, FromJSON, encode, decode')
13import Data.Char (toUpper)
14import Data.Char (toUpper)
@@ -16,7 +17,8 @@ import Data.Proxy (Proxy(Proxy))
16import Data.String (fromString)
17import Data.String (fromString)
17import Data.String.Conversions (cs)
18import Data.String.Conversions (cs)
18import GHC.Generics (Generic)
19import GHC.Generics (Generic)
19−import Network.HTTP.Types (parseQuery, ok200, status409, methodPost, hContentType)
20+import Network.HTTP.Types ( parseQuery, ok200, status409, methodPost
21+ , methodDelete, hContentType)
20import Network.Wai ( Application, Request, responseLBS, pathInfo
22import Network.Wai ( Application, Request, responseLBS, pathInfo
21 , queryString, rawQueryString )
23 , queryString, rawQueryString )
22import Network.Wai.Test (runSession, defaultRequest, simpleBody, request)
24import Network.Wai.Test (runSession, defaultRequest, simpleBody, request)
@@ -24,16 +26,9 @@ import Test.Hspec (Spec, describe, it, shouldBe)
24import Test.Hspec.Wai ( liftIO, with, get, post, shouldRespondWith
26import Test.Hspec.Wai ( liftIO, with, get, post, shouldRespondWith
25 , matchStatus, request )
27 , matchStatus, request )
26
28
27−import Servant.API (JSON)
29+import Servant.API (JSON, Capture, Get, ReqBody, Post, QueryParam
28−import Servant.API.Capture (Capture)
29−import Servant.API.Get (Get)
30−import Servant.API.ReqBody (ReqBody)
31−import Servant.API.Post (Post)
32−import Servant.API.QueryParam (QueryParam, QueryParams, QueryFlag)
33−import Servant.API.MatrixParam (MatrixParam, MatrixParams, MatrixFlag)
30+ , QueryParams, QueryFlag, MatrixParam, MatrixParams
34−import Servant.API.Raw (Raw)
31+ , MatrixFlag, Raw, (:>), (:<|>)(..), Header, Delete )
35−import Servant.API.Sub ((:>))
36−import Servant.API.Alternative ((:<|>)((:<|>)))
37import Servant.Server (Server, serve)
32import Servant.Server (Server, serve)
38import Servant.Server.Internal (RouteMismatch(..))
33import Servant.Server.Internal (RouteMismatch(..))
39
34
@@ -77,6 +72,7 @@ spec = do
77 queryParamSpec
72 queryParamSpec
78 matrixParamSpec
73 matrixParamSpec
79 postSpec
74 postSpec
75+ headerSpec
80 rawSpec
76 rawSpec
81 unionSpec
77 unionSpec
82 errorsSpec
78 errorsSpec
@@ -328,6 +324,33 @@ postSpec = do
328 , "application/nonsense")]
324 , "application/nonsense")]
329 post'' "/" "anything at all" `shouldRespondWith` 415
325 post'' "/" "anything at all" `shouldRespondWith` 415
330
326
327+type HeaderApi a = Header "MyHeader" a :> Delete
328+headerApi :: Proxy (HeaderApi a)
329+headerApi = Proxy
330+
331+headerSpec :: Spec
332+headerSpec = describe "Servant.API.Header" $ do
333+
334+ let expectsInt :: Maybe Int -> EitherT (Int,String) IO ()
335+ expectsInt (Just x) = when (x /= 5) $ error "Expected 5"
336+ expectsInt Nothing = error "Expected an int"
337+
338+ let expectsString :: Maybe String -> EitherT (Int,String) IO ()
339+ expectsString (Just x) = when (x /= "more from you") $ error "Expected more from you"
340+ expectsString Nothing = error "Expected a string"
341+
342+ with (return (serve headerApi expectsInt)) $ do
343+ let delete' x = Test.Hspec.Wai.request methodDelete x [("MyHeader" ,"5")]
344+
345+ it "passes the header to the handler (Int)" $
346+ delete' "/" "" `shouldRespondWith` 204
347+
348+ with (return (serve headerApi expectsString)) $ do
349+ let delete' x = Test.Hspec.Wai.request methodDelete x [("MyHeader" ,"more from you")]
350+
351+ it "passes the header to the handler (String)" $
352+ delete' "/" "" `shouldRespondWith` 204
353+
331
354
332type RawApi = "foo" :> Raw
355type RawApi = "foo" :> Raw
333rawApi :: Proxy RawApi
356rawApi :: Proxy RawApi