@@ -282,45 +282,73 @@ captureSpec = do
282
282
-- * captureAllSpec {{{
283
283
------------------------------------------------------------------------------
284
284
285
- type CaptureAllApi = CaptureAll " legs" Integer :> Get '[JSON ] Animal
285
+ type CaptureAllApi = " legs" :> CaptureAll " legs" Integer :> Get '[JSON ] Animal
286
+ :<|> " arms" :> CaptureAll " arms" String :> Get '[JSON ] [String ]
286
287
captureAllApi :: Proxy CaptureAllApi
287
288
captureAllApi = Proxy
288
- captureAllServer :: [Integer ] -> Handler Animal
289
- captureAllServer legs = case sum legs of
290
- 4 -> return jerry
291
- 2 -> return tweety
292
- 0 -> return beholder
293
- _ -> throwError err404
289
+ captureAllServer :: Server CaptureAllApi
290
+ captureAllServer = handleLegs :<|> return
291
+ where
292
+ handleLegs [] = return beholder
293
+ handleLegs legs = case sum legs of
294
+ 4 -> return jerry
295
+ 2 -> return tweety
296
+ _ -> throwError err404
297
+
298
+ type RootedCaptureAllApi = CaptureAll " xs" String :> Get '[JSON ] [String ]
294
299
295
300
captureAllSpec :: Spec
296
301
captureAllSpec = do
302
+ let getStringList = decode' @ [String ] . simpleBody
303
+
297
304
describe " Servant.API.CaptureAll" $ do
298
305
with (return (serve captureAllApi captureAllServer)) $ do
299
306
300
307
it " can capture a single element of the 'pathInfo'" $ do
301
- response <- get " /2"
308
+ response <- get " /legs/ 2"
302
309
liftIO $ decode' (simpleBody response) `shouldBe` Just tweety
303
310
304
311
it " can capture multiple elements of the 'pathInfo'" $ do
305
- response <- get " /2/2"
312
+ response <- get " /legs/ 2/2"
306
313
liftIO $ decode' (simpleBody response) `shouldBe` Just jerry
307
314
308
315
it " can capture arbitrarily many elements of the 'pathInfo'" $ do
309
- response <- get " /1/1/0/1/0/1"
316
+ response <- get " /legs/ 1/1/0/1/0/1"
310
317
liftIO $ decode' (simpleBody response) `shouldBe` Just jerry
311
318
312
319
it " can capture when there are no elements in 'pathInfo'" $ do
313
- response <- get " /"
320
+ response <- get " /legs/ "
314
321
liftIO $ decode' (simpleBody response) `shouldBe` Just beholder
315
322
316
323
it " returns 400 if the decoding fails" $ do
317
- get " /notAnInt" `shouldRespondWith` 400
324
+ get " /legs/ notAnInt" `shouldRespondWith` 400
318
325
319
326
it " returns 400 if the decoding fails, regardless of which element" $ do
320
- get " /1/0/0/notAnInt/3/" `shouldRespondWith` 400
327
+ get " /legs/ 1/0/0/notAnInt/3/" `shouldRespondWith` 400
321
328
322
329
it " returns 400 if the decoding fails, even when it's multiple elements" $ do
323
- get " /1/0/0/notAnInt/3/orange/" `shouldRespondWith` 400
330
+ get " /legs/1/0/0/notAnInt/3/orange/" `shouldRespondWith` 400
331
+
332
+ it " can capture single String" $ do
333
+ response <- get " /arms/jerry"
334
+ liftIO $ getStringList response `shouldBe` Just [" jerry" ]
335
+
336
+ it " can capture when there are no elements in 'pathinfo'" $ do
337
+ response <- get " /arms/"
338
+ liftIO $ getStringList response `shouldBe` Just []
339
+
340
+ it " can capture empty string from captureall" $ do
341
+ response <- get " /arms//"
342
+ liftIO $ getStringList response `shouldBe` Just [" " ]
343
+
344
+ with (return (serve (Proxy :: Proxy RootedCaptureAllApi ) return )) $ do
345
+ it " can capture empty rooted capture all" $ do
346
+ response <- get " /"
347
+ liftIO $ getStringList response `shouldBe` Just []
348
+
349
+ it " can capture empty string from rooted capture all" $ do
350
+ response <- get " //"
351
+ liftIO $ getStringList response `shouldBe` Just [" " ]
324
352
325
353
with (return (serve
326
354
(Proxy :: Proxy (CaptureAll " segments" String :> Raw ))
0 commit comments