Skip to content

Commit 2e5fe27

Browse files
committed
Fix runRouterEnv for CaptureAllRouter
* Trailing slashes will no longer affect the captures for "rooted" CaptureAll apis (test included).
1 parent cb821c5 commit 2e5fe27

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

servant-server/src/Servant/Server/Internal/Router.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,8 @@ runRouterEnv fmt router env request respond =
179179
in runRouterEnv fmt router' (first, env) request' respond
180180
CaptureAllRouter router' ->
181181
let segments = case pathInfo request of
182-
-- This case handles empty capture alls in a sub route like:
183-
-- /legs/ => [] instead of [""]
184-
-- But will this break a rooted capture all? like:
185-
-- // => [] instead of [""]
186-
-- Maybe we should fix it in Wai first.
187-
[""] -> []
182+
-- this case is to handle trailing slashes.
183+
("":xs) -> xs
188184
xs -> xs
189185
request' = request { pathInfo = [] }
190186
in runRouterEnv fmt router' (segments, env) request' respond

servant-server/test/Servant/ServerSpec.hs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,12 @@ captureAllServer = handleLegs :<|> return
281281
2 -> return tweety
282282
_ -> throwError err404
283283

284+
type RootedCaptureAllApi = CaptureAll "xs" String :> Get '[JSON] [String]
285+
284286
captureAllSpec :: Spec
285287
captureAllSpec = do
288+
let getStringList = decode' @[String] . simpleBody
289+
286290
describe "Servant.API.CaptureAll" $ do
287291
with (return (serve captureAllApi captureAllServer)) $ do
288292

@@ -311,8 +315,6 @@ captureAllSpec = do
311315
it "returns 400 if the decoding fails, even when it's multiple elements" $ do
312316
get "/legs/1/0/0/notAnInt/3/orange/" `shouldRespondWith` 400
313317

314-
let getStringList = decode' @[String] . simpleBody
315-
316318
it "can capture single String" $ do
317319
response <- get "/arms/jerry"
318320
liftIO $ getStringList response `shouldBe` Just ["jerry"]
@@ -321,6 +323,19 @@ captureAllSpec = do
321323
response <- get "/arms/"
322324
liftIO $ getStringList response `shouldBe` Just []
323325

326+
it "can capture empty string from captureall" $ do
327+
response <- get "/arms//"
328+
liftIO $ getStringList response `shouldBe` Just [""]
329+
330+
with (return (serve (Proxy :: Proxy RootedCaptureAllApi) return)) $ do
331+
it "can capture empty rooted capture all" $ do
332+
response <- get "/"
333+
liftIO $ getStringList response `shouldBe` Just []
334+
335+
it "can capture empty string from rooted capture all" $ do
336+
response <- get "//"
337+
liftIO $ getStringList response `shouldBe` Just [""]
338+
324339
with (return (serve
325340
(Proxy :: Proxy (CaptureAll "segments" String :> Raw))
326341
(\ _captured -> Tagged $ \request_ sendResponse ->

0 commit comments

Comments
 (0)