@@ -8,14 +8,19 @@ import Data.ByteString (ByteString)
8import qualified Data.ByteString as BS
8import qualified Data.ByteString as BS
9import Network.Wai.Handler.Warp.Buffer
9import Network.Wai.Handler.Warp.Buffer
10import Network.Wai.Handler.Warp.SendFile
10import Network.Wai.Handler.Warp.SendFile
11+import Network.Wai.Handler.Warp.Types
11import System.Directory
12import System.Directory
14+import qualified System.IO as IO
15+import System.Process (system)
12import Test.Hspec
16import Test.Hspec
14main :: IO ()
18main :: IO ()
15main = hspec spec
19main = hspec spec
17spec :: Spec
21spec :: Spec
18−spec = describe "packHeader" $ do
23+ describe "packHeader" $ do
19 it "returns how much the buffer is consumed (1)" $
24 it "returns how much the buffer is consumed (1)" $
20 tryPackHeader 10 ["foo"] `shouldReturn` 3
25 tryPackHeader 10 ["foo"] `shouldReturn` 3
21 it "returns how much the buffer is consumed (2)" $
26 it "returns how much the buffer is consumed (2)" $
@@ -40,6 +45,13 @@ spec = describe "packHeader" $ do
40 tryPackHeader2 10 ["01234567890", "12"] "0123456789" `shouldReturn` True
45 tryPackHeader2 10 ["01234567890", "12"] "0123456789" `shouldReturn` True
41 it "returns how much the buffer is consumed (6)" $
46 it "returns how much the buffer is consumed (6)" $
42 tryPackHeader2 10 ["012345678901234567890123456789012", "34"] "012345678901234567890123456789" `shouldReturn` True
47 tryPackHeader2 10 ["012345678901234567890123456789012", "34"] "012345678901234567890123456789" `shouldReturn` True
48+ describe "readSendFile" $ do
49+ it "sends a file correctly (1)" $
50+ tryReadSendFile 10 0 1474 ["foo"] `shouldReturn` ExitSuccess
51+ it "sends a file correctly (2)" $
52+ tryReadSendFile 10 0 1474 ["012345678", "901234"] `shouldReturn` ExitSuccess
53+ it "sends a file correctly (3)" $
54+ tryReadSendFile 10 20 100 ["012345678", "901234"] `shouldReturn` ExitSuccess
44tryPackHeader :: Int -> [ByteString] -> IO Int
56tryPackHeader :: Int -> [ByteString] -> IO Int
45tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
57tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
@@ -59,6 +71,25 @@ tryPackHeader2 siz hdrs ans = bracket setup teardown $ \buf -> do
59 send = BS.appendFile outputFile
71 send = BS.appendFile outputFile
60 hook = return ()
72 hook = return ()
74+tryReadSendFile :: Int -> Integer -> Integer -> [ByteString] -> IO ExitCode
75+tryReadSendFile siz off len hdrs = bracket setup teardown $ \buf -> do
76+ mapM_ (BS.appendFile expectedFile) hdrs
77+ copyfile inputFile expectedFile off len
78+ readSendFile buf siz send fid off len hook hdrs
79+ compareFiles expectedFile outputFile
82+ setup = allocateBuffer siz
85+ removeFileIfExists outputFile
86+ removeFileIfExists expectedFile
87+ inputFile = "test/inputFile"
88+ outputFile = "outputFile"
89+ expectedFile = "expectedFile"
90+ fid = FileId inputFile Nothing
91+ send = BS.appendFile outputFile
62checkFile :: FilePath -> ByteString -> IO Bool
93checkFile :: FilePath -> ByteString -> IO Bool
63checkFile path bs = do
94checkFile path bs = do
64 exist <- doesFileExist path
95 exist <- doesFileExist path
@@ -68,6 +99,15 @@ checkFile path bs = do
69 return $ bs == ""
100 return $ bs == ""
102+compareFiles :: FilePath -> FilePath -> IO ExitCode
103+compareFiles file1 file2 = system $ "cmp -s " ++ file1 ++ " " ++ file2
105+copyfile :: FilePath -> FilePath -> Integer -> Integer -> IO ()
106+copyfile src dst off len =
107+ IO.withBinaryFile src IO.ReadMode $ \h -> do
108+ IO.hSeek h IO.AbsoluteSeek off
109+ BS.hGet h (fromIntegral len) >>= BS.appendFile dst
71removeFileIfExists :: FilePath -> IO ()
111removeFileIfExists :: FilePath -> IO ()
72removeFileIfExists file = do
112removeFileIfExists file = do
73 exist <- doesFileExist file
113 exist <- doesFileExist file