From d6de7e83747607a1eb1d5c4f1149907e4dde8eef Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Thu, 5 Apr 2018 16:11:25 -0400 Subject: [PATCH] Preserve the trailing slash on paths when building URIs Paths with and without trailing slashes are distinct, therefore a trailing slash in the path should be preserved when building the URI. --- src/urilib.erl | 6 +++++- test/urilib_tests.erl | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/urilib.erl b/src/urilib.erl index 755cc93..0ad95e7 100644 --- a/src/urilib.erl +++ b/src/urilib.erl @@ -275,7 +275,11 @@ url_add_path(undefined, URL) -> url_add_path(Path, URL) -> Escaped = string:join([url_escape_path_segment(P) || P <- string:tokens(Path, "/")], "/"), - string:join([URL, Escaped], "/"). + Joined = string:join([URL, Escaped], "/"), + case lists:suffix("/", Path) of + true -> string:concat(Joined, "/"); + false -> Joined + end. -spec url_escape_path_segment(string()) -> string(). diff --git a/test/urilib_tests.erl b/test/urilib_tests.erl index 3c5f84a..e9033a2 100644 --- a/test/urilib_tests.erl +++ b/test/urilib_tests.erl @@ -57,6 +57,11 @@ build_variation10_test() -> Expect = "http://www.google.com/#foo", ?assertEqual(Expect, urilib:build(Params)). +build_uri_path_with_trailing_slash_test() -> + Params = {https, undefined, "www.example.com", 443, "/foo/", undefined, undefined}, + Expect = "https://www.example.com/foo/", + ?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", @@ -118,6 +123,11 @@ parse_uri_test() -> [{"heartbeat", "5"}], undefined}, ?assertEqual(Expect, urilib:parse(URI, uri)). +parse_uri_with_trailing_slash_test() -> + URI = "https://www.example.com/foo/", + Expect = {https, {undefined, "www.example.com", 443}, "/foo/", undefined, undefined}, + ?assertEqual(Expect, urilib:parse(URI)). + parse_url_variation1_test() -> URI = "amqp://guest:password@rabbitmq:5672/%2f?heartbeat=5&foo=bar&baz+corgie=qux+grault", Expect = {amqp, "guest", "password", "rabbitmq", 5672, "/%2f",