diff --git a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java index 5723a9d1..d51397a1 100755 --- a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java +++ b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java @@ -21,6 +21,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import javax.servlet.http.HttpServletRequest; + import org.aopalliance.intercept.MethodInvocation; import org.springframework.hateoas.Link; import org.springframework.hateoas.core.AnnotationAttribute; @@ -30,8 +32,12 @@ import org.springframework.hateoas.core.DummyInvocationUtils.LastInvocationAware import org.springframework.hateoas.core.LinkBuilderSupport; import org.springframework.hateoas.core.MappingDiscoverer; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriTemplate; @@ -79,7 +85,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport identifyable = mock(Identifiable.class); - when(identifyable.getId()).thenReturn(10L); + Identifiable identifyable = Mockito.mock(Identifiable.class); + Mockito.when(identifyable.getId()).thenReturn(10L); Link link = linkTo(PersonControllerImpl.class).slash(identifyable).withSelfRel(); - assertThat(link.getHref(), Matchers.endsWith("/people/10")); + assertThat(link.getHref(), endsWith("/people/10")); } @Test public void appendingNullIsANoOp() { Link link = linkTo(PersonControllerImpl.class).slash(null).withSelfRel(); - assertThat(link.getHref(), Matchers.endsWith("/people")); + assertThat(link.getHref(), endsWith("/people")); link = linkTo(PersonControllerImpl.class).slash((Object) null).withSelfRel(); - assertThat(link.getHref(), Matchers.endsWith("/people")); + assertThat(link.getHref(), endsWith("/people")); } @Test public void linksToMethod() { Link link = linkTo(methodOn(ControllerWithMethods.class).myMethod(null)).withSelfRel(); - assertThat(link.getHref(), Matchers.endsWith("/something/else")); + assertThat(link.getHref(), endsWith("/something/else")); } @Test public void linksToMethodWithPathVariable() { Link link = linkTo(methodOn(ControllerWithMethods.class).methodWithPathVariable("1")).withSelfRel(); - assertThat(link.getHref(), Matchers.endsWith("/something/1/foo")); + assertThat(link.getHref(), endsWith("/something/1/foo")); + } + + /** + * @see #33 + */ + @Test + public void usesForwardedHostAsHostIfHeaderIsSet() { + + request.addHeader("X-Forwarded-Host", "somethingDifferent"); + + Link link = linkTo(PersonControllerImpl.class).withSelfRel(); + assertThat(link.getHref(), startsWith("http://somethingDifferent")); } static class Person implements Identifiable { diff --git a/template.mf b/template.mf index c3b34056..07dae951 100644 --- a/template.mf +++ b/template.mf @@ -5,6 +5,7 @@ Bundle-ManifestVersion: 2 Import-Template: com.fasterxml.jackson.*;version="${jackson2.version:[=.=.=,+1.0.0)}";resolution:=optional, com.jayway.jsonpath.*;version="${jsonpath.version:[=.=.=,+1.0.0)}";resolution:=optional, + javax.servlet.*;version="[2.5,4.0)";resolution:=optional, javax.ws.rs.*;version="${jaxrs.version:[=.=.=,+1.0.0)}";resolution:=optional, javax.xml.bind.*;version="0", net.minidev.json.*;version="${minidevjson.version:[=.=.=,+1.0.0)}";resolution:=optional,