1
module Network.WaiSpec (spec) where
4
import Test.Hspec.QuickCheck (prop)
6
import Network.Wai.Internal (Request (Request))
9
import qualified Data.ByteString as S
10
import qualified Data.ByteString.Lazy as L
11
import Blaze.ByteString.Builder (toByteString, Builder, fromWord8)
12
import Control.Monad (forM_)
16
describe "responseToStream" $ do
18
let (_, _, f) = responseToStream res
19
f $ \streamingBody -> do
20
builderRef <- newIORef mempty
21
let add :: Builder -> IO ()
22
add b = atomicModifyIORef builderRef $ \builder ->
23
(builder `mappend` b, ())
26
streamingBody add flush
27
fmap toByteString $ readIORef builderRef
28
prop "responseLBS" $ \bytes -> do
29
body <- getBody $ responseLBS undefined undefined $ L.pack bytes
30
body `shouldBe` S.pack bytes
31
prop "responseBuilder" $ \bytes -> do
32
body <- getBody $ responseBuilder undefined undefined
33
$ mconcat $ map fromWord8 bytes
34
body `shouldBe` S.pack bytes
35
prop "responseStream" $ \chunks -> do
36
body <- getBody $ responseStream undefined undefined $ \sendChunk _ ->
37
forM_ chunks $ \chunk -> sendChunk $ mconcat $ map fromWord8 chunk
38
body `shouldBe` S.concat (map S.pack chunks)
39
it "responseFile total" $ do
41
body <- getBody $ responseFile undefined undefined fp Nothing
42
expected <- S.readFile fp
43
body `shouldBe` expected
44
prop "responseFile partial" $ \offset' count' -> do
46
totalBS <- S.readFile fp
47
let total = S.length totalBS
48
offset = abs offset' `mod` total
49
count = abs count' `mod` (total - offset)
50
body <- getBody $ responseFile undefined undefined fp $ Just FilePart
51
{ filePartOffset = fromIntegral offset
52
, filePartByteCount = fromIntegral count
53
, filePartFileSize = fromIntegral total
55
let expected = S.take count $ S.drop offset totalBS
56
body `shouldBe` expected
57
describe "lazyRequestBody" $ do
58
prop "works" $ \chunks -> do
59
ref <- newIORef $ map S.pack $ filter (not . null) chunks
61
{ requestBody = atomicModifyIORef ref $ \bss ->
66
body <- lazyRequestBody req
67
body `shouldBe` L.fromChunks (map S.pack chunks)
70
{ requestBody = error "requestBody"
72
_ <- lazyRequestBody req