@@ -51,9 +51,10 @@ import Servant.API
51
51
BasicAuthData (BasicAuthData ), Capture , Capture' , CaptureAll ,
52
52
Delete , EmptyAPI , Get , Header , Headers , HttpVersion ,
53
53
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 )
57
58
import Servant.Server
58
59
(Context ((:.) , EmptyContext ), Handler , Server , Tagged (.. ),
59
60
emptyServer , err401 , err403 , err404 , serve , serveWithContext )
@@ -465,6 +466,7 @@ queryParamSpec = do
465
466
------------------------------------------------------------------------------
466
467
type ReqBodyApi = ReqBody '[JSON ] Person :> Post '[JSON ] Person
467
468
:<|> " blah" :> ReqBody '[JSON ] Person :> Put '[JSON ] Integer
469
+ :<|> " meh" :> ReqBody' '[Optional , Strict ] '[JSON ] Person :> Put '[JSON ] Integer
468
470
469
471
reqBodyApi :: Proxy ReqBodyApi
470
472
reqBodyApi = Proxy
@@ -473,7 +475,7 @@ reqBodySpec :: Spec
473
475
reqBodySpec = describe " Servant.API.ReqBody" $ do
474
476
475
477
let server :: Server ReqBodyApi
476
- server = return :<|> return . age
478
+ server = return :<|> return . age :<|> return . maybe 0 age
477
479
mkReq method x = THW. request method x
478
480
[(hContentType, " application/json;charset=utf-8" )]
479
481
@@ -490,6 +492,31 @@ reqBodySpec = describe "Servant.API.ReqBody" $ do
490
492
THW. request methodPost " /"
491
493
[(hContentType, " application/nonsense" )] " " `shouldRespondWith` 415
492
494
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
+
493
520
-- }}}
494
521
------------------------------------------------------------------------------
495
522
-- * headerSpec {{{
0 commit comments