on master help sign in
adding test cases for readSendFile.
commit02c4ffeab258dba13219168a920f60930b4cc55d authorKazu Yamamoto <kazu@iij.ad.jp> dateFri 12 Jun 2015, 10:53 (11 years ago) parentd9c6676
2 files changed+41
warp/test/SendFileSpec.hs+40 warp/warp.cabal+1
warp/test/SendFileSpec.hs+40
@@ -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
warp/warp.cabal+1
@@ -146,6 +146,7 @@ Test-Suite spec
146146 , async
147147 , vault
148148 , stm >= 2.3
149+ , directory
149150 if flag(use-bytestring-builder)
150151 Build-Depends: bytestring < 0.10.2.0
151152 , bytestring-builder