|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.endpoint.web;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
19 | 24 | import org.junit.Test;
|
20 | 25 |
|
21 | 26 | import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
|
22 | 27 | import org.springframework.boot.actuate.autoconfigure.endpoint.ExposeExcludePropertyEndpointFilter;
|
23 | 28 | import org.springframework.boot.actuate.endpoint.EndpointId;
|
| 29 | +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; |
24 | 30 | import org.springframework.boot.actuate.endpoint.http.ActuatorMediaType;
|
25 | 31 | import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
|
| 32 | +import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint; |
| 33 | +import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoint; |
26 | 34 | import org.springframework.boot.actuate.endpoint.web.PathMapper;
|
27 | 35 | import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointDiscoverer;
|
28 | 36 | import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointDiscoverer;
|
29 | 37 | import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointDiscoverer;
|
30 | 38 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
31 | 39 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
32 | 40 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
| 41 | +import org.springframework.stereotype.Component; |
33 | 42 |
|
34 | 43 | import static org.assertj.core.api.Assertions.assertThat;
|
35 | 44 |
|
@@ -71,6 +80,27 @@ public void webApplicationConfiguresPathMapper() {
|
71 | 80 | });
|
72 | 81 | }
|
73 | 82 |
|
| 83 | + @Test |
| 84 | + public void webApplicationSupportCustomPathMatcher() { |
| 85 | + this.contextRunner |
| 86 | + .withPropertyValues("management.endpoints.web.exposure.include=*", |
| 87 | + "management.endpoints.web.path-mapping.testanotherone=foo") |
| 88 | + .withUserConfiguration(TestPathMatcher.class, TestOneEndpoint.class, |
| 89 | + TestAnotherOneEndpoint.class, TestTwoEndpoint.class) |
| 90 | + .run((context) -> { |
| 91 | + WebEndpointDiscoverer discoverer = context |
| 92 | + .getBean(WebEndpointDiscoverer.class); |
| 93 | + Collection<ExposableWebEndpoint> endpoints = discoverer |
| 94 | + .getEndpoints(); |
| 95 | + ExposableWebEndpoint[] webEndpoints = endpoints |
| 96 | + .toArray(new ExposableWebEndpoint[0]); |
| 97 | + List<String> paths = Arrays.stream(webEndpoints) |
| 98 | + .map(PathMappedEndpoint::getRootPath) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + assertThat(paths).containsOnly("1/testone", "foo", "testtwo"); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
74 | 104 | @Test
|
75 | 105 | public void webApplicationConfiguresEndpointDiscoverer() {
|
76 | 106 | this.contextRunner.run((context) -> {
|
@@ -100,4 +130,35 @@ public void contextWhenNotServletShouldNotConfigureServletEndpointDiscoverer() {
|
100 | 130 | .doesNotHaveBean(ServletEndpointDiscoverer.class));
|
101 | 131 | }
|
102 | 132 |
|
| 133 | + @Component |
| 134 | + private static class TestPathMatcher implements PathMapper { |
| 135 | + |
| 136 | + @Override |
| 137 | + public String getRootPath(EndpointId endpointId) { |
| 138 | + if (endpointId.toString().endsWith("one")) { |
| 139 | + return "1/" + endpointId.toString(); |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + @Component |
| 147 | + @Endpoint(id = "testone") |
| 148 | + private static class TestOneEndpoint { |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + @Component |
| 153 | + @Endpoint(id = "testanotherone") |
| 154 | + private static class TestAnotherOneEndpoint { |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + @Component |
| 159 | + @Endpoint(id = "testtwo") |
| 160 | + private static class TestTwoEndpoint { |
| 161 | + |
| 162 | + } |
| 163 | + |
103 | 164 | }
|
0 commit comments