@@ -31,13 +31,19 @@
3131-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232-- POSSIBILITY OF SUCH DAMAGE.
3333
34+{-# LANGUAGE CPP #-}
3435{-# LANGUAGE DataKinds #-}
3536{-# LANGUAGE FlexibleInstances #-}
3637{-# LANGUAGE OverloadedStrings #-}
3738{-# LANGUAGE RankNTypes #-}
3839{-# LANGUAGE RecordWildCards #-}
40+{-# LANGUAGE ScopedTypeVariables #-}
3941{-# OPTIONS_GHC -fno-warn-orphans #-}
4042
43+#if __GLASGOW_HASKELL__ < 710
44+{-# LANGUAGE ConstraintKinds #-}
45+#endif
46+
4147module Network.HTTP.ReqSpec
4248 ( spec )
4349where
@@ -45,11 +51,23 @@ where
4551import Control.Exception (throwIO)
4652import Control.Monad.Reader
4753import Data.ByteString (ByteString)
54+import Data.Proxy
55+import Data.Text (Text)
4856import Network.HTTP.Req
4957import Test.Hspec
58+import Test.Hspec.Core.Spec (SpecM)
5059import Test.QuickCheck
51−import qualified Data.ByteString as B
52−import qualified Network.HTTP.Client as L
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
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
5371
5472spec :: Spec
5573spec = do
@@ -59,6 +77,37 @@ spec = do
5977 request <- runReaderT (req_ GET url NoReqBody mempty) config
6078 L.proxy request `shouldBe` httpConfigProxy config
6179 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
62111
63112----------------------------------------------------------------------------
64113-- Instances
@@ -92,6 +141,9 @@ instance Arbitrary L.Proxy where
92141instance Arbitrary ByteString where
93142 arbitrary = B.pack <$> arbitrary
94143
144+instance Arbitrary Text where
145+ arbitrary = T.pack <$> arbitrary
146+
95147----------------------------------------------------------------------------
96148-- Helpers
97149
@@ -108,9 +160,12 @@ req_
108160 -> Option scheme -- ^ Collection of optional parameters
109161 -> m L.Request -- ^ Vanilla request
110162req_ method url' body options =
111− responseRequest <$> req method url' body returnRequest options
163+ responseRequest `liftM` req method url' body returnRequest options
112164
113165-- | A dummy 'Url'.
114166
115167url :: Url 'Https
116168url = https "httpbin.org"
169+
170+urlEncode :: Text -> ByteString
171+urlEncode = Y.urlEncode False . T.encodeUtf8