@@ -39,7 +39,7 @@ import Network.Wai.Test (defaultRequest, request,
39 simpleHeaders, simpleStatus)
39 simpleHeaders, simpleStatus)
40import Servant.API ((:<|>) (..), (:>), AuthProtect,
40import Servant.API ((:<|>) (..), (:>), AuthProtect,
41 BasicAuth, BasicAuthData(BasicAuthData),
41 BasicAuth, BasicAuthData(BasicAuthData),
42− Capture, Delete, Get, Header (..),
42+ Capture, CaptureAll, Delete, Get, Header (..),
43 Headers, HttpVersion,
43 Headers, HttpVersion,
44 IsSecure (..), JSON,
44 IsSecure (..), JSON,
45 NoContent (..), Patch, PlainText,
45 NoContent (..), Patch, PlainText,
@@ -216,6 +216,58 @@ captureSpec = do
216 it "strips the captured path snippet from pathInfo" $ do
216 it "strips the captured path snippet from pathInfo" $ do
217 get "/captured/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
217 get "/captured/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
220+------------------------------------------------------------------------------
221+-- * captureAllSpec {{{
222+------------------------------------------------------------------------------
224+type CaptureAllApi = CaptureAll "legs" Integer :> Get '[JSON] Animal
225+captureAllApi :: Proxy CaptureAllApi
226+captureAllApi = Proxy
227+captureAllServer :: [Integer] -> Handler Animal
228+captureAllServer legs = case sum legs of
231+ 0 -> return beholder
234+captureAllSpec :: Spec
236+ describe "Servant.API.CaptureAll" $ do
237+ with (return (serve captureAllApi captureAllServer)) $ do
239+ it "can capture a single element of the 'pathInfo'" $ do
240+ response <- get "/2"
241+ liftIO $ decode' (simpleBody response) `shouldBe` Just tweety
243+ it "can capture multiple elements of the 'pathInfo'" $ do
244+ response <- get "/2/2"
245+ liftIO $ decode' (simpleBody response) `shouldBe` Just jerry
247+ it "can capture arbitrarily many elements of the 'pathInfo'" $ do
248+ response <- get "/1/1/0/1/0/1"
249+ liftIO $ decode' (simpleBody response) `shouldBe` Just jerry
251+ it "can capture when there are no elements in 'pathInfo'" $ do
253+ liftIO $ decode' (simpleBody response) `shouldBe` Just beholder
255+ it "returns 400 if the decoding fails" $ do
256+ get "/notAnInt" `shouldRespondWith` 400
258+ it "returns 400 if the decoding fails, regardless of which element" $ do
259+ get "/1/0/0/notAnInt/3/" `shouldRespondWith` 400
261+ it "returns 400 if the decoding fails, even when it's multiple elements" $ do
262+ get "/1/0/0/notAnInt/3/orange/" `shouldRespondWith` 400
265+ (Proxy :: Proxy (CaptureAll "segments" String :> Raw))
266+ (\ _captured request_ respond ->
267+ respond $ responseLBS ok200 [] (cs $ show $ pathInfo request_)))) $ do
268+ it "consumes everything from pathInfo" $ do
269+ get "/captured/foo/bar/baz" `shouldRespondWith` (fromString (show ([] :: [Int])))
220------------------------------------------------------------------------------
272------------------------------------------------------------------------------
221-- * queryParamSpec {{{
273-- * queryParamSpec {{{
@@ -644,4 +696,7 @@ jerry = Animal "Mouse" 4
645tweety :: Animal
697tweety :: Animal
646tweety = Animal "Bird" 2
698tweety = Animal "Bird" 2
701+beholder = Animal "Beholder" 0