@@ -3,9 +3,12 @@
33module SendFileSpec where
44
55import Control.Exception
6+import Control.Monad (when)
67import Data.ByteString (ByteString)
8+import qualified Data.ByteString as BS
79import Network.Wai.Handler.Warp.Buffer
810import Network.Wai.Handler.Warp.SendFile
11+import System.Directory
912import Test.Hspec
1013
1114main :: IO ()
@@ -25,6 +28,18 @@ spec = describe "packHeader" $ do
2528 tryPackHeader 10 ["01234567890", "12"] `shouldReturn` 3
2629 it "returns how much the buffer is consumed (6)" $
2730 tryPackHeader 10 ["012345678901234567890123456789012", "34"] `shouldReturn` 5
31+ it "returns how much the buffer is consumed (1)" $
32+ tryPackHeader2 10 ["foo"] "" `shouldReturn` True
33+ it "returns how much the buffer is consumed (2)" $
34+ tryPackHeader2 10 ["foo", "bar"] "" `shouldReturn` True
35+ it "returns how much the buffer is consumed (3)" $
36+ tryPackHeader2 10 ["0123456789"] "0123456789" `shouldReturn` True
37+ it "returns how much the buffer is consumed (4)" $
38+ tryPackHeader2 10 ["01234", "56789"] "0123456789" `shouldReturn` True
39+ it "returns how much the buffer is consumed (5)" $
40+ tryPackHeader2 10 ["01234567890", "12"] "0123456789" `shouldReturn` True
41+ it "returns how much the buffer is consumed (6)" $
42+ tryPackHeader2 10 ["012345678901234567890123456789012", "34"] "012345678901234567890123456789" `shouldReturn` True
2843
2944tryPackHeader :: Int -> [ByteString] -> IO Int
3045tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
@@ -32,3 +47,28 @@ tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
3247 where
3348 send _ = return ()
3449 hook = return ()
50+
51+tryPackHeader2 :: Int -> [ByteString] -> ByteString -> IO Bool
52+tryPackHeader2 siz hdrs ans = bracket setup teardown $ \buf -> do
53+ _ <- packHeader buf siz send hook hdrs 0
54+ checkFile outputFile ans
55+ where
56+ setup = allocateBuffer siz
57+ teardown buf = freeBuffer buf >> removeFileIfExists outputFile
58+ outputFile = "tempfile"
59+ send = BS.appendFile outputFile
60+ hook = return ()
61+
62+checkFile :: FilePath -> ByteString -> IO Bool
63+checkFile path bs = do
64+ exist <- doesFileExist path
65+ if exist then do
66+ bs' <- BS.readFile path
67+ return $ bs == bs'
68+ else
69+ return $ bs == ""
70+
71+removeFileIfExists :: FilePath -> IO ()
72+removeFileIfExists file = do
73+ exist <- doesFileExist file
74+ when exist $ removeFile file