@@ -0,0 +1,51 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE TypeOperators #-}
4+module Servant.Utils.StaticFilesSpec where
5+
6+import Test.Hspec hiding (pending)
7+import System.IO.Temp
8+import Test.Hspec.Wai
9+import Network.Wai
10+import Data.Proxy
11+import System.Directory
12+import Control.Exception
13+
14+import Servant.Utils.StaticFiles
15+import Servant.API.Sub
16+import Servant.API.Raw
17+import Servant.Server
18+
19+type Api =
20+ "static" :> Raw
21+
22+app :: Application
23+app = serve (Proxy :: Proxy Api) server
24+
25+server :: Server Api
26+server = serveDirectory "static"
27+
28+withStaticFiles :: IO () -> IO ()
29+withStaticFiles action = withSystemTempDirectory "servant-test" $ \ tmpDir ->
30+ bracket (setup tmpDir) teardown (const action)
31+ where
32+ setup tmpDir = do
33+ outer <- getCurrentDirectory
34+ setCurrentDirectory tmpDir
35+ createDirectory "static"
36+ writeFile "static/foo.txt" "bar"
37+ writeFile "static/index.html" "index"
38+ return outer
39+
40+ teardown outer = do
41+ setCurrentDirectory outer
42+
43+spec :: Spec
44+spec = do
45+ around_ withStaticFiles $ with (return app) $
46+ describe "serveDirectory" $ do
47+ it "successfully serves files" $ do
48+ get "/static/foo.txt" `shouldRespondWith` "bar"
49+
50+ it "serves the contents of index.html when requesting the root of a directory" $ do
51+ get "/static" `shouldRespondWith` "index"