on master help sign in
Add the ‘renderUrl’ function
commit6105d7f0f26492cca14357214846d5dac0bedce1 authorSridhar Ratnakumar <srid@srid.ca> dateSat 20 Jun 2020, 21:21 (6 years ago) parent4c0f44c
3 files changed+36
CHANGELOG.md+2 Network/HTTP/Req.hs+14 pure-tests/Network/HTTP/ReqSpec.hs+20
CHANGELOG.md+2
@@ -6,6 +6,8 @@
66* Added the `httpConfigRetryJudgeException` field to `HttpConfig` so that
77 requests that result in exceptions can be retried.
88
9+* Added the function `renderUrl`.
10+
911## Req 3.3.0
1012
1113* Derived `Show` instances for response types `IgnoreResponse`,
Network/HTTP/Req.hs+14
@@ -149,6 +149,7 @@ module Network.HTTP.Req
149149 useHttpsURI,
150150 useURI,
151151 urlQ,
152+ renderUrl,
152153
153154 -- ** Body
154155 -- $body
@@ -929,6 +930,19 @@ infixl 5 /:
929930(/:) :: Url scheme -> Text -> Url scheme
930931(/:) = (/~)
931932
933+-- | Render a 'Url' as 'Text'.
934+--
935+-- @since 3.4.0
936+renderUrl :: Url scheme -> Text
937+renderUrl = \case
938+ Url Https parts ->
939+ "https://" <> renderParts parts
940+ Url Http parts ->
941+ "http://" <> renderParts parts
942+ where
943+ renderParts parts =
944+ T.intercalate "/" (reverse $ NE.toList parts)
945+
932946-- | The 'useHttpURI' function provides an alternative method to get 'Url'
933947-- (possibly with some 'Option's) from a 'URI'. This is useful when you are
934948-- given a URL to query dynamically and don't know it beforehand.
pure-tests/Network/HTTP/ReqSpec.hs+20
@@ -182,6 +182,26 @@ spec = do
182182 lookup "Authorization" (L.requestHeaders requestHttps)
183183 `shouldBe` uriBasicAuth uriHttps
184184
185+ describe "renderUrl" $ do
186+ context "http" $ do
187+ context "empty path" $
188+ it "renders correctly" $ do
189+ let (uriHttp, _) = [urlQ|http://httpbin.org|]
190+ renderUrl uriHttp `shouldBe` "http://httpbin.org"
191+ context "non-empty path" $
192+ it "renders correctly" $ do
193+ let (uriHttp, _) = [urlQ|http://httpbin.org/here/we/go|]
194+ renderUrl uriHttp `shouldBe` "http://httpbin.org/here/we/go"
195+ context "http" $ do
196+ context "empty path" $
197+ it "renders correctly" $ do
198+ let (uriHttp, _) = [urlQ|https://httpbin.org|]
199+ renderUrl uriHttp `shouldBe` "https://httpbin.org"
200+ context "non-empty path" $
201+ it "renders correctly" $ do
202+ let (uriHttp, _) = [urlQ|https://httpbin.org/here/we/go|]
203+ renderUrl uriHttp `shouldBe` "https://httpbin.org/here/we/go"
204+
185205 describe "bodies" $ do
186206 describe "NoReqBody" $
187207 it "sets body to empty byte string" $ do