DATACMNS-515 - Links for PagedResources should be canonical.

PagedResourcesAssembler now creates the self, prev and next links for a PagedResource to be canonical and not contain any template parameters. This is to make sure a client can not actually tweak the links and follow a link that's not strictly conforming to semantics of the rel.
This commit is contained in:
Oliver Gierke
2015-05-14 14:06:35 +02:00
parent 5b60d487e1
commit 6ebb05e606
2 changed files with 24 additions and 39 deletions

View File

@@ -129,15 +129,16 @@ public class PagedResourcesAssemblerUnitTests {
/**
* @see DATACMNS-418
* @see DATACMNS-515
*/
@Test
public void addsSelfLinkWithPaginationTemplateVariables() {
public void createsACanonicalLinkWithoutTemplateParameters() {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
Link selfLink = resources.getLink(Link.REL_SELF);
assertThat(selfLink.getHref(), endsWith("{?page,size,sort}"));
assertThat(selfLink.getHref(), endsWith("localhost"));
}
/**
@@ -158,33 +159,6 @@ public class PagedResourcesAssemblerUnitTests {
assertThat(content.iterator().next().name, is("Dave"));
}
/**
* @see DATACMNS-418
*/
@Test
public void appendsMissingTemplateParametersToLink() {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
Link link = new Link("/foo?page=0");
assertThat(assembler.appendPaginationParameterTemplates(link), is(new Link("/foo?page=0{&size,sort}")));
}
/**
* @see DATACMNS-519
*/
@Test
public void keepsExistingTemplateVariablesFromBaseLink() {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
Link link = new Link("/foo?page=0{&projection}");
Link result = assembler.appendPaginationParameterTemplates(link);
assertThat(result.getVariableNames(), hasSize(3));
assertThat(result.getVariableNames(), hasItems("projection", "size", "sort"));
}
/**
* @see DATAMCNS-563
*/
@@ -204,6 +178,20 @@ public class PagedResourcesAssemblerUnitTests {
assertThat(getQueryParameters(resource.getLink("next")), hasEntry("page", "3"));
}
/**
* @see DATACMNS-515
*/
@Test
public void generatedLinksShouldNotBeTemplated() {
PagedResourcesAssembler<Person> assembler = new PagedResourcesAssembler<Person>(resolver, null);
PagedResources<Resource<Person>> resources = assembler.toResource(createPage(1));
assertThat(resources.getLink(Link.REL_SELF).getHref(), endsWith("localhost"));
assertThat(resources.getLink(Link.REL_NEXT).getHref(), endsWith("?page=2&size=1"));
assertThat(resources.getLink(Link.REL_PREVIOUS).getHref(), endsWith("?page=0&size=1"));
}
private static Page<Person> createPage(int index) {
AbstractPageRequest request = new PageRequest(index, 1);