2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2014-12-10 16:10:57 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2014-12-10 16:10:57 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
module Servant.Utils.StaticFilesSpec where
|
|
|
|
|
2015-08-17 23:56:29 +02:00
|
|
|
import Control.Exception (bracket)
|
|
|
|
import Data.Proxy (Proxy (Proxy))
|
|
|
|
import Network.Wai (Application)
|
|
|
|
import System.Directory (createDirectory,
|
|
|
|
getCurrentDirectory,
|
|
|
|
setCurrentDirectory)
|
|
|
|
import System.IO.Temp (withSystemTempDirectory)
|
|
|
|
import Test.Hspec (Spec, around_, describe, it)
|
|
|
|
import Test.Hspec.Wai (get, shouldRespondWith, with)
|
2014-12-10 16:10:57 +01:00
|
|
|
|
2015-11-27 02:05:34 +01:00
|
|
|
import Servant.API ((:<|>) ((:<|>)), Capture, Get, Raw, (:>), JSON)
|
2016-02-18 16:36:24 +01:00
|
|
|
import Servant.Server (Server, serve)
|
2015-08-17 23:56:29 +02:00
|
|
|
import Servant.ServerSpec (Person (Person))
|
|
|
|
import Servant.Utils.StaticFiles (serveDirectory)
|
2014-12-10 16:10:57 +01:00
|
|
|
|
|
|
|
type Api =
|
2015-01-12 15:08:41 +01:00
|
|
|
"dummy_api" :> Capture "person_name" String :> Get '[JSON] Person
|
2014-12-10 16:10:57 +01:00
|
|
|
:<|> "static" :> Raw
|
|
|
|
|
|
|
|
|
|
|
|
api :: Proxy Api
|
|
|
|
api = Proxy
|
|
|
|
|
|
|
|
app :: Application
|
2016-02-18 16:36:24 +01:00
|
|
|
app = serve api server
|
2014-12-10 16:10:57 +01:00
|
|
|
|
|
|
|
server :: Server Api
|
|
|
|
server =
|
2015-01-06 17:26:37 +01:00
|
|
|
(\ name_ -> return (Person name_ 42))
|
2014-12-10 16:10:57 +01:00
|
|
|
:<|> serveDirectory "static"
|
|
|
|
|
|
|
|
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
|
|
|
|
around_ withStaticFiles $ with (return app) $ do
|
|
|
|
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
|
2015-01-16 05:52:11 +01:00
|
|
|
get "/static/" `shouldRespondWith` "index"
|