From a4e045853443c248d1e129cbe1b9428f48508a38 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Fri, 13 Apr 2018 11:49:54 -0400 Subject: [PATCH] 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). --- src/urilib.erl | 2 +- test/urilib_tests.erl | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/urilib.erl b/src/urilib.erl index fb25df8..e0a30d9 100644 --- a/src/urilib.erl +++ b/src/urilib.erl @@ -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. diff --git a/test/urilib_tests.erl b/test/urilib_tests.erl index e9033a2..254ab9d 100644 --- a/test/urilib_tests.erl +++ b/test/urilib_tests.erl @@ -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",