mirror of
https://github.com/correl/urilib.git
synced 2024-11-30 19:19:56 +00:00
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.
This commit is contained in:
parent
a24fdea043
commit
d6de7e8374
2 changed files with 15 additions and 1 deletions
|
@ -275,7 +275,11 @@ url_add_path(undefined, URL) ->
|
||||||
|
|
||||||
url_add_path(Path, URL) ->
|
url_add_path(Path, URL) ->
|
||||||
Escaped = string:join([url_escape_path_segment(P) || P <- string:tokens(Path, "/")], "/"),
|
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().
|
-spec url_escape_path_segment(string()) -> string().
|
||||||
|
|
|
@ -57,6 +57,11 @@ build_variation10_test() ->
|
||||||
Expect = "http://www.google.com/#foo",
|
Expect = "http://www.google.com/#foo",
|
||||||
?assertEqual(Expect, urilib:build(Params)).
|
?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() ->
|
build_url_variation1_test() ->
|
||||||
Params = {amqp, "guest", "password", "rabbitmq", 5672, "/%2f", [{"heartbeat", "5"}], undefined},
|
Params = {amqp, "guest", "password", "rabbitmq", 5672, "/%2f", [{"heartbeat", "5"}], undefined},
|
||||||
Expect = "amqp://guest:password@rabbitmq:5672/%2f?heartbeat=5",
|
Expect = "amqp://guest:password@rabbitmq:5672/%2f?heartbeat=5",
|
||||||
|
@ -118,6 +123,11 @@ parse_uri_test() ->
|
||||||
[{"heartbeat", "5"}], undefined},
|
[{"heartbeat", "5"}], undefined},
|
||||||
?assertEqual(Expect, urilib:parse(URI, uri)).
|
?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() ->
|
parse_url_variation1_test() ->
|
||||||
URI = "amqp://guest:password@rabbitmq:5672/%2f?heartbeat=5&foo=bar&baz+corgie=qux+grault",
|
URI = "amqp://guest:password@rabbitmq:5672/%2f?heartbeat=5&foo=bar&baz+corgie=qux+grault",
|
||||||
Expect = {amqp, "guest", "password", "rabbitmq", 5672, "/%2f",
|
Expect = {amqp, "guest", "password", "rabbitmq", 5672, "/%2f",
|
||||||
|
|
Loading…
Reference in a new issue