@@ -8,14 +8,19 @@ import Data.ByteString (ByteString)
88import qualified Data.ByteString as BS
99import Network.Wai.Handler.Warp.Buffer
1010import Network.Wai.Handler.Warp.SendFile
11+import Network.Wai.Handler.Warp.Types
1112import System.Directory
13+import System.Exit
14+import qualified System.IO as IO
15+import System.Process (system)
1216import Test.Hspec
1317
1418main :: IO ()
1519main = hspec spec
1620
1721spec :: Spec
18−spec = describe "packHeader" $ do
22+spec = do
23+ describe "packHeader" $ do
1924 it "returns how much the buffer is consumed (1)" $
2025 tryPackHeader 10 ["foo"] `shouldReturn` 3
2126 it "returns how much the buffer is consumed (2)" $
@@ -40,6 +45,13 @@ spec = describe "packHeader" $ do
4045 tryPackHeader2 10 ["01234567890", "12"] "0123456789" `shouldReturn` True
4146 it "returns how much the buffer is consumed (6)" $
4247 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
4355
4456tryPackHeader :: Int -> [ByteString] -> IO Int
4557tryPackHeader siz hdrs = bracket (allocateBuffer siz) freeBuffer $ \buf ->
@@ -59,6 +71,25 @@ tryPackHeader2 siz hdrs ans = bracket setup teardown $ \buf -> do
5971 send = BS.appendFile outputFile
6072 hook = return ()
6173
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
80+ where
81+ hook = return ()
82+ setup = allocateBuffer siz
83+ teardown buf = do
84+ freeBuffer buf
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
92+
6293checkFile :: FilePath -> ByteString -> IO Bool
6394checkFile path bs = do
6495 exist <- doesFileExist path
@@ -68,6 +99,15 @@ checkFile path bs = do
6899 else
69100 return $ bs == ""
70101
102+compareFiles :: FilePath -> FilePath -> IO ExitCode
103+compareFiles file1 file2 = system $ "cmp -s " ++ file1 ++ " " ++ file2
104+
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
110+
71111removeFileIfExists :: FilePath -> IO ()
72112removeFileIfExists file = do
73113 exist <- doesFileExist file