@@ -8,6 +8,7 @@
88module Servant.ServerSpec where
99
1010
11+import Control.Monad (when)
1112import Control.Monad.Trans.Either (EitherT, left)
1213import Data.Aeson (ToJSON, FromJSON, encode, decode')
1314import Data.Char (toUpper)
@@ -16,7 +17,8 @@ import Data.Proxy (Proxy(Proxy))
1617import Data.String (fromString)
1718import Data.String.Conversions (cs)
1819import 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)
2022import Network.Wai ( Application, Request, responseLBS, pathInfo
2123 , queryString, rawQueryString )
2224import Network.Wai.Test (runSession, defaultRequest, simpleBody, request)
@@ -24,16 +26,9 @@ import Test.Hspec (Spec, describe, it, shouldBe)
2426import Test.Hspec.Wai ( liftIO, with, get, post, shouldRespondWith
2527 , matchStatus, request )
2628
27−import Servant.API (JSON)
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)
34−import Servant.API.Raw (Raw)
35−import Servant.API.Sub ((:>))
36−import Servant.API.Alternative ((:<|>)((:<|>)))
29+import Servant.API (JSON, Capture, Get, ReqBody, Post, QueryParam
30+ , QueryParams, QueryFlag, MatrixParam, MatrixParams
31+ , MatrixFlag, Raw, (:>), (:<|>)(..), Header, Delete )
3732import Servant.Server (Server, serve)
3833import Servant.Server.Internal (RouteMismatch(..))
3934
@@ -77,6 +72,7 @@ spec = do
7772 queryParamSpec
7873 matrixParamSpec
7974 postSpec
75+ headerSpec
8076 rawSpec
8177 unionSpec
8278 errorsSpec
@@ -328,6 +324,33 @@ postSpec = do
328324 , "application/nonsense")]
329325 post'' "/" "anything at all" `shouldRespondWith` 415
330326
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+
331354
332355type RawApi = "foo" :> Raw
333356rawApi :: Proxy RawApi