@@ -51,9 +51,10 @@ import Servant.API
5151 BasicAuthData (BasicAuthData ), Capture , Capture' , CaptureAll ,
5252 Delete , EmptyAPI , Get , Header , Headers , HttpVersion ,
5353 IsSecure (.. ), JSON , Lenient , NoContent (.. ), NoContentVerb ,
54- NoFraming , OctetStream , Patch , PlainText , Post , Put ,
55- QueryFlag , QueryParam , QueryParams , Raw , RemoteHost , ReqBody ,
56- SourceIO , StdMethod (.. ), Stream , Strict , Verb , addHeader )
54+ NoFraming , OctetStream , Optional , Patch , PlainText , Post , Put ,
55+ QueryFlag , QueryParam , QueryParams , Raw , RemoteHost ,
56+ ReqBody , ReqBody' , SourceIO , StdMethod (.. ), Stream , Strict ,
57+ Verb , addHeader )
5758import Servant.Server
5859 (Context ((:.) , EmptyContext ), Handler , Server , Tagged (.. ),
5960 emptyServer , err401 , err403 , err404 , serve , serveWithContext )
@@ -465,6 +466,7 @@ queryParamSpec = do
465466------------------------------------------------------------------------------
466467type ReqBodyApi = ReqBody '[JSON ] Person :> Post '[JSON ] Person
467468 :<|> " blah" :> ReqBody '[JSON ] Person :> Put '[JSON ] Integer
469+ :<|> " meh" :> ReqBody' '[Optional , Strict ] '[JSON ] Person :> Put '[JSON ] Integer
468470
469471reqBodyApi :: Proxy ReqBodyApi
470472reqBodyApi = Proxy
@@ -473,7 +475,7 @@ reqBodySpec :: Spec
473475reqBodySpec = describe " Servant.API.ReqBody" $ do
474476
475477 let server :: Server ReqBodyApi
476- server = return :<|> return . age
478+ server = return :<|> return . age :<|> return . maybe 0 age
477479 mkReq method x = THW. request method x
478480 [(hContentType, " application/json;charset=utf-8" )]
479481
@@ -490,6 +492,31 @@ reqBodySpec = describe "Servant.API.ReqBody" $ do
490492 THW. request methodPost " /"
491493 [(hContentType, " application/nonsense" )] " " `shouldRespondWith` 415
492494
495+ describe " optional request body" $ do
496+ it " request without body succeeds" $ do
497+ THW. request methodPut " /meh" [] mempty `shouldRespondWith` 200
498+
499+ it " request without body responds with proper default value" $ do
500+ response <- THW. request methodPut " /meh" [] mempty
501+ liftIO $ simpleBody response `shouldBe` encode (0 :: Integer )
502+
503+ it " responds with 415 if the request body media type is unsupported" $ do
504+ THW. request methodPut " /meh" [(hContentType, " application/nonsense" )]
505+ (encode alice) `shouldRespondWith` 415
506+ THW. request methodPut " /meh" [(hContentType, " application/octet-stream" )]
507+ (encode alice) `shouldRespondWith` 415
508+
509+ it " request without body and with content-type header succeeds" $ do
510+ mkReq methodPut " /meh" mempty `shouldRespondWith` 200
511+
512+ it " request without body and with content-type header returns default value" $ do
513+ response <- mkReq methodPut " /meh" mempty
514+ liftIO $ simpleBody response `shouldBe` encode (0 :: Integer )
515+
516+ it " optional request body can be provided" $ do
517+ response <- mkReq methodPut " /meh" (encode alice)
518+ liftIO $ simpleBody response `shouldBe` encode (age alice)
519+
493520-- }}}
494521------------------------------------------------------------------------------
495522-- * headerSpec {{{
0 commit comments