Skip to content

Commit bcb485b

Browse files
committed
bindToApplicatonContext uses WebSessionManager bean
Issue: SPR-17094
1 parent c022f7c commit bcb485b

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public WebTestClient.Builder configureClient() {
8787
if (!CollectionUtils.isEmpty(this.filters)) {
8888
builder.filters(theFilters -> theFilters.addAll(0, this.filters));
8989
}
90-
if (this.sessionManager != null) {
90+
if (!builder.hasSessionManager() && this.sessionManager != null) {
9191
builder.sessionManager(this.sessionManager);
9292
}
9393
if (!CollectionUtils.isEmpty(this.configurers)) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-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+
* http://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+
package org.springframework.test.web.reactive.server;
17+
18+
import org.junit.Test;
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.context.ApplicationContext;
22+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.mock.web.server.MockWebSession;
26+
import org.springframework.web.reactive.config.EnableWebFlux;
27+
import org.springframework.web.reactive.function.server.RouterFunction;
28+
import org.springframework.web.reactive.function.server.RouterFunctions;
29+
import org.springframework.web.reactive.function.server.ServerResponse;
30+
import org.springframework.web.server.session.WebSessionManager;
31+
32+
/**
33+
* Unit tests with {@link ApplicationContextSpec}.
34+
* @author Rossen Stoyanchev
35+
*/
36+
public class ApplicationContextSpecTests {
37+
38+
39+
@Test // SPR-17094
40+
public void sessionManagerBean() {
41+
ApplicationContext context = new AnnotationConfigApplicationContext(WebConfig.class);
42+
ApplicationContextSpec spec = new ApplicationContextSpec(context);
43+
WebTestClient testClient = spec.configureClient().build();
44+
45+
for (int i=0; i < 2; i++) {
46+
testClient.get().uri("/sessionClassName")
47+
.exchange()
48+
.expectStatus().isOk()
49+
.expectBody(String.class).isEqualTo("MockWebSession");
50+
}
51+
}
52+
53+
54+
@Configuration
55+
@EnableWebFlux
56+
static class WebConfig {
57+
58+
@Bean
59+
public RouterFunction<?> handler() {
60+
return RouterFunctions.route()
61+
.GET("/sessionClassName", request ->
62+
request.session().flatMap(session -> {
63+
String className = session.getClass().getSimpleName();
64+
return ServerResponse.ok().syncBody(className);
65+
}))
66+
.build();
67+
}
68+
69+
@Bean
70+
public WebSessionManager webSessionManager() {
71+
MockWebSession session = new MockWebSession();
72+
return exchange -> Mono.just(session);
73+
}
74+
}
75+
76+
}

spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,35 @@ public WebHttpHandlerBuilder sessionManager(WebSessionManager manager) {
240240
}
241241

242242
/**
243-
* Configure the {@link ServerCodecConfigurer} to set on the
244-
* {@link ServerWebExchange WebServerExchange}.
243+
* Whether a {@code WebSessionManager} is configured or not, either
244+
* detected from an {@code ApplicationContext} or explicitly configured via
245+
* {@link #sessionManager(WebSessionManager)}.
246+
* @since 5.0.9
247+
*/
248+
public boolean hasSessionManager() {
249+
return this.sessionManager != null;
250+
}
251+
252+
/**
253+
* Configure the {@link ServerCodecConfigurer} to set on the {@code WebServerExchange}.
245254
* @param codecConfigurer the codec configurer
246255
*/
247256
public WebHttpHandlerBuilder codecConfigurer(ServerCodecConfigurer codecConfigurer) {
248257
this.codecConfigurer = codecConfigurer;
249258
return this;
250259
}
251260

261+
262+
/**
263+
* Whether a {@code ServerCodecConfigurer} is configured or not, either
264+
* detected from an {@code ApplicationContext} or explicitly configured via
265+
* {@link #codecConfigurer(ServerCodecConfigurer)}.
266+
* @since 5.0.9
267+
*/
268+
public boolean hasCodecConfigurer() {
269+
return this.codecConfigurer != null;
270+
}
271+
252272
/**
253273
* Configure the {@link LocaleContextResolver} to set on the
254274
* {@link ServerWebExchange WebServerExchange}.
@@ -259,6 +279,16 @@ public WebHttpHandlerBuilder localeContextResolver(LocaleContextResolver localeC
259279
return this;
260280
}
261281

282+
/**
283+
* Whether a {@code LocaleContextResolver} is configured or not, either
284+
* detected from an {@code ApplicationContext} or explicitly configured via
285+
* {@link #localeContextResolver(LocaleContextResolver)}.
286+
* @since 5.0.9
287+
*/
288+
public boolean hasLocaleContextResolver() {
289+
return this.localeContextResolver != null;
290+
}
291+
262292

263293
/**
264294
* Build the {@link HttpHandler}.

0 commit comments

Comments
 (0)