Deduplicates context path in health-check-url

fixes gh-3809
This commit is contained in:
slamhan
2020-06-05 22:50:18 +08:00
committed by spencergibb
parent 85261361d7
commit 443628827e
2 changed files with 22 additions and 1 deletions

View File

@@ -123,7 +123,7 @@ public class DefaultManagementMetadataProvider implements ManagementMetadataProv
String refinedContextPath = '/'
+ StringUtils.trimLeadingCharacter(contextPath, '/');
URL base = new URL(scheme, hostname, port, refinedContextPath);
String refinedStatusPath = StringUtils.trimLeadingCharacter(statusPath, '/');
String refinedStatusPath = refinedStatusPath(statusPath, contextPath);
return new URL(base, refinedStatusPath).toString();
}
catch (MalformedURLException e) {
@@ -133,6 +133,13 @@ public class DefaultManagementMetadataProvider implements ManagementMetadataProv
}
}
private String refinedStatusPath(String statusPath, String contextPath) {
if (statusPath.startsWith(contextPath) && !"/".equals(contextPath)) {
statusPath = StringUtils.replace(statusPath, contextPath, "");
}
return StringUtils.trimLeadingCharacter(statusPath, '/');
}
private String getErrorMessage(String scheme, String hostname, int port,
String contextPath, String statusPath) {
return String.format(

View File

@@ -325,6 +325,20 @@ public class EurekaClientAutoConfigurationTests {
.as("Wrong health check: " + instance.getHealthCheckUrl()).isTrue();
}
@Test
public void healthCheckUrlPathWithServerPortAndContextPathKebobCase() {
TestPropertyValues.of("server.port=8989",
"server.servlet.context-path=/servletContextPath",
"eureka.instance.health-check-url-path=${server.servlet.context-path:}/myHealthCheck")
.applyTo(this.context);
setupContext(RefreshAutoConfiguration.class);
EurekaInstanceConfigBean instance = this.context
.getBean(EurekaInstanceConfigBean.class);
assertThat(instance.getHealthCheckUrl())
.as("Wrong health check: " + instance.getHealthCheckUrl())
.endsWith(":8989/servletContextPath/myHealthCheck");
}
@Test
public void statusPageUrlPathAndManagementPortKabobCase() {
TestPropertyValues