Allow setting ApplicationContext on MockServerWebExchange

Closes gh-34601
This commit is contained in:
rstoyanchev
2025-03-18 15:05:50 +00:00
parent cc986cd2e8
commit 37d7af42ac
2 changed files with 27 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.mock.web.server;
import reactor.core.publisher.Mono;
import org.springframework.context.ApplicationContext;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
@@ -39,9 +40,15 @@ import org.springframework.web.server.session.WebSessionManager;
*/
public final class MockServerWebExchange extends DefaultServerWebExchange {
private MockServerWebExchange(MockServerHttpRequest request, WebSessionManager sessionManager) {
super(request, new MockServerHttpResponse(), sessionManager,
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
private MockServerWebExchange(
MockServerHttpRequest request, @Nullable WebSessionManager sessionManager,
@Nullable ApplicationContext applicationContext) {
super(request, new MockServerHttpResponse(),
sessionManager != null ? sessionManager : new DefaultWebSessionManager(),
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver(),
applicationContext);
}
@@ -101,6 +108,9 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
@Nullable
private WebSessionManager sessionManager;
@Nullable
private ApplicationContext applicationContext;
public Builder(MockServerHttpRequest request) {
this.request = request;
}
@@ -127,12 +137,21 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
return this;
}
/**
* Provide the {@code ApplicationContext} to expose through the exchange.
* @param applicationContext the context to use
* @since 6.2.5
*/
public Builder applicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
return this;
}
/**
* Build the {@code MockServerWebExchange} instance.
*/
public MockServerWebExchange build() {
return new MockServerWebExchange(this.request,
this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager());
return new MockServerWebExchange(this.request, this.sessionManager, this.applicationContext);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -119,7 +119,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
this(request, response, sessionManager, codecConfigurer, localeContextResolver, null);
}
DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
protected DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
WebSessionManager sessionManager, ServerCodecConfigurer codecConfigurer,
LocaleContextResolver localeContextResolver, @Nullable ApplicationContext applicationContext) {