git.iko.soy/typeable/servant
on master help sign in
test suite: Capture
c08ca32Sönke Hahn
src/Servant/Text.hs+11
@@ -5,6 +5,7 @@ module Servant.Text where
5
5
6import Data.String.Conversions
6import Data.String.Conversions
7import Data.Text
7import Data.Text
8+import Text.Read
8
9
9class FromText a where
10class FromText a where
10 fromText :: Text -> Maybe a
11 fromText :: Text -> Maybe a
@@ -12,18 +13,21 @@ class FromText a where
12class ToText a where
13class ToText a where
13 toText :: a -> Text
14 toText :: a -> Text
14
15
16+
15instance FromText Text where
17instance FromText Text where
16 fromText = Just
18 fromText = Just
17
19
18instance ToText Text where
20instance ToText Text where
19 toText = id
21 toText = id
20
22
23+
21instance FromText String where
24instance FromText String where
22 fromText = Just . cs
25 fromText = Just . cs
23
26
24instance ToText String where
27instance ToText String where
25 toText = cs
28 toText = cs
26
29
30+
27instance FromText Bool where
31instance FromText Bool where
28 fromText "true" = Just True
32 fromText "true" = Just True
29 fromText "false" = Just False
33 fromText "false" = Just False
@@ -32,3 +36,10 @@ instance FromText Bool where
32instance ToText Bool where
36instance ToText Bool where
33 toText True = "true"
37 toText True = "true"
34 toText False = "false"
38 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 @@
8module Servant.ServerSpec where
8module Servant.ServerSpec where
9
9
10
10
11+import Control.Monad.Trans.Either
11import Data.Aeson
12import Data.Aeson
12import Data.Proxy
13import Data.Proxy
13import Data.String.Conversions
14import Data.String.Conversions
@@ -18,6 +19,7 @@ import Network.Wai.Test
18import Test.Hspec
19import Test.Hspec
19import Test.Hspec.Wai
20import Test.Hspec.Wai
20
21
22+import Servant.API.Capture
21import Servant.API.Get
23import Servant.API.Get
22import Servant.API.GetParam
24import Servant.API.GetParam
23import Servant.API.Post
25import Servant.API.Post
@@ -54,11 +56,15 @@ instance FromJSON Animal
54jerry :: Animal
56jerry :: Animal
55jerry = Animal "Mouse" 4
57jerry = Animal "Mouse" 4
56
58
59+tweety :: Animal
60+tweety = Animal "Bird" 2
61+
57
62
58-- * specs
63-- * specs
59
64
60spec :: Spec
65spec :: Spec
61spec = do
66spec = do
67+ captureSpec
62 getSpec
68 getSpec
63 getParamSpec
69 getParamSpec
64 postSpec
70 postSpec
@@ -66,6 +72,25 @@ spec = do
66 unionSpec
72 unionSpec
67
73
68
74
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+
69type GetApi = Get Person
94type GetApi = Get Person
70getApi :: Proxy GetApi
95getApi :: Proxy GetApi
71getApi = Proxy
96getApi = Proxy