cassie
cassie

Reputation: 11

Haskell spec post doesn't work

I am currently writing a Haskell testing code, but the post part doesn't work.

    module Main where

      import           MyLib (app)
      import           Control.Monad       (mzero)
      import           Data.Aeson
      import           Test.Hspec
      import           Test.Hspec.Wai
      import           Data.Semigroup ((<>))

      --import           Test.Hspec.Wai.JSON
      --import           UseHaskellAPI
      data ResponseMsg = ResponseMsg { name    :: String
                                     , message :: String
                                     } deriving (Eq, Show)
      instance FromJSON ResponseMsg where
       parseJSON (Object o) =
         ResponseMsg <$> o .: "name"
                     <*> o .: "message"
       parseJSON _ = mzero

      instance ToJSON ResponseMsg where
        -- this generates a Value
        toJSON (ResponseMsg n m) =
            object ["name" .= n, "message" .= m]

        -- this encodes directly to a bytestring Builder
        toEncoding (ResponseMsg n m) =
            pairs ("name" .= n <> "message" .= m)


      main :: IO ()
      main = do
        --
        putStrLn $ show $ toJSON $ ResponseMsg "ecky" "hello"
        hspec spec

      spec :: Spec
      spec = with (return app) $ do
        --test case for sample storage message
        describe "POST /storeMessage true" $ do
          it "responds with storeMessage" $ do
            (post "/storeMessage" $ encode $ toJSON $ ResponseMsg "ecky" "hello") `shouldRespondWith` "true%" {matchHeaders = ["Content-Type" <:> "application/json"]}

The console output like this

test/Main.hs:48: 1) POST /storeMessage true responds with storeMessage status mismatch: expected: 200 but got: 415 missing header: Content-Type: application/json the actual headers were: body mismatch: expected: "true%" but got: ""

This is the data type

    data Message = Message { name    :: String
                           , message :: String
                           } deriving (Show, Generic, FromJSON, ToJSON, ToBSON, FromBSON)

    deriving instance FromBSON String  -- we need these as BSON does not provide
    deriving instance ToBSON   String

and this is server api

        storeMessage :: Message -> Handler Bool
        storeMessage msg@(Message key _) = liftIO $ do
          warnLog $ "Storing message under key " ++ key ++ "."
          withMongoDbConnection $ upsert (select ["name" =: key] "MESSAGE_RECORD") $ toBSON msg

          return True 

does any one know the problem? Thanks in advance

Upvotes: 1

Views: 192

Answers (1)

dgiugg
dgiugg

Reputation: 1334

This is quite an old message, but for those who would be looking for an answer today, here it is, I hope.

I don't know what the default content-type header is, but here it is clearly not "application/json", so you have to specify it because that's what you are sending. So use request instead:

request methodPost "/storeMessage" [(hApplicationContent, "application/json" :: ByteString)] $ encode $ toJSON $ ResponseMsg "ecky" "hello"

See doc Test.Hspec.Wai and headers

Upvotes: 2

Related Questions