File: ResponseSpec.hs

package info (click to toggle)
haskell-http-client 0.7.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 532 kB
  • sloc: haskell: 4,091; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 3,034 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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Network.HTTP.Client.ResponseSpec where

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

main :: IO ()
main = hspec spec

spec :: Spec
spec = describe "ResponseSpec" $ do
    let getResponse' conn = getResponse Nothing Nothing Nothing req (dummyManaged conn) Nothing
        req = parseRequest_ "http://localhost"
    it "basic" $ do
        (conn, _, _) <- dummyConnection
            [ "HTTP/1.1 200 OK\r\n"
            , "Key1: Value1\r\n"
            , "Content-length: 11\r\n\r\n"
            , "Hello"
            , " W"
            , "orld\r\nHTTP/1.1"
            ]
        Response {..} <- getResponse' conn
        responseStatus `shouldBe` status200
        responseVersion `shouldBe` HttpVersion 1 1
        responseHeaders `shouldBe`
            [ ("Key1", "Value1")
            , ("Content-length", "11")
            ]
        pieces <- brConsume responseBody
        pieces `shouldBe` ["Hello", " W", "orld"]
    it "no length" $ do
        (conn, _, _) <- dummyConnection
            [ "HTTP/1.1 200 OK\r\n"
            , "Key1: Value1\r\n\r\n"
            , "Hello"
            , " W"
            , "orld\r\nHTTP/1.1"
            ]
        Response {..} <- getResponse' conn
        responseStatus `shouldBe` status200
        responseVersion `shouldBe` HttpVersion 1 1
        responseHeaders `shouldBe`
            [ ("Key1", "Value1")
            ]
        pieces <- brConsume responseBody
        pieces `shouldBe` ["Hello", " W", "orld\r\nHTTP/1.1"]
    it "chunked" $ do
        (conn, _, _) <- dummyConnection
            [ "HTTP/1.1 200 OK\r\n"
            , "Key1: Value1\r\n"
            , "Transfer-encoding: chunked\r\n\r\n"
            , "5\r\nHello\r"
            , "\n2\r\n W"
            , "\r\n4  ignored\r\norld\r\n0\r\n\r\nHTTP/1.1"
            ]
        Response {..} <- getResponse' conn
        responseStatus `shouldBe` status200
        responseVersion `shouldBe` HttpVersion 1 1
        responseHeaders `shouldBe`
            [ ("Key1", "Value1")
            , ("Transfer-encoding", "chunked")
            ]
        pieces <- brConsume responseBody
        pieces `shouldBe` ["Hello", " W", "orld"]
    it "gzip" $ do
        (conn, _, _) <- dummyConnection
            $ "HTTP/1.1 200 OK\r\n"
            : "Key1: Value1\r\n"
            : "Content-Encoding: gzip\r\n\r\n"
            : L.toChunks (compress "Compressed Hello World")
        Response {..} <- getResponse' conn
        responseStatus `shouldBe` status200
        responseVersion `shouldBe` HttpVersion 1 1
        responseHeaders `shouldBe`
            [ ("Key1", "Value1")
            , ("Content-Encoding", "gzip")
            ]
        pieces <- brConsume responseBody
        S.concat pieces `shouldBe` "Compressed Hello World"