Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

AEROGEAR-1068 "Next" link returned when there is not a next page" #62

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+58 −6
Split
@@ -59,23 +59,31 @@ public Links getLinks() {
public Map<String, String> getHeaders(final int resultsSize) {
final Map<String, String> headers = new HashMap<String, String>();
if (headerPrefix.isPresent()) {
- if (!params.isFirstOffset()) {
+ if (!firstpage()) {
headers.put(headerPrefix.get() + "Links-Previous", links.getPrevious());
}
- if (resultsSize == params.limit()) {
+ if (fullpage(resultsSize)) {
headers.put(headerPrefix.get() + "Links-Next", links.getNext());
}
} else {
- if (params.isFirstOffset()) {
+ if (firstpage() && fullpage(resultsSize)) {
headers.put(webLinking.getLinkHeaderName(), webLinking.getNext());
- } else if (resultsSize < params.limit()) {
+ } else if (!firstpage() && !fullpage(resultsSize)) {
headers.put(webLinking.getLinkHeaderName(), webLinking.getPrevious());
- } else {
+ } else if (fullpage(resultsSize)) {
headers.put(webLinking.getLinkHeaderName(), webLinking.getLinkHeaders());
}
}
return headers;
}
+
+ private boolean firstpage() {
+ return params.isFirstOffset();
+ }
+
+ private boolean fullpage(final int results) {
+ return results == params.limit();
+ }
public WebLinking getWebLinking() {
return webLinking;
@@ -44,6 +44,28 @@ public void links() {
assertThat(links.getFirst()).isEqualTo("cars?offset=0&limit=10");
assertThat(links.getPrevious()).isEqualTo("cars?offset=0&limit=10");
}
+
+ @Test
+ public void customHeadersNoData() {
+ final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser, "TEST-");
+ final Map<String, String> headers = metadata.getHeaders(0);
+ assertThat(headers.isEmpty()).isTrue();
+ }
+
+ @Test
+ public void customHeadersNoFullPageAvailable() {
+ final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser, "TEST-");
+ final Map<String, String> headers = metadata.getHeaders(5);
+ assertThat(headers.isEmpty()).isTrue();
+ }
+
+ @Test
+ public void customHeadersOnePageAvailable() {
+ final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser, "TEST-");
+ final Map<String, String> headers = metadata.getHeaders(10);
+ assertThat(headers.get("TEST-Links-Next")).isEqualTo("cars?offset=10&limit=10");
+ assertThat(headers.size()).isEqualTo(1);
+ }
@Test
public void navigateForward() {
@@ -75,7 +97,7 @@ public void navigateBackwards() {
links = metadata.getLinks();
assertThat(links.getPrevious()).isEqualTo("cars?offset=0&limit=5");
}
-
+
@Test
public void webLinkingFirst() {
final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser);
@@ -102,6 +124,28 @@ public void webLinkingLast() {
assertThat(headers.get(WebLinking.PREVIOUS)).isEqualTo("cars?offset=0&limit=5");
assertThat(headers.get(WebLinking.NEXT)).isNull();
}
+
+ @Test
+ public void webLinkingNoData() {
+ final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser);
+ final Map<String, String> responseHeaders = metadata.getHeaders(0);
+ assertThat(responseHeaders.isEmpty()).isTrue();
+ }
+
+ @Test
+ public void webLinkingNoFullPageAvailable() {
+ final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser);
+ final Map<String, String> responseHeaders = metadata.getHeaders(5);
+ assertThat(responseHeaders.isEmpty()).isTrue();
+ }
+
+ @Test
+ public void webLinkingOneFullPageAvailable() {
+ final PaginationMetadata metadata = new PaginationMetadata(new PaginationProperties(0, 10), requestPathParser);
+ final Map<String, String> responseHeaders = metadata.getHeaders(10);
+ final Map<String, String> headers = Util.parseWebLinkHeader(responseHeaders.get(WebLinking.LINK_HEADER));
+ assertThat(headers.get(WebLinking.NEXT)).isEqualTo("cars?offset=10&limit=10");
+ }
private int parseOffset(final String header) {
final Matcher matcher = offsetLimitValue.matcher(header);