on master help sign in
adding test cases for readSendFile.
commit229737f63da11be5fc7c07ddb3e35c8f02cffc3b authorKazu Yamamoto <kazu@iij.ad.jp> dateFri 12 Jun 2015, 14:37 (11 years ago) parent8dab1e5
3 files changed+142−1
warp/test/SendFileSpec.hs+41−1 warp/test/inputFile+100 warp/warp.cabal+1
warp/test/SendFileSpec.hs+41−1
@@ -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
warp/test/inputFilenew file+100
@@ -0,0 +1,100 @@
1+A acid
2+abacus major
3+abacus pythagoricus
4+A battery
5+abbey counter
6+abbey laird
7+abbey lands
8+abbey lubber
9+abbot cloth
10+Abbott papyrus
11+abb wool
12+A-b-c book
13+A-b-c method
14+abdomino-uterotomy
15+Abdul-baha
16+a-be
17+aberrant duct
18+aberration constant
19+abiding place
20+able-bodied
21+able-bodiedness
22+able-minded
23+able-mindedness
24+able seaman
25+aboli fruit
26+A bond
27+Abor-miri
28+a-borning
29+about-face
30+about ship
31+about-sledge
32+above-cited
33+above-found
34+above-given
35+above-mentioned
36+above-named
37+above-quoted
38+above-reported
39+above-said
40+above-water
41+above-written
42+Abraham-man
43+abraum salts
44+abraxas stone
45+Abri audit culture
46+abruptly acuminate
47+abruptly pinnate
48+absciss layer
49+absence state
50+absentee voting
51+absent-minded
52+absent-mindedly
53+absent-mindedness
54+absent treatment
55+absent voter
56+Absent voting
57+absinthe green
58+absinthe oil
59+absorption bands
60+absorption circuit
61+absorption coefficient
62+absorption current
63+absorption dynamometer
64+absorption factor
65+absorption lines
66+absorption pipette
67+absorption screen
68+absorption spectrum
69+absorption system
70+A b station
71+abstinence theory
72+abstract group
73+Abt system
74+abundance declaree
75+aburachan seed
76+abutment arch
77+abutment pier
78+abutting joint
79+acacia veld
80+academy blue
81+academy board
82+academy figure
83+acajou balsam
84+acanthosis nigricans
85+acanthus family
86+acanthus leaf
87+acaroid resin
88+Acca larentia
89+acceleration note
90+accelerator nerve
91+accent mark
92+acceptance bill
93+acceptance house
94+acceptance supra protest
95+acceptor supra protest
96+accession book
97+accession number
98+accession service
99+access road
100+accident insurance
warp/warp.cabal+1
@@ -147,6 +147,7 @@ Test-Suite spec
147147 , vault
148148 , stm >= 2.3
149149 , directory
150+ , process
150151 if flag(use-bytestring-builder)
151152 Build-Depends: bytestring < 0.10.2.0
152153 , bytestring-builder