Skip to content

Commit e401519

Browse files
committed
Expose urlWithQueryString
1 parent 9ee01bc commit e401519

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/Engage/Http.elm

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Engage.Http exposing
22
( Config, Error(..)
33
, get, post, patch, put, delete
44
, requestJson, requestString
5-
, getErrorMessage
5+
, getErrorMessage, urlWithQueryString
66
, configDecoder, serverErrorDecoder, multipleServerErrorDecoder, nullDecoder
77
)
88

@@ -26,7 +26,7 @@ module Engage.Http exposing
2626
2727
# Helper functions
2828
29-
@docs getErrorMessage
29+
@docs getErrorMessage, urlWithQueryString
3030
3131
3232
# Decoders
@@ -291,6 +291,29 @@ ensureStringEndsWithSlash baseUrl =
291291
baseUrl ++ "/"
292292

293293

294+
{-| Get the full URL from a base URL, a method name (i.e. a route), and query string values.
295+
296+
The function adds a trailing slash to the base URL if missing. Query string values are URL encoded.
297+
298+
config : Engage.Http.Config
299+
config =
300+
{ baseUrl = "https://example.com/API/MyModule"
301+
, headers = []
302+
}
303+
304+
Engage.Http.urlWithQueryString config.baseUrl "" []
305+
--> "https://example.com/API/MyModule/"
306+
307+
Engage.Http.urlWithQueryString config.baseUrl "health" []
308+
--> "https://example.com/API/MyModule/health"
309+
310+
Engage.Http.urlWithQueryString config.baseUrl "articles" [ ( "page", "1" ) ]
311+
--> "https://example.com/API/MyModule/articles?page=1"
312+
313+
Engage.Http.urlWithQueryString config.baseUrl "author" [ ( "name", "Joël Quenneville" ) ]
314+
--> "https://example.com/API/MyModule/author?name=Jo%C3%ABl%20Quenneville"
315+
316+
-}
294317
urlWithQueryString : String -> String -> List ( String, String ) -> String
295318
urlWithQueryString baseUrl methodName queryStringParams =
296319
queryStringParams

tests/Tests.elm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ suite =
1616
, nullDecoderTests
1717
, serverErrorDecoderTests
1818
, getErrorMessageTests
19+
, urlWithQueryStringTests
1920
]
2021

2122

@@ -237,3 +238,65 @@ getErrorMessageTests =
237238
|> Engage.Http.getErrorMessage { localization = Engage.Localization.empty }
238239
|> Expect.equal "It wasn't found"
239240
]
241+
242+
243+
urlWithQueryStringTests : Test
244+
urlWithQueryStringTests =
245+
describe "urlWithQueryString"
246+
[ test "empty" <|
247+
\_ ->
248+
Engage.Http.urlWithQueryString "" "" []
249+
|> Expect.equal "/"
250+
, test "relative base" <|
251+
\_ ->
252+
Engage.Http.urlWithQueryString "/api/" "" []
253+
|> Expect.equal "/api/"
254+
, test "relative base ensures ends with slash" <|
255+
\_ ->
256+
Engage.Http.urlWithQueryString "/api" "" []
257+
|> Expect.equal "/api/"
258+
, test "relative base with path separated by slash" <|
259+
\_ ->
260+
Engage.Http.urlWithQueryString "/api" "cat/blog" []
261+
|> Expect.equal "/api/cat/blog"
262+
, test "relative multi-part base with path separated by slash" <|
263+
\_ ->
264+
Engage.Http.urlWithQueryString "/DesktopModules/MyCompany/API/MyModule" "cat/blog" []
265+
|> Expect.equal "/DesktopModules/MyCompany/API/MyModule/cat/blog"
266+
, test "absolute base" <|
267+
\_ ->
268+
Engage.Http.urlWithQueryString "https://example.com/" "" []
269+
|> Expect.equal "https://example.com/"
270+
, test "absolute base ensures ends with slash" <|
271+
\_ ->
272+
Engage.Http.urlWithQueryString "https://example.com" "" []
273+
|> Expect.equal "https://example.com/"
274+
, test "absolute base with path separated by slash" <|
275+
\_ ->
276+
Engage.Http.urlWithQueryString "https://example.com" "cat/blog" []
277+
|> Expect.equal "https://example.com/cat/blog"
278+
, test "absolute multi-part base with path separated by slash" <|
279+
\_ ->
280+
Engage.Http.urlWithQueryString "https://example.com/DesktopModules/MyCompany/API/MyModule" "cat/blog" []
281+
|> Expect.equal "https://example.com/DesktopModules/MyCompany/API/MyModule/cat/blog"
282+
, test "no methodName - single query-string added with question mark" <|
283+
\_ ->
284+
Engage.Http.urlWithQueryString "https://example.com/API/MyModule" "" [ ( "page", "1" ) ]
285+
|> Expect.equal "https://example.com/API/MyModule/?page=1"
286+
, test "with methodName - single query-string added with question mark" <|
287+
\_ ->
288+
Engage.Http.urlWithQueryString "https://example.com/API/MyModule" "elevate" [ ( "page", "1" ) ]
289+
|> Expect.equal "https://example.com/API/MyModule/elevate?page=1"
290+
, test "no methodName - many query-strings" <|
291+
\_ ->
292+
Engage.Http.urlWithQueryString "https://example.com/API/MyModule" "" [ ( "page", "1" ), ( "name", "fang" ), ( "type", "page" ), ( "author", "b" ), ( "choice", "0" ) ]
293+
|> Expect.equal "https://example.com/API/MyModule/?page=1&name=fang&type=page&author=b&choice=0"
294+
, test "with methodName - many query-strings" <|
295+
\_ ->
296+
Engage.Http.urlWithQueryString "https://example.com/API/MyModule" "elevate" [ ( "name", "fang" ), ( "type", "page" ), ( "author", "b" ), ( "choice", "0" ) ]
297+
|> Expect.equal "https://example.com/API/MyModule/elevate?name=fang&type=page&author=b&choice=0"
298+
, test "query string values encoded" <|
299+
\_ ->
300+
Engage.Http.urlWithQueryString "https://example.com/" "" [ ( "name", "Brené Brown" ), ( "type", "one & done" ), ( "other", "b (or c)" ) ]
301+
|> Expect.equal "https://example.com/?name=Bren%C3%A9%20Brown&type=one%20%26%20done&other=b%20(or%20c)"
302+
]

0 commit comments

Comments
 (0)