diff --git a/src/main/java/com/widen/urlbuilder/UrlBuilder.java b/src/main/java/com/widen/urlbuilder/UrlBuilder.java index 9a4cc8c..209355e 100644 --- a/src/main/java/com/widen/urlbuilder/UrlBuilder.java +++ b/src/main/java/com/widen/urlbuilder/UrlBuilder.java @@ -20,6 +20,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -46,13 +47,13 @@ public class UrlBuilder { private int port; - private List path = new ArrayList(); + private List path = new ArrayList<>(); private boolean trailingPathSlash = false; private String fragment; - List queryParams = new ArrayList(); + List queryParams = new ArrayList<>(); private GenerationMode mode = GenerationMode.HOSTNAME_RELATIVE; @@ -134,7 +135,6 @@ public UrlBuilder(String hostname, int port, String path) { mode = GenerationMode.FULLY_QUALIFIED; } - public boolean isSslEnabled() { return ssl; } @@ -147,10 +147,24 @@ public String getHostname() { return hostname; } + /** + * Get the full path string of the URL. + * + * @return The URL path. + */ public String getPath() { return "/" + StringUtilsInternal.join(path, "/"); } + /** + * Get the path segments in the URL. This effectively returns the path split on "/" in an efficient way. + * + * @return A read-only list of path segments. + */ + public List getPathSegments() { + return Collections.unmodifiableList(path); + } + public String getFragment() { return fragment; } diff --git a/src/test/groovy/com/widen/urlbuilder/UrlBuilderSpec.groovy b/src/test/groovy/com/widen/urlbuilder/UrlBuilderSpec.groovy index ce30db6..e84de2d 100644 --- a/src/test/groovy/com/widen/urlbuilder/UrlBuilderSpec.groovy +++ b/src/test/groovy/com/widen/urlbuilder/UrlBuilderSpec.groovy @@ -30,6 +30,21 @@ class UrlBuilderSpec extends Specification { ] } + def "Path segments are parsed correctly"() { + when: + def builder = new UrlBuilder(url) + + then: + builder.path == path + builder.pathSegments == segments + + where: + url | path | segments + "http://my.host.com" | "/" | [] + "http://my.host.com/foo/bar" | "/foo/bar" | ["foo", "bar"] + "http://my.host.com/foo//bar/" | "/foo/bar" | ["foo", "bar"] + } + def "Query parameters as map"() { when: def builder = new UrlBuilder('https://my.host.com/bar?a=x&b=2&c=3&c=4&a&d#foo')