Add tests for ResponseHeaders

This commit is contained in:
Timo von Holtz 2015-03-18 16:43:27 +11:00
parent 6738a38818
commit 6774f0275c
3 changed files with 26 additions and 0 deletions

View file

@ -90,7 +90,9 @@ test-suite spec
, aeson
, attoparsec
, bytestring
, http-types
, hspec == 2.*
, HUnit
, QuickCheck
, quickcheck-instances
, parsec

View file

@ -332,6 +332,12 @@ instance MimeUnrender OctetStream BS.ByteString where
class KnownSymbols a where
symbolVals :: Proxy a -> [String]
instance KnownSymbols '[] where
symbolVals _ = []
instance (KnownSymbol x, KnownSymbols xs) => KnownSymbols (x ': xs) where
symbolVals _ = symbolVal (Proxy :: Proxy x) : symbolVals (Proxy :: Proxy xs)
instance (KnownSymbols hs, MimeUnrender ct a)
=> MimeUnrender (ResponseHeaders hs ct) ([H.Header], a) where
fromByteString _ hs body = do

View file

@ -23,8 +23,10 @@ import Data.String.Conversions (cs)
import qualified Data.Text as TextS
import qualified Data.Text.Lazy as TextL
import GHC.Generics
import qualified Network.HTTP.Types as H
import Network.URL (exportParams, importParams)
import Test.Hspec
import Test.HUnit
import Test.QuickCheck
import Test.QuickCheck.Instances ()
@ -165,6 +167,22 @@ spec = describe "Servant.API.ContentTypes" $ do
property $ \x -> toMaybe (eitherDecodeLenient x)
`shouldBe` toMaybe (parseOnly jstring $ cs x)
describe "ResponseHeaders" $ do
it "decoding succeeds and only returns the specified header" $ do
let res = fromByteString (Proxy :: Proxy (ResponseHeaders '["TestHeader"] JSON))
[("Foo", "NOOOOO"), ("TestHeader", "YAY")]
"1"
res `shouldBe` Right ([("TestHeader", "YAY") :: H.Header], (1 :: Int))
it "decoding fails if a required header is not present" $ do
let res = fromByteString (Proxy :: Proxy (ResponseHeaders '["TestHeader"] JSON))
[("Foo", "NOOOOO")]
"1" :: Either String ([H.Header], Int)
case res of
Right _ -> assertFailure "Expected an error"
Left _ -> return ()
data SomeData = SomeData { record1 :: String, record2 :: Int }
deriving (Generic, Eq, Show)