Fix extra slash being added on root path URIs

The following bug was introduced in d6de7e83:

{https, undefined, "www.example.com", 443, "/", undefined, undefined},
  was being built as
"https://www.example.com//"

This corrects the issue by not appending a slash when the root
path ("/") is specified (it should behave the same as passing an
empty ("") path).
This commit is contained in:
Correl Roush 2018-04-13 11:49:54 -04:00
parent abb70de724
commit a4e0458534
2 changed files with 11 additions and 1 deletions

View file

@ -274,7 +274,7 @@ url_add_path(undefined, URL) ->
url_add_path(Path, URL) ->
Escaped = string:join([url_escape_path_segment(P) || P <- string:tokens(Path, "/")], "/"),
Joined = string:join([URL, Escaped], "/"),
case lists:suffix("/", Path) of
case Path /= "/" andalso lists:suffix("/", Path) of
true -> string:concat(Joined, "/");
false -> Joined
end.

View file

@ -62,6 +62,16 @@ build_uri_path_with_trailing_slash_test() ->
Expect = "https://www.example.com/foo/",
?assertEqual(Expect, urilib:build(Params)).
build_uri_path_with_root_path_test() ->
Params = {https, undefined, "www.example.com", 443, "/", undefined, undefined},
Expect = "https://www.example.com/",
?assertEqual(Expect, urilib:build(Params)).
build_uri_path_with_no_path_test() ->
Params = {https, undefined, "www.example.com", 443, "", undefined, undefined},
Expect = "https://www.example.com/",
?assertEqual(Expect, urilib:build(Params)).
build_url_variation1_test() ->
Params = {amqp, "guest", "password", "rabbitmq", 5672, "/%2f", [{"heartbeat", "5"}], undefined},
Expect = "amqp://guest:password@rabbitmq:5672/%2f?heartbeat=5",