git.iko.soy/typeable/servant
on f76c729 help sign in
typeable/servant/test/Servant/Utils/StaticFilesSpec.hs
65 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 (JSON)
17import Servant.API.Alternative ((:<|>)((:<|>)))
18import Servant.API.Capture (Capture)
19import Servant.API.Get (Get)
20import Servant.API.Raw (Raw)
21import Servant.API.Sub ((:>))
22import Servant.Server (Server, serve)
23import Servant.ServerSpec (Person(Person))
24import Servant.Utils.StaticFiles (serveDirectory)
26type Api =
27 "dummy_api" :> Capture "person_name" String :> Get '[JSON] Person
28 :<|> "static" :> Raw
31api :: Proxy Api
32api = Proxy
34app :: Application
35app = serve api server
37server :: Server Api
38server =
39 (\ name_ -> return (Person name_ 42))
40 :<|> serveDirectory "static"
42withStaticFiles :: IO () -> IO ()
43withStaticFiles action = withSystemTempDirectory "servant-test" $ \ tmpDir ->
44 bracket (setup tmpDir) teardown (const action)
45 where
46 setup tmpDir = do
47 outer <- getCurrentDirectory
48 setCurrentDirectory tmpDir
49 createDirectory "static"
50 writeFile "static/foo.txt" "bar"
51 writeFile "static/index.html" "index"
52 return outer
54 teardown outer = do
55 setCurrentDirectory outer
57spec :: Spec
58spec = do
59 around_ withStaticFiles $ with (return app) $ do
60 describe "serveDirectory" $ do
61 it "successfully serves files" $ do
62 get "/static/foo.txt" `shouldRespondWith` "bar"
64 it "serves the contents of index.html when requesting the root of a directory" $ do
65 get "/static/" `shouldRespondWith` "index"