2015-09-09 23:17:17 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2016-02-17 19:56:15 +01:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2015-09-09 23:17:17 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2015-09-09 22:29:52 +02:00
|
|
|
module Servant.Server.ErrorSpec (spec) where
|
|
|
|
|
2018-06-29 21:08:26 +02:00
|
|
|
import Control.Monad
|
|
|
|
(when)
|
|
|
|
import Data.Aeson
|
|
|
|
(encode)
|
2015-09-15 11:37:17 +02:00
|
|
|
import qualified Data.ByteString.Char8 as BC
|
2015-09-16 22:07:55 +02:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BCL
|
2018-06-29 21:08:26 +02:00
|
|
|
import Data.Monoid
|
|
|
|
((<>))
|
2015-09-09 23:17:17 +02:00
|
|
|
import Data.Proxy
|
2018-06-29 21:08:26 +02:00
|
|
|
import Network.HTTP.Types
|
|
|
|
(hAccept, hAuthorization, hContentType, methodGet, methodPost,
|
|
|
|
methodPut)
|
|
|
|
import Safe
|
|
|
|
(readMay)
|
2015-09-09 23:17:17 +02:00
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.Wai
|
2015-09-09 22:29:52 +02:00
|
|
|
|
2015-09-09 23:17:17 +02:00
|
|
|
import Servant
|
2015-09-09 22:29:52 +02:00
|
|
|
|
|
|
|
spec :: Spec
|
2015-09-09 23:17:17 +02:00
|
|
|
spec = describe "HTTP Errors" $ do
|
2015-09-10 08:49:19 +02:00
|
|
|
errorOrderSpec
|
2015-09-15 11:37:17 +02:00
|
|
|
prioErrorsSpec
|
2015-09-10 08:49:19 +02:00
|
|
|
errorRetrySpec
|
|
|
|
errorChoiceSpec
|
2015-09-09 22:29:52 +02:00
|
|
|
|
2016-02-17 19:56:15 +01:00
|
|
|
-- * Auth machinery (reused throughout)
|
|
|
|
|
|
|
|
-- | 'BasicAuthCheck' holds the handler we'll use to verify a username and password.
|
|
|
|
errorOrderAuthCheck :: BasicAuthCheck ()
|
|
|
|
errorOrderAuthCheck =
|
|
|
|
let check (BasicAuthData username password) =
|
|
|
|
if username == "servant" && password == "server"
|
|
|
|
then return (Authorized ())
|
|
|
|
else return Unauthorized
|
|
|
|
in BasicAuthCheck check
|
|
|
|
|
2015-09-09 23:17:17 +02:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- * Error Order {{{
|
2015-09-09 22:29:52 +02:00
|
|
|
|
|
|
|
type ErrorOrderApi = "home"
|
2016-02-17 19:56:15 +01:00
|
|
|
:> BasicAuth "error-realm" ()
|
2015-09-09 22:29:52 +02:00
|
|
|
:> ReqBody '[JSON] Int
|
|
|
|
:> Capture "t" Int
|
2016-12-12 15:17:06 +01:00
|
|
|
:> QueryParam "param" Int
|
2015-09-09 22:29:52 +02:00
|
|
|
:> Post '[JSON] Int
|
|
|
|
|
|
|
|
errorOrderApi :: Proxy ErrorOrderApi
|
|
|
|
errorOrderApi = Proxy
|
|
|
|
|
|
|
|
errorOrderServer :: Server ErrorOrderApi
|
2016-12-12 15:17:06 +01:00
|
|
|
errorOrderServer = \_ _ _ _ -> throwError err402
|
2015-09-09 22:29:52 +02:00
|
|
|
|
2016-04-12 10:35:07 +02:00
|
|
|
-- On error priorities:
|
|
|
|
--
|
|
|
|
-- We originally had
|
|
|
|
--
|
|
|
|
-- 404, 405, 401, 415, 400, 406, 402
|
|
|
|
--
|
|
|
|
-- but we changed this to
|
|
|
|
--
|
|
|
|
-- 404, 405, 401, 406, 415, 400, 402
|
|
|
|
--
|
|
|
|
-- for servant-0.7.
|
|
|
|
--
|
|
|
|
-- This change is due to the body check being irreversible (to support
|
|
|
|
-- streaming). Any check done after the body check has to be made fatal,
|
|
|
|
-- breaking modularity. We've therefore moved the accept check before
|
|
|
|
-- the body check, to allow it being recoverable and modular, and this
|
|
|
|
-- goes along with promoting the error priority of 406.
|
2015-09-10 08:49:19 +02:00
|
|
|
errorOrderSpec :: Spec
|
2016-02-17 19:56:15 +01:00
|
|
|
errorOrderSpec =
|
|
|
|
describe "HTTP error order" $
|
2016-03-08 23:28:27 +01:00
|
|
|
with (return $ serveWithContext errorOrderApi
|
|
|
|
(errorOrderAuthCheck :. EmptyContext)
|
2016-02-17 19:56:15 +01:00
|
|
|
errorOrderServer
|
|
|
|
) $ do
|
2015-09-10 08:49:19 +02:00
|
|
|
let badContentType = (hContentType, "text/plain")
|
|
|
|
badAccept = (hAccept, "text/plain")
|
2015-09-09 23:17:17 +02:00
|
|
|
badMethod = methodGet
|
2016-03-23 08:06:38 +01:00
|
|
|
badUrl = "nonexistent"
|
2015-09-09 23:17:17 +02:00
|
|
|
badBody = "nonsense"
|
2016-02-17 19:56:15 +01:00
|
|
|
badAuth = (hAuthorization, "Basic foofoofoo")
|
2015-09-10 08:49:19 +02:00
|
|
|
goodContentType = (hContentType, "application/json")
|
2015-09-16 22:07:55 +02:00
|
|
|
goodAccept = (hAccept, "application/json")
|
2015-09-09 23:17:17 +02:00
|
|
|
goodMethod = methodPost
|
2016-12-12 15:17:06 +01:00
|
|
|
goodUrl = "home/2?param=55"
|
|
|
|
badParams = goodUrl <> "?param=foo"
|
2015-09-09 23:17:17 +02:00
|
|
|
goodBody = encode (5 :: Int)
|
2016-02-17 19:56:15 +01:00
|
|
|
-- username:password = servant:server
|
|
|
|
goodAuth = (hAuthorization, "Basic c2VydmFudDpzZXJ2ZXI=")
|
2015-09-09 23:17:17 +02:00
|
|
|
|
|
|
|
it "has 404 as its highest priority error" $ do
|
2016-02-17 19:56:15 +01:00
|
|
|
request badMethod badUrl [badAuth, badContentType, badAccept] badBody
|
2015-09-09 23:17:17 +02:00
|
|
|
`shouldRespondWith` 404
|
|
|
|
|
|
|
|
it "has 405 as its second highest priority error" $ do
|
2016-12-12 15:17:06 +01:00
|
|
|
request badMethod badParams [badAuth, badContentType, badAccept] badBody
|
2015-09-09 23:17:17 +02:00
|
|
|
`shouldRespondWith` 405
|
|
|
|
|
2016-02-17 19:56:15 +01:00
|
|
|
it "has 401 as its third highest priority error (auth)" $ do
|
2016-12-12 15:17:06 +01:00
|
|
|
request goodMethod badParams [badAuth, badContentType, badAccept] badBody
|
2016-02-17 19:56:15 +01:00
|
|
|
`shouldRespondWith` 401
|
|
|
|
|
2016-04-12 10:35:07 +02:00
|
|
|
it "has 406 as its fourth highest priority error" $ do
|
2016-12-12 15:17:06 +01:00
|
|
|
request goodMethod badParams [goodAuth, badContentType, badAccept] badBody
|
2016-04-12 10:35:07 +02:00
|
|
|
`shouldRespondWith` 406
|
|
|
|
|
|
|
|
it "has 415 as its fifth highest priority error" $ do
|
2016-12-12 15:17:06 +01:00
|
|
|
request goodMethod badParams [goodAuth, badContentType, goodAccept] badBody
|
2015-09-09 23:17:17 +02:00
|
|
|
`shouldRespondWith` 415
|
|
|
|
|
2016-04-12 10:35:07 +02:00
|
|
|
it "has 400 as its sixth highest priority error" $ do
|
2017-01-16 13:17:20 +01:00
|
|
|
badParamsRes <- request goodMethod badParams [goodAuth, goodContentType, goodAccept] goodBody
|
|
|
|
badBodyRes <- request goodMethod goodUrl [goodAuth, goodContentType, goodAccept] badBody
|
|
|
|
|
|
|
|
-- Both bad body and bad params result in 400
|
|
|
|
return badParamsRes `shouldRespondWith` 400
|
|
|
|
return badBodyRes `shouldRespondWith` 400
|
|
|
|
|
|
|
|
-- Param check should occur before body checks
|
|
|
|
both <- request goodMethod badParams [goodAuth, goodContentType, goodAccept ] badBody
|
|
|
|
when (both /= badParamsRes) $ liftIO $
|
|
|
|
expectationFailure $ "badParams + badBody /= badParams: " ++ show both ++ ", " ++ show badParamsRes
|
|
|
|
when (both == badBodyRes) $ liftIO $
|
|
|
|
expectationFailure $ "badParams + badBody == badBody: " ++ show both
|
2015-09-09 23:17:17 +02:00
|
|
|
|
2015-09-16 22:07:55 +02:00
|
|
|
it "has handler-level errors as last priority" $ do
|
2016-02-17 19:56:15 +01:00
|
|
|
request goodMethod goodUrl [goodAuth, goodContentType, goodAccept] goodBody
|
2015-09-16 22:07:55 +02:00
|
|
|
`shouldRespondWith` 402
|
|
|
|
|
2015-09-15 11:37:17 +02:00
|
|
|
type PrioErrorsApi = ReqBody '[JSON] Integer :> "foo" :> Get '[JSON] Integer
|
|
|
|
|
|
|
|
prioErrorsApi :: Proxy PrioErrorsApi
|
|
|
|
prioErrorsApi = Proxy
|
|
|
|
|
|
|
|
-- Check whether matching continues even if a 'ReqBody' or similar construct
|
|
|
|
-- is encountered early in a path. We don't want to see a complaint about the
|
|
|
|
-- request body unless the path actually matches.
|
|
|
|
prioErrorsSpec :: Spec
|
|
|
|
prioErrorsSpec = describe "PrioErrors" $ do
|
|
|
|
let server = return
|
2016-02-18 16:36:24 +01:00
|
|
|
with (return $ serve prioErrorsApi server) $ do
|
2015-09-15 11:37:17 +02:00
|
|
|
let check (mdescr, method) path (cdescr, ctype, body) resp =
|
|
|
|
it fulldescr $
|
|
|
|
Test.Hspec.Wai.request method path [(hContentType, ctype)] body
|
|
|
|
`shouldRespondWith` resp
|
|
|
|
where
|
|
|
|
fulldescr = "returns " ++ show (matchStatus resp) ++ " on " ++ mdescr
|
2015-09-16 22:07:55 +02:00
|
|
|
++ " " ++ BC.unpack path ++ " (" ++ cdescr ++ ")"
|
2015-09-15 11:37:17 +02:00
|
|
|
|
|
|
|
get' = ("GET", methodGet)
|
|
|
|
put' = ("PUT", methodPut)
|
|
|
|
|
|
|
|
txt = ("text" , "text/plain;charset=utf8" , "42" )
|
|
|
|
ijson = ("invalid json", "application/json;charset=utf8", "invalid" )
|
|
|
|
vjson = ("valid json" , "application/json;charset=utf8", encode (5 :: Int))
|
|
|
|
|
|
|
|
check get' "/" txt 404
|
|
|
|
check get' "/bar" txt 404
|
|
|
|
check get' "/foo" txt 415
|
|
|
|
check put' "/" txt 404
|
|
|
|
check put' "/bar" txt 404
|
|
|
|
check put' "/foo" txt 405
|
|
|
|
check get' "/" ijson 404
|
|
|
|
check get' "/bar" ijson 404
|
|
|
|
check get' "/foo" ijson 400
|
|
|
|
check put' "/" ijson 404
|
|
|
|
check put' "/bar" ijson 404
|
|
|
|
check put' "/foo" ijson 405
|
|
|
|
check get' "/" vjson 404
|
|
|
|
check get' "/bar" vjson 404
|
|
|
|
check get' "/foo" vjson 200
|
|
|
|
check put' "/" vjson 404
|
|
|
|
check put' "/bar" vjson 404
|
|
|
|
check put' "/foo" vjson 405
|
|
|
|
|
2015-09-09 23:17:17 +02:00
|
|
|
-- }}}
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- * Error Retry {{{
|
|
|
|
|
|
|
|
type ErrorRetryApi
|
2015-09-16 22:07:55 +02:00
|
|
|
= "a" :> ReqBody '[JSON] Int :> Post '[JSON] Int -- err402
|
2015-09-09 23:17:17 +02:00
|
|
|
:<|> "a" :> ReqBody '[PlainText] Int :> Post '[JSON] Int -- 1
|
|
|
|
:<|> "a" :> ReqBody '[JSON] Int :> Post '[PlainText] Int -- 2
|
|
|
|
:<|> "a" :> ReqBody '[JSON] String :> Post '[JSON] Int -- 3
|
2015-09-10 08:49:19 +02:00
|
|
|
:<|> "a" :> ReqBody '[JSON] Int :> Get '[JSON] Int -- 4
|
2016-02-17 19:56:15 +01:00
|
|
|
:<|> "a" :> BasicAuth "bar-realm" ()
|
|
|
|
:> ReqBody '[JSON] Int :> Get '[PlainText] Int -- 5
|
|
|
|
:<|> "a" :> ReqBody '[JSON] Int :> Get '[PlainText] Int -- 6
|
|
|
|
|
|
|
|
:<|> ReqBody '[JSON] Int :> Get '[JSON] Int -- 7
|
|
|
|
:<|> ReqBody '[JSON] Int :> Post '[JSON] Int -- 8
|
2015-09-09 23:17:17 +02:00
|
|
|
|
|
|
|
errorRetryApi :: Proxy ErrorRetryApi
|
|
|
|
errorRetryApi = Proxy
|
|
|
|
|
|
|
|
errorRetryServer :: Server ErrorRetryApi
|
|
|
|
errorRetryServer
|
2017-01-16 10:44:25 +01:00
|
|
|
= (\_ -> throwError err402)
|
2015-09-09 23:17:17 +02:00
|
|
|
:<|> (\_ -> return 1)
|
|
|
|
:<|> (\_ -> return 2)
|
|
|
|
:<|> (\_ -> return 3)
|
|
|
|
:<|> (\_ -> return 4)
|
2016-02-17 19:56:15 +01:00
|
|
|
:<|> (\_ _ -> return 5)
|
2015-09-09 23:17:17 +02:00
|
|
|
:<|> (\_ -> return 6)
|
2015-09-10 08:49:19 +02:00
|
|
|
:<|> (\_ -> return 7)
|
2016-02-17 19:56:15 +01:00
|
|
|
:<|> (\_ -> return 8)
|
2015-09-09 23:17:17 +02:00
|
|
|
|
2015-09-10 08:49:19 +02:00
|
|
|
errorRetrySpec :: Spec
|
2016-02-17 19:56:15 +01:00
|
|
|
errorRetrySpec =
|
|
|
|
describe "Handler search" $
|
2016-03-08 23:28:27 +01:00
|
|
|
with (return $ serveWithContext errorRetryApi
|
|
|
|
(errorOrderAuthCheck :. EmptyContext)
|
2016-02-17 19:56:15 +01:00
|
|
|
errorRetryServer
|
|
|
|
) $ do
|
2015-09-10 08:49:19 +02:00
|
|
|
|
2015-10-13 20:29:14 +02:00
|
|
|
let jsonCT = (hContentType, "application/json")
|
2015-09-10 08:49:19 +02:00
|
|
|
jsonAccept = (hAccept, "application/json")
|
2015-09-09 23:17:17 +02:00
|
|
|
jsonBody = encode (1797 :: Int)
|
|
|
|
|
|
|
|
it "should continue when URLs don't match" $ do
|
|
|
|
request methodPost "" [jsonCT, jsonAccept] jsonBody
|
2017-01-01 19:52:18 +01:00
|
|
|
`shouldRespondWith` 200 { matchBody = mkBody $ encode (8 :: Int) }
|
2015-09-09 23:17:17 +02:00
|
|
|
|
|
|
|
it "should continue when methods don't match" $ do
|
|
|
|
request methodGet "a" [jsonCT, jsonAccept] jsonBody
|
2017-01-01 19:52:18 +01:00
|
|
|
`shouldRespondWith` 200 { matchBody = mkBody $ encode (4 :: Int) }
|
|
|
|
where
|
|
|
|
mkBody b = MatchBody $ \_ b' ->
|
|
|
|
if b == b'
|
|
|
|
then Nothing
|
|
|
|
else Just "body not correct\n"
|
|
|
|
|
2015-09-10 08:49:19 +02:00
|
|
|
-- }}}
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- * Error Choice {{{
|
|
|
|
|
|
|
|
type ErrorChoiceApi
|
|
|
|
= "path0" :> Get '[JSON] Int -- 0
|
|
|
|
:<|> "path1" :> Post '[JSON] Int -- 1
|
|
|
|
:<|> "path2" :> Post '[PlainText] Int -- 2
|
|
|
|
:<|> "path3" :> ReqBody '[JSON] Int :> Post '[PlainText] Int -- 3
|
|
|
|
:<|> "path4" :> (ReqBody '[PlainText] Int :> Post '[PlainText] Int -- 4
|
|
|
|
:<|> ReqBody '[PlainText] Int :> Post '[JSON] Int) -- 5
|
2018-04-05 00:53:40 +02:00
|
|
|
:<|> "path5" :> (ReqBody '[JSON] Int :> Post '[PlainText] Int -- 6
|
|
|
|
:<|> ReqBody '[PlainText] Int :> Post '[PlainText] Int) -- 7
|
2015-09-10 08:49:19 +02:00
|
|
|
|
|
|
|
errorChoiceApi :: Proxy ErrorChoiceApi
|
|
|
|
errorChoiceApi = Proxy
|
|
|
|
|
|
|
|
errorChoiceServer :: Server ErrorChoiceApi
|
|
|
|
errorChoiceServer = return 0
|
|
|
|
:<|> return 1
|
|
|
|
:<|> return 2
|
|
|
|
:<|> (\_ -> return 3)
|
2018-04-05 00:53:40 +02:00
|
|
|
:<|> ((\_ -> return 4) :<|> (\_ -> return 5))
|
|
|
|
:<|> ((\_ -> return 6) :<|> (\_ -> return 7))
|
2015-09-10 08:49:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
errorChoiceSpec :: Spec
|
|
|
|
errorChoiceSpec = describe "Multiple handlers return errors"
|
2016-02-18 16:36:24 +01:00
|
|
|
$ with (return $ serve errorChoiceApi errorChoiceServer) $ do
|
2015-09-10 08:49:19 +02:00
|
|
|
|
|
|
|
it "should respond with 404 if no path matches" $ do
|
|
|
|
request methodGet "" [] "" `shouldRespondWith` 404
|
|
|
|
|
|
|
|
it "should respond with 405 if a path but not method matches" $ do
|
|
|
|
request methodGet "path2" [] "" `shouldRespondWith` 405
|
|
|
|
|
|
|
|
it "should respond with the corresponding error if path and method match" $ do
|
|
|
|
request methodPost "path3" [(hContentType, "text/plain;charset=utf-8")] ""
|
|
|
|
`shouldRespondWith` 415
|
|
|
|
request methodPost "path3" [(hContentType, "application/json")] ""
|
|
|
|
`shouldRespondWith` 400
|
|
|
|
request methodPost "path4" [(hContentType, "text/plain;charset=utf-8"),
|
2015-09-16 22:07:55 +02:00
|
|
|
(hAccept, "blah")] "5"
|
2015-09-10 08:49:19 +02:00
|
|
|
`shouldRespondWith` 406
|
2018-04-05 00:53:40 +02:00
|
|
|
it "should respond with 415 only if none of the subservers supports the request's content type" $ do
|
|
|
|
request methodPost "path5" [(hContentType, "text/plain;charset=utf-8")] "1"
|
|
|
|
`shouldRespondWith` 200
|
|
|
|
request methodPost "path5" [(hContentType, "application/json")] "1"
|
|
|
|
`shouldRespondWith` 200
|
|
|
|
request methodPost "path5" [(hContentType, "application/not-supported")] ""
|
|
|
|
`shouldRespondWith` 415
|
2015-09-10 08:49:19 +02:00
|
|
|
|
|
|
|
|
2015-09-09 23:17:17 +02:00
|
|
|
-- }}}
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- * Instances {{{
|
|
|
|
|
|
|
|
instance MimeUnrender PlainText Int where
|
2015-09-16 22:07:55 +02:00
|
|
|
mimeUnrender _ x = maybe (Left "no parse") Right (readMay $ BCL.unpack x)
|
2015-09-09 23:17:17 +02:00
|
|
|
|
|
|
|
instance MimeRender PlainText Int where
|
2015-09-15 11:37:17 +02:00
|
|
|
mimeRender _ = BCL.pack . show
|
2015-09-09 23:17:17 +02:00
|
|
|
-- }}}
|