@@ -39,7 +39,7 @@ import Network.Wai.Test (defaultRequest, request,
3939 simpleHeaders, simpleStatus)
4040import Servant.API ((:<|>) (..), (:>), AuthProtect,
4141 BasicAuth, BasicAuthData(BasicAuthData),
42− Capture, Delete, Get, Header (..),
42+ Capture, CaptureAll, Delete, Get, Header (..),
4343 Headers, HttpVersion,
4444 IsSecure (..), JSON,
4545 NoContent (..), Patch, PlainText,
@@ -216,6 +216,58 @@ captureSpec = do
216216 it "strips the captured path snippet from pathInfo" $ do
217217 get "/captured/foo" `shouldRespondWith` (fromString (show ["foo" :: String]))
218218
219+-- }}}
220+------------------------------------------------------------------------------
221+-- * captureAllSpec {{{
222+------------------------------------------------------------------------------
223+
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
229+ 4 -> return jerry
230+ 2 -> return tweety
231+ 0 -> return beholder
232+ _ -> throwE err404
233+
234+captureAllSpec :: Spec
235+captureAllSpec = do
236+ describe "Servant.API.CaptureAll" $ do
237+ with (return (serve captureAllApi captureAllServer)) $ do
238+
239+ it "can capture a single element of the 'pathInfo'" $ do
240+ response <- get "/2"
241+ liftIO $ decode' (simpleBody response) `shouldBe` Just tweety
242+
243+ it "can capture multiple elements of the 'pathInfo'" $ do
244+ response <- get "/2/2"
245+ liftIO $ decode' (simpleBody response) `shouldBe` Just jerry
246+
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
250+
251+ it "can capture when there are no elements in 'pathInfo'" $ do
252+ response <- get "/"
253+ liftIO $ decode' (simpleBody response) `shouldBe` Just beholder
254+
255+ it "returns 400 if the decoding fails" $ do
256+ get "/notAnInt" `shouldRespondWith` 400
257+
258+ it "returns 400 if the decoding fails, regardless of which element" $ do
259+ get "/1/0/0/notAnInt/3/" `shouldRespondWith` 400
260+
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
263+
264+ with (return (serve
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])))
270+
219271-- }}}
220272------------------------------------------------------------------------------
221273-- * queryParamSpec {{{
@@ -644,4 +696,7 @@ jerry = Animal "Mouse" 4
644696
645697tweety :: Animal
646698tweety = Animal "Bird" 2
699+
700+beholder :: Animal
701+beholder = Animal "Beholder" 0
647702-- }}}