diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpoint.java index 49be123016e..6d8f61df4fd 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-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. @@ -38,6 +38,7 @@ import org.springframework.web.servlet.resource.TransformedResource; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @author Stephane Nicoll * @since 1.3.0 */ public class HalBrowserMvcEndpoint extends HalJsonMvcEndpoint @@ -142,15 +143,15 @@ public class HalBrowserMvcEndpoint extends HalJsonMvcEndpoint resource = transformerChain.transform(request, resource); if (resource.getFilename().equalsIgnoreCase( HalBrowserMvcEndpoint.this.location.getHtmlFile())) { - return replaceInitialLink(resource); + return replaceInitialLink(request.getContextPath(), resource); } return resource; } - private Resource replaceInitialLink(Resource resource) throws IOException { + private Resource replaceInitialLink(String contextPath, Resource resource) throws IOException { byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); String content = new String(bytes, DEFAULT_CHARSET); - String initial = getManagementServletContext().getContextPath() + getPath(); + String initial = contextPath + getManagementServletContext().getContextPath() + getPath(); content = content.replace("entryPoint: '/'", "entryPoint: '" + initial + "'"); return new TransformedResource(resource, content.getBytes(DEFAULT_CHARSET)); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointManagementContextPathIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointManagementContextPathIntegrationTests.java index 9b65cdd077c..431b0e8f5e4 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointManagementContextPathIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointManagementContextPathIntegrationTests.java @@ -36,8 +36,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.WebApplicationContext; +import static org.hamcrest.CoreMatchers.containsString; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -88,6 +90,13 @@ public class HalBrowserMvcEndpointManagementContextPathIntegrationTests { .andExpect(forwardedUrl("/admin/browser.html")); } + @Test + public void actuatorBrowserHtml() throws Exception { + this.mockMvc.perform(get("/admin/browser.html").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("entryPoint: '/admin'"))); + } + @Test public void trace() throws Exception { this.mockMvc.perform(get("/admin/trace").accept(MediaType.APPLICATION_JSON)) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java index 855dc417572..e17265235f7 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HalBrowserMvcEndpointServerContextPathIntegrationTests.java @@ -85,6 +85,18 @@ public class HalBrowserMvcEndpointServerContextPathIntegrationTests { entity.getBody().contains(" entity = new TestRestTemplate().exchange( + "http://localhost:" + this.port + "/spring/actuator/browser.html", HttpMethod.GET, + new HttpEntity(null, headers), String.class); + assertEquals(HttpStatus.OK, entity.getStatusCode()); + assertTrue("Wrong body: " + entity.getBody(), + entity.getBody().contains("entryPoint: '/spring/actuator'")); + } + @Test public void actuatorLinks() throws Exception { HttpHeaders headers = new HttpHeaders();