@@ -51,10 +51,11 @@ import Servant.API
51
51
BasicAuthData (BasicAuthData ), Capture , Capture' , CaptureAll ,
52
52
Delete , EmptyAPI , Fragment , Get , HasStatus (StatusOf ), Header ,
53
53
Headers , HttpVersion , IsSecure (.. ), JSON , Lenient ,
54
- NoContent (.. ), NoContentVerb , NoFraming , OctetStream , Patch ,
55
- PlainText , Post , Put , QueryFlag , QueryParam , QueryParams , Raw ,
56
- RemoteHost , ReqBody , SourceIO , StdMethod (.. ), Stream , Strict ,
57
- UVerb , Union , Verb , WithStatus (.. ), addHeader )
54
+ NoContent (.. ), NoContentVerb , NoFraming , OctetStream ,
55
+ Optional , Patch , PlainText , Post , Put , QueryFlag , QueryParam ,
56
+ QueryParams , Raw , RemoteHost , ReqBody , ReqBody' , SourceIO ,
57
+ StdMethod (.. ), Stream , Strict , UVerb , Union , Verb ,
58
+ WithStatus (.. ), addHeader )
58
59
import Servant.Server
59
60
(Context ((:.) , EmptyContext ), Handler , Server , Tagged (.. ),
60
61
emptyServer , err401 , err403 , err404 , respond , serve ,
@@ -501,6 +502,7 @@ fragmentSpec = do
501
502
------------------------------------------------------------------------------
502
503
type ReqBodyApi = ReqBody '[JSON ] Person :> Post '[JSON ] Person
503
504
:<|> " blah" :> ReqBody '[JSON ] Person :> Put '[JSON ] Integer
505
+ :<|> " meh" :> ReqBody' '[Optional , Strict ] '[JSON ] Person :> Put '[JSON ] Integer
504
506
505
507
reqBodyApi :: Proxy ReqBodyApi
506
508
reqBodyApi = Proxy
@@ -509,7 +511,7 @@ reqBodySpec :: Spec
509
511
reqBodySpec = describe " Servant.API.ReqBody" $ do
510
512
511
513
let server :: Server ReqBodyApi
512
- server = return :<|> return . age
514
+ server = return :<|> return . age :<|> return . maybe 0 age
513
515
mkReq method x = THW. request method x
514
516
[(hContentType, " application/json;charset=utf-8" )]
515
517
@@ -526,6 +528,31 @@ reqBodySpec = describe "Servant.API.ReqBody" $ do
526
528
THW. request methodPost " /"
527
529
[(hContentType, " application/nonsense" )] " " `shouldRespondWith` 415
528
530
531
+ describe " optional request body" $ do
532
+ it " request without body succeeds" $ do
533
+ THW. request methodPut " /meh" [] mempty `shouldRespondWith` 200
534
+
535
+ it " request without body responds with proper default value" $ do
536
+ response <- THW. request methodPut " /meh" [] mempty
537
+ liftIO $ simpleBody response `shouldBe` encode (0 :: Integer )
538
+
539
+ it " responds with 415 if the request body media type is unsupported" $ do
540
+ THW. request methodPut " /meh" [(hContentType, " application/nonsense" )]
541
+ (encode alice) `shouldRespondWith` 415
542
+ THW. request methodPut " /meh" [(hContentType, " application/octet-stream" )]
543
+ (encode alice) `shouldRespondWith` 415
544
+
545
+ it " request without body and with content-type header succeeds" $ do
546
+ mkReq methodPut " /meh" mempty `shouldRespondWith` 200
547
+
548
+ it " request without body and with content-type header returns default value" $ do
549
+ response <- mkReq methodPut " /meh" mempty
550
+ liftIO $ simpleBody response `shouldBe` encode (0 :: Integer )
551
+
552
+ it " optional request body can be provided" $ do
553
+ response <- mkReq methodPut " /meh" (encode alice)
554
+ liftIO $ simpleBody response `shouldBe` encode (age alice)
555
+
529
556
-- }}}
530
557
------------------------------------------------------------------------------
531
558
-- * headerSpec {{{
0 commit comments