git.iko.soy/typeable/servant
on 1148e21 help sign in
typeable/servant/test/Servant/Utils/StaticFilesSpec.hs
64 lines 1.9 kB history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE TypeOperators #-}
5{-# OPTIONS_GHC -fno-warn-orphans #-}
6module Servant.Utils.StaticFilesSpec where
8import Control.Exception (bracket)
9import Data.Proxy (Proxy(Proxy))
10import Network.Wai (Application)
11import System.Directory (getCurrentDirectory, setCurrentDirectory, createDirectory)
12import System.IO.Temp (withSystemTempDirectory)
13import Test.Hspec (Spec, describe, it, around_)
14import Test.Hspec.Wai (with, get, shouldRespondWith)
16import Servant.API.Alternative ((:<|>)((:<|>)))
17import Servant.API.Capture (Capture)
18import Servant.API.Get (Get)
19import Servant.API.Raw (Raw)
20import Servant.API.Sub ((:>))
21import Servant.Server (Server, serve)
22import Servant.ServerSpec (Person(Person))
23import Servant.Utils.StaticFiles (serveDirectory)
25type Api =
26 "dummy_api" :> Capture "person_name" String :> Get Person
27 :<|> "static" :> Raw
30api :: Proxy Api
31api = Proxy
33app :: Application
34app = serve api server
36server :: Server Api
37server =
38 (\ name_ -> return (Person name_ 42))
39 :<|> serveDirectory "static"
41withStaticFiles :: IO () -> IO ()
42withStaticFiles action = withSystemTempDirectory "servant-test" $ \ tmpDir ->
43 bracket (setup tmpDir) teardown (const action)
44 where
45 setup tmpDir = do
46 outer <- getCurrentDirectory
47 setCurrentDirectory tmpDir
48 createDirectory "static"
49 writeFile "static/foo.txt" "bar"
50 writeFile "static/index.html" "index"
51 return outer
53 teardown outer = do
54 setCurrentDirectory outer
56spec :: Spec
57spec = do
58 around_ withStaticFiles $ with (return app) $ do
59 describe "serveDirectory" $ do
60 it "successfully serves files" $ do
61 get "/static/foo.txt" `shouldRespondWith` "bar"
63 it "serves the contents of index.html when requesting the root of a directory" $ do
64 get "/static/" `shouldRespondWith` "index"