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)
27 "dummy_api" :> Capture "person_name" String :> Get '[JSON] Person
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)
47 outer <- getCurrentDirectory
48 setCurrentDirectory tmpDir
49 createDirectory "static"
50 writeFile "static/foo.txt" "bar"
51 writeFile "static/index.html" "index"
55 setCurrentDirectory outer
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"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list