Skip to content

Commit 84735f5

Browse files
committed
Fix build failure
1 parent 87af1a6 commit 84735f5

File tree

2 files changed

+196
-185
lines changed

2 files changed

+196
-185
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/integrationtest/WebFluxEndpointCorsIntegrationTests.java

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.integrationtest;
1818

19-
import org.junit.Before;
19+
import java.util.function.Consumer;
20+
2021
import org.junit.Test;
2122

2223
import org.springframework.boot.actuate.autoconfigure.beans.BeansEndpointAutoConfiguration;
@@ -25,156 +26,157 @@
2526
import org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration;
2627
import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementContextAutoConfiguration;
2728
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2830
import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration;
2931
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
3032
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
3133
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
32-
import org.springframework.boot.test.util.TestPropertyValues;
33-
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
34+
import org.springframework.boot.test.context.runner.ContextConsumer;
35+
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
36+
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
3437
import org.springframework.http.HttpHeaders;
3538
import org.springframework.test.web.reactive.server.WebTestClient;
3639

3740
/**
3841
* Integration tests for the WebFlux actuator endpoints' CORS support
3942
*
4043
* @author Brian Clozel
44+
* @author Stephane Nicoll
4145
* @see WebFluxEndpointManagementContextConfiguration
4246
*/
4347
public class WebFluxEndpointCorsIntegrationTests {
4448

45-
private AnnotationConfigReactiveWebApplicationContext context;
46-
47-
@Before
48-
public void createContext() {
49-
this.context = new AnnotationConfigReactiveWebApplicationContext();
50-
this.context.register(JacksonAutoConfiguration.class,
51-
CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class,
52-
HttpHandlerAutoConfiguration.class, EndpointAutoConfiguration.class,
53-
WebEndpointAutoConfiguration.class,
54-
ManagementContextAutoConfiguration.class,
55-
ReactiveManagementContextAutoConfiguration.class,
56-
BeansEndpointAutoConfiguration.class);
57-
TestPropertyValues.of("management.endpoints.web.exposure.include:*")
58-
.applyTo(this.context);
59-
}
49+
private ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
50+
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class,
51+
CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class,
52+
HttpHandlerAutoConfiguration.class, EndpointAutoConfiguration.class,
53+
WebEndpointAutoConfiguration.class,
54+
ManagementContextAutoConfiguration.class,
55+
ReactiveManagementContextAutoConfiguration.class,
56+
BeansEndpointAutoConfiguration.class))
57+
.withPropertyValues("management.endpoints.web.exposure.include:*");
6058

6159
@Test
6260
public void corsIsDisabledByDefault() {
63-
createWebTestClient().options().uri("/actuator/beans")
64-
.header("Origin", "spring.example.org")
61+
this.contextRunner.run(withWebTestClient((webTestClient) -> webTestClient
62+
.options().uri("/actuator/beans").header("Origin", "spring.example.org")
6563
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").exchange()
66-
.expectHeader().doesNotExist(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN);
64+
.expectHeader().doesNotExist(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)));
6765
}
6866

6967
@Test
7068
public void settingAllowedOriginsEnablesCors() {
71-
TestPropertyValues
72-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org")
73-
.applyTo(this.context);
74-
createWebTestClient().options().uri("/actuator/beans")
75-
.header("Origin", "test.example.org")
76-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").exchange()
77-
.expectStatus().isForbidden();
78-
performAcceptedCorsRequest("/actuator/beans");
69+
this.contextRunner.withPropertyValues(
70+
"management.endpoints.web.cors.allowed-origins:spring.example.org")
71+
.run(withWebTestClient((webTestClient) -> {
72+
webTestClient.options().uri("/actuator/beans")
73+
.header("Origin", "test.example.org")
74+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")
75+
.exchange().expectStatus().isForbidden();
76+
performAcceptedCorsRequest(webTestClient, "/actuator/beans");
77+
}));
7978
}
8079

8180
@Test
8281
public void maxAgeDefaultsTo30Minutes() {
83-
TestPropertyValues
84-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org")
85-
.applyTo(this.context);
86-
performAcceptedCorsRequest("/actuator/beans").expectHeader()
87-
.valueEquals(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "1800");
82+
this.contextRunner.withPropertyValues(
83+
"management.endpoints.web.cors.allowed-origins:spring.example.org")
84+
.run(withWebTestClient(
85+
(webTestClient) -> performAcceptedCorsRequest(webTestClient,
86+
"/actuator/beans").expectHeader().valueEquals(
87+
HttpHeaders.ACCESS_CONTROL_MAX_AGE, "1800")));
8888
}
8989

9090
@Test
9191
public void maxAgeCanBeConfigured() {
92-
TestPropertyValues
93-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org",
94-
"management.endpoints.web.cors.max-age: 2400")
95-
.applyTo(this.context);
96-
performAcceptedCorsRequest("/actuator/beans").expectHeader()
97-
.valueEquals(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "2400");
92+
this.contextRunner.withPropertyValues(
93+
"management.endpoints.web.cors.allowed-origins:spring.example.org",
94+
"management.endpoints.web.cors.max-age: 2400")
95+
.run(withWebTestClient(
96+
(webTestClient) -> performAcceptedCorsRequest(webTestClient,
97+
"/actuator/beans").expectHeader().valueEquals(
98+
HttpHeaders.ACCESS_CONTROL_MAX_AGE, "2400")));
9899
}
99100

100101
@Test
101102
public void requestsWithDisallowedHeadersAreRejected() {
102-
TestPropertyValues
103-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org")
104-
.applyTo(this.context);
105-
createWebTestClient().options().uri("/actuator/beans")
106-
.header("Origin", "spring.example.org")
107-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")
108-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Alpha").exchange()
109-
.expectStatus().isForbidden();
103+
this.contextRunner.withPropertyValues(
104+
"management.endpoints.web.cors.allowed-origins:spring.example.org")
105+
.run(withWebTestClient((webTestClient) -> webTestClient.options()
106+
.uri("/actuator/beans").header("Origin", "spring.example.org")
107+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")
108+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Alpha")
109+
.exchange().expectStatus().isForbidden()));
110110
}
111111

112112
@Test
113113
public void allowedHeadersCanBeConfigured() {
114-
TestPropertyValues
115-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org",
116-
"management.endpoints.web.cors.allowed-headers:Alpha,Bravo")
117-
.applyTo(this.context);
118-
createWebTestClient().options().uri("/actuator/beans")
119-
.header("Origin", "spring.example.org")
120-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")
121-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Alpha").exchange()
122-
.expectStatus().isOk().expectHeader()
123-
.valueEquals(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "Alpha");
114+
this.contextRunner.withPropertyValues(
115+
"management.endpoints.web.cors.allowed-origins:spring.example.org",
116+
"management.endpoints.web.cors.allowed-headers:Alpha,Bravo")
117+
.run(withWebTestClient((webTestClient) -> webTestClient.options()
118+
.uri("/actuator/beans").header("Origin", "spring.example.org")
119+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")
120+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Alpha")
121+
.exchange().expectStatus().isOk().expectHeader()
122+
.valueEquals(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "Alpha")));
124123
}
125124

126125
@Test
127126
public void requestsWithDisallowedMethodsAreRejected() {
128-
TestPropertyValues
129-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org")
130-
.applyTo(this.context);
131-
createWebTestClient().options().uri("/actuator/beans")
132-
.header("Origin", "spring.example.org")
133-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PATCH").exchange()
134-
.expectStatus().isForbidden();
127+
this.contextRunner.withPropertyValues(
128+
"management.endpoints.web.cors.allowed-origins:spring.example.org")
129+
.run(withWebTestClient((webTestClient) -> webTestClient.options()
130+
.uri("/actuator/beans").header("Origin", "spring.example.org")
131+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PATCH")
132+
.exchange().expectStatus().isForbidden()));
135133
}
136134

137135
@Test
138136
public void allowedMethodsCanBeConfigured() {
139-
TestPropertyValues
140-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org",
141-
"management.endpoints.web.cors.allowed-methods:GET,HEAD")
142-
.applyTo(this.context);
143-
createWebTestClient().options().uri("/actuator/beans")
144-
.header("Origin", "spring.example.org")
145-
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "HEAD").exchange()
146-
.expectStatus().isOk().expectHeader()
147-
.valueEquals(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,HEAD");
137+
this.contextRunner.withPropertyValues(
138+
"management.endpoints.web.cors.allowed-origins:spring.example.org",
139+
"management.endpoints.web.cors.allowed-methods:GET,HEAD")
140+
.run(withWebTestClient((webTestClient) -> webTestClient.options()
141+
.uri("/actuator/beans").header("Origin", "spring.example.org")
142+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "HEAD")
143+
.exchange().expectStatus().isOk().expectHeader().valueEquals(
144+
HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,HEAD")));
148145
}
149146

150147
@Test
151148
public void credentialsCanBeAllowed() {
152-
TestPropertyValues
153-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org",
154-
"management.endpoints.web.cors.allow-credentials:true")
155-
.applyTo(this.context);
156-
performAcceptedCorsRequest("/actuator/beans").expectHeader()
157-
.valueEquals(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
149+
this.contextRunner.withPropertyValues(
150+
"management.endpoints.web.cors.allowed-origins:spring.example.org",
151+
"management.endpoints.web.cors.allow-credentials:true")
152+
.run(withWebTestClient(
153+
(webTestClient) -> performAcceptedCorsRequest(webTestClient,
154+
"/actuator/beans").expectHeader().valueEquals(
155+
HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS,
156+
"true")));
158157
}
159158

160159
@Test
161160
public void credentialsCanBeDisabled() {
162-
TestPropertyValues
163-
.of("management.endpoints.web.cors.allowed-origins:spring.example.org",
164-
"management.endpoints.web.cors.allow-credentials:false")
165-
.applyTo(this.context);
166-
performAcceptedCorsRequest("/actuator/beans").expectHeader()
167-
.doesNotExist(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS);
161+
this.contextRunner.withPropertyValues(
162+
"management.endpoints.web.cors.allowed-origins:spring.example.org",
163+
"management.endpoints.web.cors.allow-credentials:false")
164+
.run(withWebTestClient(
165+
(webTestClient) -> performAcceptedCorsRequest(webTestClient,
166+
"/actuator/beans").expectHeader().doesNotExist(
167+
HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)));
168168
}
169169

170-
private WebTestClient createWebTestClient() {
171-
this.context.refresh();
172-
return WebTestClient.bindToApplicationContext(this.context).configureClient()
173-
.baseUrl("https://spring.example.org").build();
170+
private ContextConsumer<ReactiveWebApplicationContext> withWebTestClient(
171+
Consumer<WebTestClient> webTestClient) {
172+
return (context) -> webTestClient
173+
.accept(WebTestClient.bindToApplicationContext(context).configureClient()
174+
.baseUrl("https://spring.example.org").build());
174175
}
175176

176-
private WebTestClient.ResponseSpec performAcceptedCorsRequest(String url) {
177-
return createWebTestClient().options().uri(url)
177+
private WebTestClient.ResponseSpec performAcceptedCorsRequest(
178+
WebTestClient webTestClient, String url) {
179+
return webTestClient.options().uri(url)
178180
.header(HttpHeaders.ORIGIN, "spring.example.org")
179181
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").exchange()
180182
.expectHeader().valueEquals(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,

0 commit comments

Comments
 (0)