git.iko.soy/typeable/servant
on master help sign in
test suite: Capture
commitc08ca320a9262763158cc3f1076f55a894db6cc5 authorSönke Hahn <soenke.hahn@zalora.com> dateTue 28 Oct 2014, 14:00 (12 years ago) parentb749f7f
2 files changed+36
src/Servant/Text.hs+11 test/Servant/ServerSpec.hs+25
src/Servant/Text.hs+11
@@ -5,6 +5,7 @@ module Servant.Text where
55
66import Data.String.Conversions
77import Data.Text
8+import Text.Read
89
910class FromText a where
1011 fromText :: Text -> Maybe a
@@ -12,18 +13,21 @@ class FromText a where
1213class ToText a where
1314 toText :: a -> Text
1415
16+
1517instance FromText Text where
1618 fromText = Just
1719
1820instance ToText Text where
1921 toText = id
2022
23+
2124instance FromText String where
2225 fromText = Just . cs
2326
2427instance ToText String where
2528 toText = cs
2629
30+
2731instance FromText Bool where
2832 fromText "true" = Just True
2933 fromText "false" = Just False
@@ -32,3 +36,10 @@ instance FromText Bool where
3236instance ToText Bool where
3337 toText True = "true"
3438 toText False = "false"
39+
40+
41+instance FromText Integer where
42+ fromText = readMaybe . cs
43+
44+instance ToText Integer where
45+ toText = cs . show
test/Servant/ServerSpec.hs+25
@@ -8,6 +8,7 @@
88module Servant.ServerSpec where
99
1010
11+import Control.Monad.Trans.Either
1112import Data.Aeson
1213import Data.Proxy
1314import Data.String.Conversions
@@ -18,6 +19,7 @@ import Network.Wai.Test
1819import Test.Hspec
1920import Test.Hspec.Wai
2021
22+import Servant.API.Capture
2123import Servant.API.Get
2224import Servant.API.GetParam
2325import Servant.API.Post
@@ -54,11 +56,15 @@ instance FromJSON Animal
5456jerry :: Animal
5557jerry = Animal "Mouse" 4
5658
59+tweety :: Animal
60+tweety = Animal "Bird" 2
61+
5762
5863-- * specs
5964
6065spec :: Spec
6166spec = do
67+ captureSpec
6268 getSpec
6369 getParamSpec
6470 postSpec
@@ -66,6 +72,25 @@ spec = do
6672 unionSpec
6773
6874
75+type CaptureApi = Capture "legs" Integer :> Get Animal
76+captureApi :: Proxy CaptureApi
77+captureApi = Proxy
78+captureServer :: Integer -> EitherT (Int, String) IO Animal
79+captureServer legs = case legs of
80+ 4 -> return jerry
81+ 2 -> return tweety
82+ _ -> left (404, "not found")
83+
84+captureSpec :: Spec
85+captureSpec = do
86+ describe "Servant.API.Capture" $ do
87+ with (return (serve captureApi captureServer)) $ do
88+ it "can capture parts of the 'pathInfo'" $ do
89+ response <- get "/2"
90+ liftIO $ do
91+ decode' (simpleBody response) `shouldBe` Just tweety
92+
93+
6994type GetApi = Get Person
7095getApi :: Proxy GetApi
7196getApi = Proxy