@@ -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