1{-# LANGUAGE OverloadedStrings #-}
2module Network.HTTP.ClientSpec where
3
4import qualified Data.ByteString.Char8        as BS
5import           Network.HTTP.Client
6import           Network.HTTP.Client.Internal
7import           Network.HTTP.Types        (status200, found302, status405)
8import           Network.HTTP.Types.Status
9import           Test.Hspec
10import           Control.Applicative       ((<$>))
11import           Data.ByteString.Lazy.Char8 () -- orphan instance
12
13main :: IO ()
14main = hspec spec
15
16spec :: Spec
17spec = describe "Client" $ do
18    it "works" $ do
19        req <- parseUrlThrow "http://httpbin.org/"
20        man <- newManager defaultManagerSettings
21        res <- httpLbs req man
22        responseStatus res `shouldBe` status200
23
24    describe "method in URL" $ do
25        it "success" $ do
26            req <- parseUrlThrow "POST http://httpbin.org/post"
27            man <- newManager defaultManagerSettings
28            res <- httpLbs req man
29            responseStatus res `shouldBe` status200
30
31        it "failure" $ do
32            req <- parseRequest "PUT http://httpbin.org/post"
33            man <- newManager defaultManagerSettings
34            res <- httpLbs req man
35            responseStatus res `shouldBe` status405
36    describe "bearer auth" $ do
37        it "success" $ do
38            initialReq <- parseUrlThrow "http://httpbin.org/bearer"
39            let finalReq = applyBearerAuth "token" initialReq
40            man <- newManager defaultManagerSettings
41            res <- httpLbs finalReq man
42            responseStatus res `shouldBe` status200
43        it "failure" $ do
44            req <- parseRequest "http://httpbin.org/bearer"
45            man <- newManager defaultManagerSettings
46            res <- httpLbs req man
47            responseStatus res `shouldBe` status401
48
49    describe "redirects" $ do
50        xit "follows redirects" $ do
51            req <- parseRequest "http://httpbin.org/redirect-to?url=http://httpbin.org"
52            man <- newManager defaultManagerSettings
53            res <- httpLbs req man
54            responseStatus res `shouldBe` status200
55
56        xit "allows to disable redirect following" $ do
57            req <- (\ r -> r{ redirectCount = 0 }) <$>
58              parseRequest "http://httpbin.org/redirect-to?url=http://httpbin.org"
59            man <- newManager defaultManagerSettings
60            res <- httpLbs req man
61            responseStatus res `shouldBe` found302
62
63    context "managerModifyResponse" $ do
64      it "allows to modify the response status code" $ do
65        let modify :: Response BodyReader -> IO (Response BodyReader)
66            modify res = do
67              return res {
68                responseStatus = (responseStatus res) {
69                  statusCode = 201
70                }
71              }
72            settings = defaultManagerSettings { managerModifyResponse = modify }
73        man <- newManager settings
74        res <- httpLbs "http://httpbin.org" man
75        (statusCode.responseStatus) res `shouldBe` 201
76
77      it "modifies the response body" $ do
78        let modify :: Response BodyReader -> IO (Response BodyReader)
79            modify res = do
80              reader <- constBodyReader [BS.pack "modified response body"]
81              return res {
82                responseBody = reader
83              }
84            settings = defaultManagerSettings { managerModifyResponse = modify }
85        man <- newManager settings
86        res <- httpLbs "http://httpbin.org" man
87        responseBody res `shouldBe` "modified response body"
88
89    context "managerModifyRequest" $ do
90        it "port" $ do
91            let modify req = return req { port = 80 }
92                settings = defaultManagerSettings { managerModifyRequest = modify }
93            man <- newManager settings
94            res <- httpLbs "http://httpbin.org:1234" man
95            responseStatus res `shouldBe` status200
96
97        it "checkResponse" $ do
98            let modify req = return req { checkResponse = \_ _ -> error "some exception" }
99                settings = defaultManagerSettings { managerModifyRequest = modify }
100            man <- newManager settings
101            httpLbs "http://httpbin.org" man `shouldThrow` anyException
102
103        xit "redirectCount" $ do
104            let modify req = return req { redirectCount = 0 }
105                settings = defaultManagerSettings { managerModifyRequest = modify }
106            man <- newManager settings
107            response <- httpLbs "http://httpbin.org/redirect-to?url=foo" man
108            responseStatus response `shouldBe` found302
109