~ubuntu-branches/ubuntu/wily/haskell-wai/wily

« back to all changes in this revision

Viewing changes to test/Network/WaiSpec.hs

  • Committer: Package Import Robot
  • Author(s): Clint Adams
  • Date: 2014-06-06 11:37:52 UTC
  • mfrom: (12.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140606113752-7y3jlw020luj42yn
Tags: 3.0.0-1
* New upstream version.
* Enable testsuite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Network.WaiSpec (spec) where
 
2
 
 
3
import Test.Hspec
 
4
import Test.Hspec.QuickCheck (prop)
 
5
import Network.Wai
 
6
import Network.Wai.Internal (Request (Request))
 
7
import Data.IORef
 
8
import Data.Monoid
 
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_)
 
13
 
 
14
spec :: Spec
 
15
spec = do
 
16
    describe "responseToStream" $ do
 
17
        let getBody res = 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, ())
 
24
                        flush :: IO ()
 
25
                        flush = return ()
 
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
 
40
            let fp = "wai.cabal"
 
41
            body <- getBody $ responseFile undefined undefined fp Nothing
 
42
            expected <- S.readFile fp
 
43
            body `shouldBe` expected
 
44
        prop "responseFile partial" $ \offset' count' -> do
 
45
            let fp = "wai.cabal"
 
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
 
54
                }
 
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
 
60
            let req = Request
 
61
                        { requestBody = atomicModifyIORef ref $ \bss ->
 
62
                            case bss of
 
63
                                [] -> ([], S.empty)
 
64
                                x:y -> (y, x)
 
65
                        }
 
66
            body <- lazyRequestBody req
 
67
            body `shouldBe` L.fromChunks (map S.pack chunks)
 
68
        it "is lazy" $ do
 
69
            let req = Request
 
70
                        { requestBody = error "requestBody"
 
71
                        }
 
72
            _ <- lazyRequestBody req
 
73
            return ()