@@ -1,5 +1,4 @@
11{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveAnyClass #-}
32{-# LANGUAGE DeriveGeneric #-}
43{-# LANGUAGE OverloadedStrings #-}
54{-# LANGUAGE TypeOperators #-}
@@ -7,8 +6,11 @@
76module Servant.MockSpec where
87
98import Data.Aeson as Aeson
9+import Data.ByteString.Conversion.To
1010import Data.Proxy
11+import Data.String
1112import GHC.Generics
13+import Network.Wai
1214import Servant.API
1315import Test.Hspec hiding (pending)
1416import Test.Hspec.Wai
@@ -23,11 +25,24 @@ _ = mock comprehensiveAPI
2325data Body
2426 = Body
2527 | ArbitraryBody
26− deriving (Generic, ToJSON)
28+ deriving (Generic)
29+
30+instance ToJSON Body
2731
2832instance Arbitrary Body where
2933 arbitrary = return ArbitraryBody
3034
35+data TestHeader
36+ = TestHeader
37+ | ArbitraryHeader
38+ deriving (Show)
39+
40+instance ToByteString TestHeader where
41+ builder = fromString . show
42+
43+instance Arbitrary TestHeader where
44+ arbitrary = return ArbitraryHeader
45+
3146spec :: Spec
3247spec = do
3348 describe "mock" $ do
@@ -40,3 +55,28 @@ spec = do
4055 get "/" `shouldRespondWith` 200{
4156 matchBody = Just $ Aeson.encode ArbitraryBody
4257 }
58+
59+ context "response headers" $ do
60+ let withHeader :: Proxy (Get '[JSON] (Headers '[Header "foo" TestHeader] Body))
61+ withHeader = Proxy
62+ withoutHeader :: Proxy (Get '[JSON] (Headers '[] Body))
63+ withoutHeader = Proxy
64+ toApp :: HasMock api => Proxy api -> IO Application
65+ toApp api = return $ serve api (mock api)
66+ with (toApp withHeader) $ do
67+ it "serves arbitrary response bodies" $ do
68+ get "/" `shouldRespondWith` 200{
69+ matchHeaders = return $ MatchHeader $ \ h ->
70+ if h == [("Content-Type", "application/json"), ("foo", "ArbitraryHeader")]
71+ then Nothing
72+ else Just ("headers not correct\n")
73+ }
74+
75+ with (toApp withoutHeader) $ do
76+ it "works for no additional headers" $ do
77+ get "/" `shouldRespondWith` 200{
78+ matchHeaders = return $ MatchHeader $ \ h ->
79+ if h == [("Content-Type", "application/json")]
80+ then Nothing
81+ else Just ("headers not correct\n")
82+ }