Browse Source

Allow setting ApplicationContext on MockServerWebExchange

Closes gh-34601
pull/34656/head
rstoyanchev 9 months ago
parent
commit
37d7af42ac
  1. 31
      spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java
  2. 4
      spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

31
spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java

@ -1,5 +1,5 @@ @@ -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; @@ -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; @@ -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 { @@ -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 { @@ -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);
}
}

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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) {

Loading…
Cancel
Save