DATAREST-815 - Tweaked HalBrowser controller implementation to consider proxied requests.

We now use the APIs in place in Spring MVC to make sure we create proper redirects for requests that carry proxy headers so that the redirect target is still going through the proxy.
This commit is contained in:
Oliver Gierke
2016-04-27 15:33:40 +02:00
parent 127f1a846f
commit 3306b5ce40
2 changed files with 50 additions and 35 deletions

View File

@@ -15,18 +15,12 @@
*/
package org.springframework.data.rest.webmvc.halbrowser;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.data.rest.core.config.EnumTranslationConfiguration;
import org.springframework.data.rest.core.config.MetadataConfiguration;
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -51,14 +45,12 @@ public class HalBrowserUnitTests {
@Test
public void createsContextRelativeRedirectForBrowser() throws Exception {
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration(new ProjectionDefinitionConfiguration(),
new MetadataConfiguration(), mock(EnumTranslationConfiguration.class));
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/context");
request.setContextPath("/context");
View view = new HalBrowser(configuration).browser(request);
View view = new HalBrowser().browser(request);
assertThat(view, is(instanceOf(RedirectView.class)));
@@ -66,7 +58,26 @@ public class HalBrowserUnitTests {
UriComponents components = UriComponentsBuilder.fromUriString(response.getHeader(HttpHeaders.LOCATION)).build();
assertThat(components.getPath(), Matchers.startsWith("/context"));
assertThat(components.getPath(), startsWith("/context"));
assertThat(components.getFragment(), is("/context"));
}
@Test
public void producesProxyRelativeRedirectIfNecessary() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/browser");
request.addHeader("X-Forwarded-Host", "somehost");
request.addHeader("X-Forwarded-Port", "4711");
request.addHeader("X-Forwarded-Proto", "https");
request.addHeader("X-Forwarded-Prefix", "/prefix");
View view = new HalBrowser().browser(request);
assertThat(view, is(instanceOf(RedirectView.class)));
String url = ((RedirectView) view).getUrl();
assertThat(url, startsWith("https://somehost:4711/prefix"));
assertThat(url, endsWith("/prefix"));
}
}