on master help sign in
Add more tests to ‘pure-tests’
8d32ab5mrkkrp
pure-tests/Network/HTTP/ReqSpec.hs+58−3
@@ -31,13 +31,19 @@
31-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32-- POSSIBILITY OF SUCH DAMAGE.
32-- POSSIBILITY OF SUCH DAMAGE.
33
33
34+{-# LANGUAGE CPP #-}
34{-# LANGUAGE DataKinds #-}
35{-# LANGUAGE DataKinds #-}
35{-# LANGUAGE FlexibleInstances #-}
36{-# LANGUAGE FlexibleInstances #-}
36{-# LANGUAGE OverloadedStrings #-}
37{-# LANGUAGE OverloadedStrings #-}
37{-# LANGUAGE RankNTypes #-}
38{-# LANGUAGE RankNTypes #-}
38{-# LANGUAGE RecordWildCards #-}
39{-# LANGUAGE RecordWildCards #-}
40+{-# LANGUAGE ScopedTypeVariables #-}
39{-# OPTIONS_GHC -fno-warn-orphans #-}
41{-# OPTIONS_GHC -fno-warn-orphans #-}
40
42
43+#if __GLASGOW_HASKELL__ < 710
44+{-# LANGUAGE ConstraintKinds #-}
45+#endif
46+
41module Network.HTTP.ReqSpec
47module Network.HTTP.ReqSpec
42 ( spec )
48 ( spec )
43where
49where
@@ -45,11 +51,23 @@ where
45import Control.Exception (throwIO)
51import Control.Exception (throwIO)
46import Control.Monad.Reader
52import Control.Monad.Reader
47import Data.ByteString (ByteString)
53import Data.ByteString (ByteString)
54+import Data.Proxy
55+import Data.Text (Text)
48import Network.HTTP.Req
56import Network.HTTP.Req
49import Test.Hspec
57import Test.Hspec
58+import Test.Hspec.Core.Spec (SpecM)
50import Test.QuickCheck
59import Test.QuickCheck
51−import qualified Data.ByteString as B
60+import qualified Data.ByteString as B
61+import qualified Data.ByteString.Char8 as B8
62+import qualified Data.Text as T
63+import qualified Data.Text.Encoding as T
52−import qualified Network.HTTP.Client as L
64+import qualified Network.HTTP.Client as L
65+import qualified Network.HTTP.Types as Y
66+
67+#if !MIN_VERSION_base(4,8,0)
68+import Control.Applicative
69+import Data.Monoid (mempty)
70+#endif
53
71
54spec :: Spec
72spec :: Spec
55spec = do
73spec = do
@@ -59,6 +77,37 @@ spec = do
59 request <- runReaderT (req_ GET url NoReqBody mempty) config
77 request <- runReaderT (req_ GET url NoReqBody mempty) config
60 L.proxy request `shouldBe` httpConfigProxy config
78 L.proxy request `shouldBe` httpConfigProxy config
61 L.redirectCount request `shouldBe` httpConfigRedirectCount config
79 L.redirectCount request `shouldBe` httpConfigRedirectCount config
80+ describe "methods" $ do
81+ let -- 'mnth' = method name test helper
82+ mnth
83+ :: forall method.
84+ ( HttpMethod method
85+ , HttpBodyAllowed (AllowsBody method) 'NoBody )
86+ => method
87+ -> SpecM () ()
88+ mnth method = do
89+ let name = httpMethodName (Proxy :: Proxy method)
90+ describe (B8.unpack name) $ do
91+ it "affects name of HTTP method" $ do
92+ request <- req_ method url NoReqBody mempty
93+ L.method request `shouldBe` name
94+ mnth GET
95+ mnth POST
96+ mnth HEAD
97+ mnth PUT
98+ mnth DELETE
99+ mnth TRACE
100+ mnth CONNECT
101+ mnth OPTIONS
102+ mnth PATCH
103+ describe "urls" $ do
104+ describe "http" $ do
105+ it "sets all the params" $ do
106+ property $ \host -> do
107+ request <- req_ GET (http host) NoReqBody mempty
108+ L.secure request `shouldBe` False
109+ L.port request `shouldBe` 80
110+ L.host request `shouldBe` urlEncode host
62
111
63----------------------------------------------------------------------------
112----------------------------------------------------------------------------
64-- Instances
113-- Instances
@@ -92,6 +141,9 @@ instance Arbitrary L.Proxy where
92instance Arbitrary ByteString where
141instance Arbitrary ByteString where
93 arbitrary = B.pack <$> arbitrary
142 arbitrary = B.pack <$> arbitrary
94
143
144+instance Arbitrary Text where
145+ arbitrary = T.pack <$> arbitrary
146+
95----------------------------------------------------------------------------
147----------------------------------------------------------------------------
96-- Helpers
148-- Helpers
97
149
@@ -108,9 +160,12 @@ req_
108 -> Option scheme -- ^ Collection of optional parameters
160 -> Option scheme -- ^ Collection of optional parameters
109 -> m L.Request -- ^ Vanilla request
161 -> m L.Request -- ^ Vanilla request
110req_ method url' body options =
162req_ method url' body options =
111− responseRequest <$> req method url' body returnRequest options
163+ responseRequest `liftM` req method url' body returnRequest options
112
164
113-- | A dummy 'Url'.
165-- | A dummy 'Url'.
114
166
115url :: Url 'Https
167url :: Url 'Https
116url = https "httpbin.org"
168url = https "httpbin.org"
169+
170+urlEncode :: Text -> ByteString
171+urlEncode = Y.urlEncode False . T.encodeUtf8
req.cabal+3
@@ -90,9 +90,12 @@ test-suite pure-tests
90 , base >= 4.7 && < 5.0
90 , base >= 4.7 && < 5.0
91 , bytestring >= 0.10.8 && < 0.11
91 , bytestring >= 0.10.8 && < 0.11
92 , hspec >= 2.0 && < 3.0
92 , hspec >= 2.0 && < 3.0
93+ , hspec-core >= 2.0 && < 3.0
93 , http-client >= 0.5 && < 0.6
94 , http-client >= 0.5 && < 0.6
95+ , http-types >= 0.8 && < 10.0
94 , mtl >= 2.0 && < 3.0
96 , mtl >= 2.0 && < 3.0
95 , req >= 0.1.0
97 , req >= 0.1.0
98+ , text >= 0.2 && < 1.3
96 if flag(dev)
99 if flag(dev)
97 ghc-options: -Wall -Werror
100 ghc-options: -Wall -Werror
98 else
101 else