@@ -0,0 +1,34 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module SendFileSpec where
4+
5+import Control.Exception
6+import Data.ByteString (ByteString)
7+import Network.Wai.Handler.Warp.Buffer
8+import Network.Wai.Handler.Warp.SendFile
9+import Test.Hspec
10+
11+main :: IO ()
12+main = hspec spec
13+
14+spec :: Spec
15+spec = describe "packHeader" $ do
16+ it "returns how much the buffer is consumed (1)" $
17+ tryPackHeader 10 ["foo"] `shouldReturn` 3
18+ it "returns how much the buffer is consumed (2)" $
19+ tryPackHeader 10 ["foo", "bar"] `shouldReturn` 6
20+ it "returns how much the buffer is consumed (3)" $
21+ tryPackHeader 10 ["0123456789"] `shouldReturn` 0
22+ it "returns how much the buffer is consumed (4)" $
23+ tryPackHeader 10 ["01234", "56789"] `shouldReturn` 0
24+ it "returns how much the buffer is consumed (5)" $
25+ tryPackHeader 10 ["01234567890", "12"] `shouldReturn` 3
26+ it "returns how much the buffer is consumed (6)" $
27+ tryPackHeader 10 ["012345678901234567890123456789012", "34"] `shouldReturn` 5
28+
29+tryPackHeader :: Int -> [ByteString] -> IO Int
30+tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
31+ packHeader buf siz send hook hdrs 0
32+ where
33+ send _ = return ()
34+ hook = return ()