Browse Source

Polish

pull/1904/merge
Rossen Stoyanchev 6 years ago
parent
commit
c022f7c19d
  1. 39
      spring-test/src/main/java/org/springframework/test/web/reactive/server/AbstractMockServerSpec.java

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

@ -20,7 +20,8 @@ import java.util.ArrayList; @@ -20,7 +20,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.util.Assert;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import org.springframework.web.server.session.DefaultWebSessionManager;
@ -36,22 +37,33 @@ import org.springframework.web.server.session.WebSessionManager; @@ -36,22 +37,33 @@ import org.springframework.web.server.session.WebSessionManager;
abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
implements WebTestClient.MockServerSpec<B> {
private final List<WebFilter> filters = new ArrayList<>(4);
@Nullable
private List<WebFilter> filters;
private WebSessionManager sessionManager = new DefaultWebSessionManager();
@Nullable
private WebSessionManager sessionManager;
private final List<MockServerConfigurer> configurers = new ArrayList<>(4);
@Nullable
private List<MockServerConfigurer> configurers;
AbstractMockServerSpec() {
// Default instance to be re-used across requests, unless one is configured explicitly
this.sessionManager = new DefaultWebSessionManager();
}
@Override
public <T extends B> T webFilter(WebFilter... filter) {
this.filters.addAll(Arrays.asList(filter));
public <T extends B> T webFilter(WebFilter... filters) {
if (filters.length > 0) {
this.filters = (this.filters != null ? this.filters : new ArrayList<>(4));
this.filters.addAll(Arrays.asList(filters));
}
return self();
}
@Override
public <T extends B> T webSessionManager(WebSessionManager sessionManager) {
Assert.notNull(sessionManager, "WebSessionManager must not be null.");
this.sessionManager = sessionManager;
return self();
}
@ -59,6 +71,7 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>> @@ -59,6 +71,7 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
@Override
public <T extends B> T apply(MockServerConfigurer configurer) {
configurer.afterConfigureAdded(this);
this.configurers = (this.configurers != null ? this.configurers : new ArrayList<>(4));
this.configurers.add(configurer);
return self();
}
@ -71,9 +84,15 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>> @@ -71,9 +84,15 @@ abstract class AbstractMockServerSpec<B extends WebTestClient.MockServerSpec<B>>
@Override
public WebTestClient.Builder configureClient() {
WebHttpHandlerBuilder builder = initHttpHandlerBuilder();
builder.filters(theFilters -> theFilters.addAll(0, this.filters));
builder.sessionManager(this.sessionManager);
this.configurers.forEach(configurer -> configurer.beforeServerCreated(builder));
if (!CollectionUtils.isEmpty(this.filters)) {
builder.filters(theFilters -> theFilters.addAll(0, this.filters));
}
if (this.sessionManager != null) {
builder.sessionManager(this.sessionManager);
}
if (!CollectionUtils.isEmpty(this.configurers)) {
this.configurers.forEach(configurer -> configurer.beforeServerCreated(builder));
}
return new DefaultWebTestClientBuilder(builder);
}

Loading…
Cancel
Save