|
| 1 | +module Servant.Server.Internal.RoutingApplicationSpec (spec) where |
| 2 | + |
| 3 | +import Control.Exception hiding (Handler) |
| 4 | +import Control.Monad.IO.Class |
| 5 | +import Servant.Server |
| 6 | +import Servant.Server.Internal.RoutingApplication |
| 7 | +import System.Directory |
| 8 | +import Test.Hspec |
| 9 | + |
| 10 | +ok :: IO (RouteResult ()) |
| 11 | +ok = return (Route ()) |
| 12 | + |
| 13 | +delayed :: DelayedIO () -> RouteResult (Handler ()) -> Delayed () (Handler ()) |
| 14 | +delayed body srv = Delayed |
| 15 | + { capturesD = \() -> DelayedIO $ \_req _cl -> ok |
| 16 | + , methodD = DelayedIO $ \_req_ _cl -> ok |
| 17 | + , authD = DelayedIO $ \_req _cl -> ok |
| 18 | + , bodyD = do |
| 19 | + liftIO (writeFile "delayed.test" "hia") |
| 20 | + addCleanup (removeFile "delayed.test" >> putStrLn "file removed") |
| 21 | + body |
| 22 | + , serverD = \() () _body _req -> srv |
| 23 | + } |
| 24 | + |
| 25 | +simpleRun :: Delayed () (Handler ()) |
| 26 | + -> IO () |
| 27 | +simpleRun d = fmap (either ignoreE id) . try $ |
| 28 | + runAction d () undefined (\_ -> return ()) (\_ -> FailFatal err500) |
| 29 | + |
| 30 | + where ignoreE :: SomeException -> () |
| 31 | + ignoreE = const () |
| 32 | + |
| 33 | +spec :: Spec |
| 34 | +spec = do |
| 35 | + describe "Delayed" $ do |
| 36 | + it "actually runs clean up actions" $ do |
| 37 | + _ <- simpleRun $ delayed (return ()) (Route $ return ()) |
| 38 | + fileStillThere <- doesFileExist "delayed.test" |
| 39 | + fileStillThere `shouldBe` False |
| 40 | + it "even with exceptions in serverD" $ do |
| 41 | + _ <- simpleRun $ delayed (return ()) (Route $ throw DivideByZero) |
| 42 | + fileStillThere <- doesFileExist "delayed.test" |
| 43 | + fileStillThere `shouldBe` False |
| 44 | + it "even with routing failure in bodyD" $ do |
| 45 | + _ <- simpleRun $ delayed (delayedFailFatal err500) (Route $ return ()) |
| 46 | + fileStillThere <- doesFileExist "delayed.test" |
| 47 | + fileStillThere `shouldBe` False |
| 48 | + it "even with exceptions in bodyD" $ do |
| 49 | + _ <- simpleRun $ delayed (liftIO $ throwIO DivideByZero) (Route $ return ()) |
| 50 | + fileStillThere <- doesFileExist "delayed.test" |
| 51 | + fileStillThere `shouldBe` False |
0 commit comments