@@ -1,5 +1,4 @@
1{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveAnyClass #-}
3{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE TypeOperators #-}
4{-# LANGUAGE TypeOperators #-}
@@ -7,8 +6,11 @@
7module Servant.MockSpec where
6module Servant.MockSpec where
9import Data.Aeson as Aeson
8import Data.Aeson as Aeson
9+import Data.ByteString.Conversion.To
10import Data.Proxy
10import Data.Proxy
11import GHC.Generics
12import GHC.Generics
12import Servant.API
14import Servant.API
13import Test.Hspec hiding (pending)
15import Test.Hspec hiding (pending)
14import Test.Hspec.Wai
16import Test.Hspec.Wai
@@ -23,11 +25,24 @@ _ = mock comprehensiveAPI
25 | ArbitraryBody
27 | ArbitraryBody
26− deriving (Generic, ToJSON)
28+ deriving (Generic)
28instance Arbitrary Body where
32instance Arbitrary Body where
29 arbitrary = return ArbitraryBody
33 arbitrary = return ArbitraryBody
40+instance ToByteString TestHeader where
41+ builder = fromString . show
43+instance Arbitrary TestHeader where
44+ arbitrary = return ArbitraryHeader
31spec :: Spec
46spec :: Spec
33 describe "mock" $ do
48 describe "mock" $ do
@@ -40,3 +55,28 @@ spec = do
40 get "/" `shouldRespondWith` 200{
55 get "/" `shouldRespondWith` 200{
41 matchBody = Just $ Aeson.encode ArbitraryBody
56 matchBody = Just $ Aeson.encode ArbitraryBody
59+ context "response headers" $ do
60+ let withHeader :: Proxy (Get '[JSON] (Headers '[Header "foo" TestHeader] Body))
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")]
72+ else Just ("headers not correct\n")
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")]
81+ else Just ("headers not correct\n")