Skip to content

Commit

Permalink
handle trailing slashes in HttpUrlBuilder (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed Apr 22, 2019
1 parent d9959ce commit 259717a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Expand Up @@ -121,14 +121,16 @@ public HttpUrlBuilder port(int port) {
@Override
public HttpUrlBuilder encodedPath(String path) {
Objects.requireNonNull(path, "path must not be null");
Arrays.asList(path.split("/")).forEach(pathSegments::add);
hasTrailingSlash = path.endsWith("/");
Arrays.stream(path.split("/")).filter(s -> !s.isEmpty()).forEach(pathSegments::add);
return this;
}

@Override
public HttpUrlBuilder path(String path) {
Objects.requireNonNull(path, "path must not be null");
Arrays.asList(path.split("/")).forEach(this::segment);
hasTrailingSlash = path.endsWith("/");
Arrays.stream(path.split("/")).filter(s -> !s.isEmpty()).forEach(this::segment);
return this;
}

Expand Down
Expand Up @@ -154,4 +154,15 @@ class HttpUrlBuilderSpec extends Specification {
]
}

def "can append trailing slash"() {
expect:
build { path("foo/") } == "http://localhost/foo/"
build { path("foo/").path("/foo/") } == "http://localhost/foo/foo/"
build { path("foo/").params("bar") } == "http://localhost/foo/?bar"
build { path("foo/").path("bar")} == "http://localhost/foo/bar"
HttpUrlBuilder.base("http://localhost/foo/".toURI()).build().toString() == "http://localhost/foo/"
HttpUrlBuilder.base("http://localhost".toURI()).path("foo/").build().toString() == "http://localhost/foo/"
HttpUrlBuilder.base("http://localhost/foo/".toURI()).path("bar").build().toString() == "http://localhost/foo/bar"
}

}

0 comments on commit 259717a

Please sign in to comment.