From 060b94e00071826ed74c356e039b884dd187c564 Mon Sep 17 00:00:00 2001 From: Michael Wirth Date: Mon, 1 Apr 2019 21:03:41 +0200 Subject: [PATCH] #975 - Only expand templated links with Traverson when requested. Original issue: #971 --- .../springframework/hateoas/client/Server.java | 6 ++++++ .../hateoas/client/TraversonTest.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/test/java/org/springframework/hateoas/client/Server.java b/src/test/java/org/springframework/hateoas/client/Server.java index 77244151..7760ef2f 100644 --- a/src/test/java/org/springframework/hateoas/client/Server.java +++ b/src/test/java/org/springframework/hateoas/client/Server.java @@ -96,6 +96,12 @@ public class Server implements Closeable { withBody("{ \"_links\" : { \"self\" : { \"href\" : \"/{?template}\" }}}"). // withContentType(MediaTypes.HAL_JSON.toString()); + onRequest(). // + havingPathEqualTo("/github-with-template"). // + respond(). // + withBody("{ \"foo_url_templated\" : \"" + rootResource() + "/github/{issue}\"}"). // + withContentType(MediaTypes.HAL_JSON.toString()); + // Sample traversal of HAL docs based on Spring-a-Gram showcase org.springframework.core.io.Resource springagramRoot = resourceLoader.getResource("classpath:springagram-root.json"); org.springframework.core.io.Resource springagramItems = resourceLoader.getResource("classpath:springagram-items.json"); diff --git a/src/test/java/org/springframework/hateoas/client/TraversonTest.java b/src/test/java/org/springframework/hateoas/client/TraversonTest.java index 4a6fe491..6865dbc1 100644 --- a/src/test/java/org/springframework/hateoas/client/TraversonTest.java +++ b/src/test/java/org/springframework/hateoas/client/TraversonTest.java @@ -239,6 +239,23 @@ public class TraversonTest { assertThat(link.isTemplated(), is(false)); } + @Test + public void returnsTemplatedRequiredLinkIfRequested() { + + TraversalBuilder follow = new Traverson(URI.create(server.rootResource().concat("/github-with-template")), MediaTypes.HAL_JSON) + .follow("foo_url_templated"); + + Link link = follow.asTemplatedLink(); + + assertThat(link.isTemplated()).isTrue(); + assertThat(link.getVariableNames()).contains("template"); + + link = follow.asLink(); + + assertThat(link.isTemplated()).isFalse(); + } + + /** * @see #258 */