Skip to content
Browse files

#56 - LinkBuilderSupport.slash(…) now handles fragments correctly.

  • Loading branch information...
1 parent db4f96a commit 2c49f397da4dd8cba8f98d187b68ae27631e4ae6 @olivergierke olivergierke committed Mar 8, 2013
View
9 src/main/java/org/springframework/hateoas/core/LinkBuilderSupport.java
@@ -62,6 +62,10 @@ public T slash(Object object) {
String path = object.toString();
+ if (path.endsWith("#")) {
+ path = path.substring(0, path.length() - 1);
+ }
+
if (!StringUtils.hasText(path)) {
return getThis();
}
@@ -73,6 +77,11 @@ public T slash(Object object) {
builder.pathSegment(pathSegment);
}
+ String fragment = components.getFragment();
+ if (StringUtils.hasText(fragment)) {
+ builder.fragment(fragment);
+ }
+
return createNewInstance(builder.query(components.getQuery()));
}
View
14 src/test/java/org/springframework/hateoas/core/LinkBuilderSupportUnitTest.java
@@ -36,6 +36,20 @@ public void callingSlashWithEmptyStringIsNoOp() {
assertThat(builder.slash(""), is(builder));
}
+ @Test
+ public void appendsFragmentCorrectly() {
+
+ SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance());
+ builder = builder.slash("foo#bar");
+ assertThat(builder.toString(), endsWith("foo#bar"));
+ builder = builder.slash("bar");
+ assertThat(builder.toString(), endsWith("foo/bar#bar"));
+ builder = builder.slash("#foo");
+ assertThat(builder.toString(), endsWith("foo/bar#foo"));
+ builder = builder.slash("#");
+ assertThat(builder.toString(), endsWith("foo/bar#foo"));
+ }
+
static class SampleLinkBuilder extends LinkBuilderSupport<SampleLinkBuilder> {
public SampleLinkBuilder(UriComponentsBuilder builder) {

0 comments on commit 2c49f39

Please sign in to comment.
Something went wrong with that request. Please try again.