|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.web.servlet; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; |
| 22 | +import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; |
| 23 | +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration; |
| 24 | +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; |
| 25 | +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; |
| 26 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 27 | +import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; |
| 28 | +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; |
| 30 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 31 | +import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer; |
| 32 | +import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; |
| 33 | +import org.springframework.http.MediaType; |
| 34 | +import org.springframework.stereotype.Component; |
| 35 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 36 | +import org.springframework.web.reactive.function.client.WebClient; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * Integration tests for {@link WebMvcEndpointChildContextConfiguration}. |
| 42 | + * |
| 43 | + * @author Phillip Webb |
| 44 | + */ |
| 45 | +public class WebMvcEndpointChildContextConfigurationIntegrationTests { |
| 46 | + |
| 47 | + @Test // gh-17938 |
| 48 | + public void errorPageAndErrorControllerAreUsed() { |
| 49 | + new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new) |
| 50 | + .withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class, |
| 51 | + ServletWebServerFactoryAutoConfiguration.class, ServletManagementContextAutoConfiguration.class, |
| 52 | + WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class, |
| 53 | + DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class)) |
| 54 | + .withUserConfiguration(FailingEndpoint.class) |
| 55 | + .withInitializer(new ServerPortInfoApplicationContextInitializer()).withPropertyValues("server.port=0", |
| 56 | + "management.server.port=0", "management.endpoints.web.exposure.include=*") |
| 57 | + .run((context) -> { |
| 58 | + String port = context.getEnvironment().getProperty("local.management.port"); |
| 59 | + WebClient client = WebClient.create("http://localhost:" + port); |
| 60 | + ClientResponse response = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON) |
| 61 | + .exchange().block(); |
| 62 | + assertThat(response.bodyToMono(String.class).block()).contains("message\":\"Epic Fail"); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + @Component |
| 67 | + @Endpoint(id = "fail") |
| 68 | + public static class FailingEndpoint { |
| 69 | + |
| 70 | + @ReadOperation |
| 71 | + public String fail() { |
| 72 | + throw new IllegalStateException("Epic Fail"); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments