2014-11-12 06:21:59 +01:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2014-11-12 09:58:22 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2014-11-12 06:21:59 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2014-11-12 09:58:22 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2014-11-12 06:21:59 +01:00
|
|
|
module Servant.Utils.StaticFilesSpec where
|
|
|
|
|
2014-11-12 09:58:22 +01:00
|
|
|
import Control.Exception
|
2014-11-12 06:21:59 +01:00
|
|
|
import Data.Proxy
|
2014-11-12 09:58:22 +01:00
|
|
|
import Network.Wai
|
2014-11-12 06:21:59 +01:00
|
|
|
import System.Directory
|
2014-11-12 09:58:22 +01:00
|
|
|
import System.IO.Temp
|
|
|
|
import Test.Hspec hiding (pending)
|
|
|
|
import Test.Hspec.Wai
|
2014-11-12 06:21:59 +01:00
|
|
|
|
2014-11-12 09:58:22 +01:00
|
|
|
import Servant.API.Alternative
|
|
|
|
import Servant.API.Capture
|
|
|
|
import Servant.API.Get
|
2014-11-12 06:21:59 +01:00
|
|
|
import Servant.API.Raw
|
2014-11-12 09:58:22 +01:00
|
|
|
import Servant.API.Sub
|
2014-11-12 06:21:59 +01:00
|
|
|
import Servant.Server
|
2014-11-12 09:58:22 +01:00
|
|
|
import Servant.ServerSpec
|
|
|
|
import Servant.Utils.StaticFiles
|
2014-11-12 06:21:59 +01:00
|
|
|
|
|
|
|
type Api =
|
2014-11-12 09:58:22 +01:00
|
|
|
"dummy_api" :> Capture "person_name" String :> Get Person
|
|
|
|
:<|> "static" :> Raw
|
|
|
|
|
|
|
|
|
|
|
|
api :: Proxy Api
|
|
|
|
api = Proxy
|
2014-11-12 06:21:59 +01:00
|
|
|
|
|
|
|
app :: Application
|
2014-11-12 09:58:22 +01:00
|
|
|
app = serve api server
|
2014-11-12 06:21:59 +01:00
|
|
|
|
|
|
|
server :: Server Api
|
2014-11-12 09:58:22 +01:00
|
|
|
server =
|
|
|
|
(\ name -> return (Person name 42))
|
|
|
|
:<|> serveDirectory "static"
|
2014-11-12 06:21:59 +01:00
|
|
|
|
|
|
|
withStaticFiles :: IO () -> IO ()
|
|
|
|
withStaticFiles action = withSystemTempDirectory "servant-test" $ \ tmpDir ->
|
|
|
|
bracket (setup tmpDir) teardown (const action)
|
|
|
|
where
|
|
|
|
setup tmpDir = do
|
|
|
|
outer <- getCurrentDirectory
|
|
|
|
setCurrentDirectory tmpDir
|
|
|
|
createDirectory "static"
|
|
|
|
writeFile "static/foo.txt" "bar"
|
|
|
|
writeFile "static/index.html" "index"
|
|
|
|
return outer
|
|
|
|
|
|
|
|
teardown outer = do
|
|
|
|
setCurrentDirectory outer
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
2014-11-12 09:58:22 +01:00
|
|
|
around_ withStaticFiles $ with (return app) $ do
|
2014-11-12 06:21:59 +01:00
|
|
|
describe "serveDirectory" $ do
|
|
|
|
it "successfully serves files" $ do
|
|
|
|
get "/static/foo.txt" `shouldRespondWith` "bar"
|
|
|
|
|
|
|
|
it "serves the contents of index.html when requesting the root of a directory" $ do
|
|
|
|
get "/static" `shouldRespondWith` "index"
|