Skip to content

Commit a8cd6e3

Browse files
authored
Merge pull request #937 from potomak/subserver-content-types
Update request content-type handling
2 parents 3750f22 + 92f8d23 commit a8cd6e3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

251253
errorChoiceApi :: Proxy ErrorChoiceApi
252254
errorChoiceApi = 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

263265
errorChoiceSpec :: 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
-- }}}

0 commit comments

Comments
 (0)