From 207f39857222cb953b4a09d66df61c90f1452a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Hahn?= Date: Mon, 27 Oct 2014 16:24:56 +0800 Subject: [PATCH] test suite: GetParam --- src/Servant/Text.hs | 11 ++++++++++- test/Servant/ServerSpec.hs | 30 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Servant/Text.hs b/src/Servant/Text.hs index 26efdc16..dca76dcf 100644 --- a/src/Servant/Text.hs +++ b/src/Servant/Text.hs @@ -1,6 +1,9 @@ +{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeSynonymInstances #-} module Servant.Text where +import Data.String.Conversions import Data.Text class FromText a where @@ -15,6 +18,12 @@ instance FromText Text where instance ToText Text where toText = id +instance FromText String where + fromText = Just . cs + +instance ToText String where + toText = cs + instance FromText Bool where fromText "true" = Just True fromText "false" = Just False @@ -22,4 +31,4 @@ instance FromText Bool where instance ToText Bool where toText True = "true" - toText False = "false" \ No newline at end of file + toText False = "false" diff --git a/test/Servant/ServerSpec.hs b/test/Servant/ServerSpec.hs index c9d7f3ff..87ef2aac 100644 --- a/test/Servant/ServerSpec.hs +++ b/test/Servant/ServerSpec.hs @@ -11,11 +11,14 @@ module Servant.ServerSpec where import Data.Aeson import Data.Proxy import GHC.Generics +import Network.HTTP.Types +import Network.Wai import Network.Wai.Test import Test.Hspec -import Test.Hspec.Wai as Wai +import Test.Hspec.Wai import Servant.API.Get +import Servant.API.GetParam import Servant.API.Post import Servant.API.RQBody import Servant.API.Sub @@ -55,6 +58,7 @@ jerry = Animal "Mouse" 4 spec :: Spec spec = do getSpec + getParamSpec postSpec unionSpec @@ -76,6 +80,30 @@ getSpec :: Spec = do post "/" "" `shouldRespondWith` 404 +type GetParamApi = GetParam "name" String :> Get Person +getParamApi :: Proxy GetParamApi +getParamApi = Proxy + +getParamServer :: Server GetParamApi +getParamServer (Just name) = return alice{name = name} +getParamServer Nothing = return alice + +getParamSpec :: Spec +getParamSpec = do + describe "Servant.API.GetParam" $ do + it "allows to retrieve GET parameters" $ do + (flip runSession) (serve getParamApi getParamServer) $ do + let params = "?name=bob" + response <- Network.Wai.Test.request defaultRequest{ + rawQueryString = params, + queryString = parseQuery params + } + liftIO $ do + decode' (simpleBody response) `shouldBe` Just alice{ + name = "bob" + } + + type PostApi = RQBody Person :> (Post Integer) postApi :: Proxy PostApi postApi = Proxy