DATAREST-720 - HAL Browser fragment now considers the context path if set.

Removed some obsolete Maven dependency versions along the way.
This commit is contained in:
Oliver Gierke
2016-04-01 12:59:37 +02:00
parent 47e89d9fec
commit 9e58ab92c5
3 changed files with 23 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.rest.webmvc.halbrowser;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.BasePathAwareController;
@@ -58,8 +60,8 @@ public class HalBrowser {
* @return
*/
@RequestMapping(value = { "/", "" }, method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
public View index() {
return browser();
public View index(HttpServletRequest request) {
return browser(request);
}
/**
@@ -68,9 +70,11 @@ public class HalBrowser {
* @return
*/
@RequestMapping(value = "/browser", method = RequestMethod.GET)
public View browser() {
public View browser(HttpServletRequest request) {
String contextPath = request.getContextPath();
String basePath = configuration.getBasePath().toString();
return new RedirectView(basePath.concat(BROWSER_INDEX).concat("#").concat(basePath), true);
return new RedirectView(basePath.concat(BROWSER_INDEX).concat("#").concat(contextPath.concat(basePath)), true);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,8 @@ import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Unit tests for {@link HalBrowser}.
@@ -44,22 +46,27 @@ public class HalBrowserUnitTests {
/**
* @see DATAREST-565
* @see DATAREST-720
*/
@Test
public void createsContextRelativeRedirectForBrowser() throws Exception {
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration(new ProjectionDefinitionConfiguration(),
new MetadataConfiguration(), mock(EnumTranslationConfiguration.class));
View view = new HalBrowser(configuration).browser();
assertThat(view, is(instanceOf(RedirectView.class)));
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath("/context");
View view = new HalBrowser(configuration).browser(request);
assertThat(view, is(instanceOf(RedirectView.class)));
((AbstractView) view).render(Collections.<String, Object> emptyMap(), request, response);
assertThat(response.getHeader(HttpHeaders.LOCATION), Matchers.startsWith("/context"));
UriComponents components = UriComponentsBuilder.fromUriString(response.getHeader(HttpHeaders.LOCATION)).build();
assertThat(components.getPath(), Matchers.startsWith("/context"));
assertThat(components.getFragment(), is("/context"));
}
}