@@ -11,11 +11,12 @@ module Servant.ServerSpec where
11import Control.Monad.Trans.Either (EitherT, left)
11import Control.Monad.Trans.Either (EitherT, left)
12import Data.Aeson (ToJSON, FromJSON, encode, decode')
12import Data.Aeson (ToJSON, FromJSON, encode, decode')
13import Data.Char (toUpper)
13import Data.Char (toUpper)
14+import Data.Monoid ((<>))
14import Data.Proxy (Proxy(Proxy))
15import Data.Proxy (Proxy(Proxy))
15import Data.String (fromString)
16import Data.String (fromString)
16import Data.String.Conversions (cs)
17import Data.String.Conversions (cs)
17import GHC.Generics (Generic)
18import GHC.Generics (Generic)
18−import Network.HTTP.Types (parseQuery, ok200)
19+import Network.HTTP.Types (parseQuery, ok200, status409)
19import Network.Wai (Application, Request, responseLBS, pathInfo, queryString, rawQueryString)
20import Network.Wai (Application, Request, responseLBS, pathInfo, queryString, rawQueryString)
20import Network.Wai.Test (runSession, request, defaultRequest, simpleBody)
21import Network.Wai.Test (runSession, request, defaultRequest, simpleBody)
21import Test.Hspec (Spec, describe, it, shouldBe)
22import Test.Hspec (Spec, describe, it, shouldBe)
@@ -31,6 +32,7 @@ import Servant.API.Raw (Raw)
31import Servant.API.Sub ((:>))
32import Servant.API.Sub ((:>))
32import Servant.API.Alternative ((:<|>)((:<|>)))
33import Servant.API.Alternative ((:<|>)((:<|>)))
33import Servant.Server (Server, serve)
34import Servant.Server (Server, serve)
35+import Servant.Server.Internal (RouteMismatch(..))
36-- * test data types
38-- * test data types
@@ -74,6 +76,7 @@ spec = do
79type CaptureApi = Capture "legs" Integer :> Get Animal
82type CaptureApi = Capture "legs" Integer :> Get Animal
@@ -364,3 +367,48 @@ unionSpec = do
364 liftIO $ do
367 liftIO $ do
365 decode' (simpleBody response_) `shouldBe`
368 decode' (simpleBody response_) `shouldBe`
366 Just jerry
369 Just jerry
371+-- | Test server error functionality.
374+ let he = HttpError status409 (Just "A custom error")
375+ let ib = InvalidBody "The body is invalid"
376+ let wm = WrongMethod
379+ describe "Servant.Server.Internal.RouteMismatch" $ do
380+ it "HttpError > *" $ do
381+ ib <> he `shouldBe` he
382+ wm <> he `shouldBe` he
383+ nf <> he `shouldBe` he
385+ he <> ib `shouldBe` he
386+ he <> wm `shouldBe` he
387+ he <> nf `shouldBe` he
389+ it "HE > InvalidBody > (WM,NF)" $ do
390+ he <> ib `shouldBe` he
391+ wm <> ib `shouldBe` ib
392+ nf <> ib `shouldBe` ib
394+ ib <> he `shouldBe` he
395+ ib <> wm `shouldBe` ib
396+ ib <> nf `shouldBe` ib
398+ it "HE > IB > WrongMethod > NF" $ do
399+ he <> wm `shouldBe` he
400+ ib <> wm `shouldBe` ib
401+ nf <> wm `shouldBe` wm
403+ wm <> he `shouldBe` he
404+ wm <> ib `shouldBe` ib
405+ wm <> nf `shouldBe` wm
407+ it "* > NotFound" $ do
408+ he <> nf `shouldBe` he
409+ ib <> nf `shouldBe` ib
410+ wm <> nf `shouldBe` wm
412+ nf <> he `shouldBe` he
413+ nf <> ib `shouldBe` ib
414+ nf <> wm `shouldBe` wm