test suite: Capture
This commit is contained in:
parent
b749f7fbc6
commit
c08ca320a9
2 changed files with 36 additions and 0 deletions
|
@ -5,6 +5,7 @@ module Servant.Text where
|
||||||
|
|
||||||
import Data.String.Conversions
|
import Data.String.Conversions
|
||||||
import Data.Text
|
import Data.Text
|
||||||
|
import Text.Read
|
||||||
|
|
||||||
class FromText a where
|
class FromText a where
|
||||||
fromText :: Text -> Maybe a
|
fromText :: Text -> Maybe a
|
||||||
|
@ -12,18 +13,21 @@ class FromText a where
|
||||||
class ToText a where
|
class ToText a where
|
||||||
toText :: a -> Text
|
toText :: a -> Text
|
||||||
|
|
||||||
|
|
||||||
instance FromText Text where
|
instance FromText Text where
|
||||||
fromText = Just
|
fromText = Just
|
||||||
|
|
||||||
instance ToText Text where
|
instance ToText Text where
|
||||||
toText = id
|
toText = id
|
||||||
|
|
||||||
|
|
||||||
instance FromText String where
|
instance FromText String where
|
||||||
fromText = Just . cs
|
fromText = Just . cs
|
||||||
|
|
||||||
instance ToText String where
|
instance ToText String where
|
||||||
toText = cs
|
toText = cs
|
||||||
|
|
||||||
|
|
||||||
instance FromText Bool where
|
instance FromText Bool where
|
||||||
fromText "true" = Just True
|
fromText "true" = Just True
|
||||||
fromText "false" = Just False
|
fromText "false" = Just False
|
||||||
|
@ -32,3 +36,10 @@ instance FromText Bool where
|
||||||
instance ToText Bool where
|
instance ToText Bool where
|
||||||
toText True = "true"
|
toText True = "true"
|
||||||
toText False = "false"
|
toText False = "false"
|
||||||
|
|
||||||
|
|
||||||
|
instance FromText Integer where
|
||||||
|
fromText = readMaybe . cs
|
||||||
|
|
||||||
|
instance ToText Integer where
|
||||||
|
toText = cs . show
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
module Servant.ServerSpec where
|
module Servant.ServerSpec where
|
||||||
|
|
||||||
|
|
||||||
|
import Control.Monad.Trans.Either
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
import Data.String.Conversions
|
import Data.String.Conversions
|
||||||
|
@ -18,6 +19,7 @@ import Network.Wai.Test
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Test.Hspec.Wai
|
import Test.Hspec.Wai
|
||||||
|
|
||||||
|
import Servant.API.Capture
|
||||||
import Servant.API.Get
|
import Servant.API.Get
|
||||||
import Servant.API.GetParam
|
import Servant.API.GetParam
|
||||||
import Servant.API.Post
|
import Servant.API.Post
|
||||||
|
@ -54,11 +56,15 @@ instance FromJSON Animal
|
||||||
jerry :: Animal
|
jerry :: Animal
|
||||||
jerry = Animal "Mouse" 4
|
jerry = Animal "Mouse" 4
|
||||||
|
|
||||||
|
tweety :: Animal
|
||||||
|
tweety = Animal "Bird" 2
|
||||||
|
|
||||||
|
|
||||||
-- * specs
|
-- * specs
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
spec = do
|
spec = do
|
||||||
|
captureSpec
|
||||||
getSpec
|
getSpec
|
||||||
getParamSpec
|
getParamSpec
|
||||||
postSpec
|
postSpec
|
||||||
|
@ -66,6 +72,25 @@ spec = do
|
||||||
unionSpec
|
unionSpec
|
||||||
|
|
||||||
|
|
||||||
|
type CaptureApi = Capture "legs" Integer :> Get Animal
|
||||||
|
captureApi :: Proxy CaptureApi
|
||||||
|
captureApi = Proxy
|
||||||
|
captureServer :: Integer -> EitherT (Int, String) IO Animal
|
||||||
|
captureServer legs = case legs of
|
||||||
|
4 -> return jerry
|
||||||
|
2 -> return tweety
|
||||||
|
_ -> left (404, "not found")
|
||||||
|
|
||||||
|
captureSpec :: Spec
|
||||||
|
captureSpec = do
|
||||||
|
describe "Servant.API.Capture" $ do
|
||||||
|
with (return (serve captureApi captureServer)) $ do
|
||||||
|
it "can capture parts of the 'pathInfo'" $ do
|
||||||
|
response <- get "/2"
|
||||||
|
liftIO $ do
|
||||||
|
decode' (simpleBody response) `shouldBe` Just tweety
|
||||||
|
|
||||||
|
|
||||||
type GetApi = Get Person
|
type GetApi = Get Person
|
||||||
getApi :: Proxy GetApi
|
getApi :: Proxy GetApi
|
||||||
getApi = Proxy
|
getApi = Proxy
|
||||||
|
|
Loading…
Add table
Reference in a new issue