git.iko.soy/typeable/servant
on master help sign in
Update request content-type handling

In case that a sub-server doesn't support the content-type specified in the request invoke `delayedFail` instead of `delayedFailFatal` in order to give the chance to other sub-servers to handle the request.

commit92f8d2314e1a10bca64eef8d0dad7f8feac1dfd4 authorGiovanni Cappellotto <potomak84@gmail.com> dateWed 4 Apr 2018, 18:53 (9 years ago) parent3750f22
2 files changed+12−3
servant-server/src/Servant/Server/Internal.hs+1−1 servant-server/test/Servant/Server/ErrorSpec.hs+11−2
servant-server/src/Servant/Server/Internal.hs+1−1
@@ -603,7 +603,7 @@ instance ( AllCTUnrender list a, HasServer api context, SBoolI (FoldLenient mods
603603 let contentTypeH = fromMaybe "application/octet-stream"
604604 $ lookup hContentType $ requestHeaders request
605605 case canHandleCTypeH (Proxy :: Proxy list) (cs contentTypeH) :: Maybe (BL.ByteString -> Either String a) of
606− Nothing -> delayedFailFatal err415
606+ Nothing -> delayedFail err415
607607 Just f -> return f
608608
609609 -- Body check, we get a body parsing functions as the first argument.
servant-server/test/Servant/Server/ErrorSpec.hs+11−2
@@ -247,6 +247,8 @@ type ErrorChoiceApi
247247 :<|> "path3" :> ReqBody '[JSON] Int :> Post '[PlainText] Int -- 3
248248 :<|> "path4" :> (ReqBody '[PlainText] Int :> Post '[PlainText] Int -- 4
249249 :<|> ReqBody '[PlainText] Int :> Post '[JSON] Int) -- 5
250+ :<|> "path5" :> (ReqBody '[JSON] Int :> Post '[PlainText] Int -- 6
251+ :<|> ReqBody '[PlainText] Int :> Post '[PlainText] Int) -- 7
250252
251253errorChoiceApi :: Proxy ErrorChoiceApi
252254errorChoiceApi = Proxy
@@ -256,8 +258,8 @@ errorChoiceServer = return 0
256258 :<|> return 1
257259 :<|> return 2
258260 :<|> (\_ -> return 3)
259− :<|> (\_ -> return 4)
260− :<|> (\_ -> return 5)
261+ :<|> ((\_ -> return 4) :<|> (\_ -> return 5))
262+ :<|> ((\_ -> return 6) :<|> (\_ -> return 7))
261263
262264
263265errorChoiceSpec :: Spec
@@ -278,6 +280,13 @@ errorChoiceSpec = describe "Multiple handlers return errors"
278280 request methodPost "path4" [(hContentType, "text/plain;charset=utf-8"),
279281 (hAccept, "blah")] "5"
280282 `shouldRespondWith` 406
283+ it "should respond with 415 only if none of the subservers supports the request's content type" $ do
284+ request methodPost "path5" [(hContentType, "text/plain;charset=utf-8")] "1"
285+ `shouldRespondWith` 200
286+ request methodPost "path5" [(hContentType, "application/json")] "1"
287+ `shouldRespondWith` 200
288+ request methodPost "path5" [(hContentType, "application/not-supported")] ""
289+ `shouldRespondWith` 415
281290
282291
283292-- }}}