1{-# LANGUAGE OverloadedStrings #-}
2
3module FileSpec (main, spec) where
4
5import Network.HTTP.Types
6import Network.Wai.Handler.Warp.File
7import Network.Wai.Handler.Warp.FileInfoCache
8import Network.Wai.Handler.Warp.Header
9
10import Test.Hspec
11
12main :: IO ()
13main = hspec spec
14
15testFileRange :: String
16              -> RequestHeaders -> FilePath
17              -> RspFileInfo
18              -> Spec
19testFileRange desc reqhs file ans = it desc $ do
20    finfo <- getInfo file
21    let WithBody s hs off len = ans
22        hs' = ("Last-Modified",fileInfoDate finfo) : hs
23        ans' = WithBody s hs' off len
24    conditionalRequest finfo [] (indexResponseHeader hs) (indexRequestHeader reqhs) `shouldBe` ans'
25
26spec :: Spec
27spec = do
28    describe "conditionalRequest" $ do
29        testFileRange
30            "gets a file size from file system"
31            [] "attic/hex"
32            $ WithBody ok200 [("Content-Length","16"),("Accept-Ranges","bytes")] 0 16
33        testFileRange
34            "gets a file size from file system and handles Range and returns Partical Content"
35            [("Range","bytes=2-14")] "attic/hex"
36            $ WithBody status206 [("Content-Range","bytes 2-14/16"),("Content-Length","13"),("Accept-Ranges","bytes")] 2 13
37        testFileRange
38            "truncates end point of range to file size"
39            [("Range","bytes=10-20")] "attic/hex"
40            $ WithBody status206 [("Content-Range","bytes 10-15/16"),("Content-Length","6"),("Accept-Ranges","bytes")] 10 6
41        testFileRange
42            "gets a file size from file system and handles Range and returns OK if Range means the entire"
43            [("Range:","bytes=0-15")] "attic/hex"
44            $ WithBody status200 [("Content-Length","16"),("Accept-Ranges","bytes")] 0 16
45
46