on master help sign in
adding test cases for readSendFile.
02c4ffeKazu Yamamoto
warp/test/SendFileSpec.hs+40
@@ -3,9 +3,12 @@
3module SendFileSpec where
3module SendFileSpec where
4
4
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
10
13
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
28
43
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 ->
32 where
47 where
33 send _ = return ()
48 send _ = return ()
34 hook = return ()
49 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
146 , async
146 , async
147 , vault
147 , vault
148 , stm >= 2.3
148 , stm >= 2.3
149+ , directory
149 if flag(use-bytestring-builder)
150 if flag(use-bytestring-builder)
150 Build-Depends: bytestring < 0.10.2.0
151 Build-Depends: bytestring < 0.10.2.0
151 , bytestring-builder
152 , bytestring-builder