File: BodySpec.hs

package info (click to toggle)
haskell-http-client 0.7.17-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 528 kB
  • sloc: haskell: 4,029; makefile: 3
file content (136 lines) | stat: -rw-r--r-- 5,575 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{-# LANGUAGE OverloadedStrings #-}
module Network.HTTP.Client.BodySpec where

import Test.Hspec
import Network.HTTP.Client
import Network.HTTP.Client.Internal
import qualified Data.ByteString as S
import Codec.Compression.GZip (compress)
import qualified Data.ByteString.Lazy as L

main :: IO ()
main = hspec spec

brComplete :: BodyReader -> IO Bool
brComplete brRead' = do
  xs <- brRead'
  return (xs == "")

spec :: Spec
spec = describe "BodySpec" $ do
    it "chunked, single" $ do
        (conn, _, input) <- dummyConnection
            [ "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\nnot consumed"
            ]
        reader <- makeChunkedReader Nothing (return ()) False conn
        body <- brConsume reader
        S.concat body `shouldBe` "hello world"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, single, with trailers" $ do
        (conn, _, input) <- dummyConnection
            [ "5\r\nhello\r\n6\r\n world\r\n0\r\ntrailers-are: ignored\r\nbut: consumed\r\n\r\nnot consumed"
            ]
        reader <- makeChunkedReader Nothing (return ()) False conn
        body <- brConsume reader
        S.concat body `shouldBe` "hello world"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, pieces" $ do
        (conn, _, input) <- dummyConnection $ map S.singleton $ S.unpack
            "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\nnot consumed"
        reader <- makeChunkedReader Nothing (return ()) False conn
        body <- brConsume reader
        S.concat body `shouldBe` "hello world"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, pieces, with trailers" $ do
        (conn, _, input) <- dummyConnection $ map S.singleton $ S.unpack
            "5\r\nhello\r\n6\r\n world\r\n0\r\ntrailers-are: ignored\r\nbut: consumed\r\n\r\nnot consumed"
        reader <- makeChunkedReader Nothing (return ()) False conn
        body <- brConsume reader
        S.concat body `shouldBe` "hello world"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, raw" $ do
        (conn, _, input) <- dummyConnection
            [ "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\nnot consumed"
            ]
        reader <- makeChunkedReader Nothing (return ()) True conn
        body <- brConsume reader
        S.concat body `shouldBe` "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, raw, with trailers" $ do
        (conn, _, input) <- dummyConnection
            [ "5\r\nhello\r\n6\r\n world\r\n0\r\ntrailers-are: returned\r\nin-raw: body\r\n\r\nnot consumed"
            ]
        reader <- makeChunkedReader Nothing (return ()) True conn
        body <- brConsume reader
        S.concat body `shouldBe` "5\r\nhello\r\n6\r\n world\r\n0\r\ntrailers-are: returned\r\nin-raw: body\r\n\r\n"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, pieces, raw" $ do
        (conn, _, input) <- dummyConnection $ map S.singleton $ S.unpack
            "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\nnot consumed"
        reader <- makeChunkedReader Nothing (return ()) True conn
        body <- brConsume reader
        S.concat body `shouldBe` "5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "chunked, pieces, raw, with trailers" $ do
        (conn, _, input) <- dummyConnection $ map S.singleton $ S.unpack
            "5\r\nhello\r\n6\r\n world\r\n0\r\ntrailers-are: returned\r\nin-raw: body\r\n\r\nnot consumed"
        reader <- makeChunkedReader Nothing (return ()) True conn
        body <- brConsume reader
        S.concat body `shouldBe` "5\r\nhello\r\n6\r\n world\r\n0\r\ntrailers-are: returned\r\nin-raw: body\r\n\r\n"
        input' <- input
        S.concat input' `shouldBe` "not consumed"
        brComplete reader `shouldReturn` True

    it "length, single" $ do
        (conn, _, input) <- dummyConnection
            [ "hello world done"
            ]
        reader <- makeLengthReader (return ()) 11 conn
        body <- brConsume reader
        S.concat body `shouldBe` "hello world"
        input' <- input
        S.concat input' `shouldBe` " done"
        brComplete reader `shouldReturn` True

    it "length, pieces" $ do
        (conn, _, input) <- dummyConnection $ map S.singleton $ S.unpack
            "hello world done"
        reader <- makeLengthReader (return ()) 11 conn
        body <- brConsume reader
        S.concat body `shouldBe` "hello world"
        input' <- input
        S.concat input' `shouldBe` " done"
        brComplete reader `shouldReturn` True

    it "gzip" $ do
        let orig = L.fromChunks $ replicate 5000 "Hello world!"
            origZ = compress orig
        (conn, _, input) <- dummyConnection $ L.toChunks origZ ++ ["ignored"]
        reader' <- makeLengthReader (return ()) (fromIntegral $ L.length origZ) conn
        reader <- makeGzipReader reader'
        body <- brConsume reader
        L.fromChunks body `shouldBe` orig
        input' <- input
        S.concat input' `shouldBe` "ignored"
        brComplete reader `shouldReturn` True