@@ -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.
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 #-}
43+#if __GLASGOW_HASKELL__ < 710
44+{-# LANGUAGE ConstraintKinds #-}
41module Network.HTTP.ReqSpec
47module Network.HTTP.ReqSpec
@@ -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)
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
67+#if !MIN_VERSION_base(4,8,0)
68+import Control.Applicative
69+import Data.Monoid (mempty)
54spec :: Spec
72spec :: Spec
@@ -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
85+ , HttpBodyAllowed (AllowsBody method) 'NoBody )
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
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
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
144+instance Arbitrary Text where
145+ arbitrary = T.pack <$> arbitrary
95----------------------------------------------------------------------------
147----------------------------------------------------------------------------
96-- Helpers
148-- Helpers
@@ -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
113-- | A dummy 'Url'.
165-- | A dummy 'Url'.
115url :: Url 'Https
167url :: Url 'Https
116url = https "httpbin.org"
168url = https "httpbin.org"
170+urlEncode :: Text -> ByteString
171+urlEncode = Y.urlEncode False . T.encodeUtf8