@@ -3,9 +3,12 @@
3module SendFileSpec where
3module SendFileSpec where
5import Control.Exception
5import Control.Exception
6+import Control.Monad (when)
6import Data.ByteString (ByteString)
7import Data.ByteString (ByteString)
8+import qualified Data.ByteString as BS
7import Network.Wai.Handler.Warp.Buffer
9import Network.Wai.Handler.Warp.Buffer
8import Network.Wai.Handler.Warp.SendFile
10import Network.Wai.Handler.Warp.SendFile
11+import System.Directory
9import Test.Hspec
12import Test.Hspec
11main :: IO ()
14main :: IO ()
@@ -25,6 +28,18 @@ spec = describe "packHeader" $ do
25 tryPackHeader 10 ["01234567890", "12"] `shouldReturn` 3
28 tryPackHeader 10 ["01234567890", "12"] `shouldReturn` 3
26 it "returns how much the buffer is consumed (6)" $
29 it "returns how much the buffer is consumed (6)" $
27 tryPackHeader 10 ["012345678901234567890123456789012", "34"] `shouldReturn` 5
30 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
29tryPackHeader :: Int -> [ByteString] -> IO Int
44tryPackHeader :: Int -> [ByteString] -> IO Int
30tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
45tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
@@ -32,3 +47,28 @@ tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
33 send _ = return ()
48 send _ = return ()
34 hook = return ()
49 hook = return ()
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
56+ setup = allocateBuffer siz
57+ teardown buf = freeBuffer buf >> removeFileIfExists outputFile
58+ outputFile = "tempfile"
59+ send = BS.appendFile outputFile
62+checkFile :: FilePath -> ByteString -> IO Bool
63+checkFile path bs = do
64+ exist <- doesFileExist path
66+ bs' <- BS.readFile path
71+removeFileIfExists :: FilePath -> IO ()
72+removeFileIfExists file = do
73+ exist <- doesFileExist file
74+ when exist $ removeFile file