Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance MockMvcWebTestClient to allow applying RequestPostProcessors #30233

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* @author Michał Rowicki
* @since 5.0
*/
class DefaultWebTestClient implements WebTestClient {
public class DefaultWebTestClient implements WebTestClient {

private final WiretapConnector wiretapConnector;

Expand All @@ -91,7 +91,7 @@ class DefaultWebTestClient implements WebTestClient {
private final AtomicLong requestIndex = new AtomicLong();


DefaultWebTestClient(ClientHttpConnector connector,
protected DefaultWebTestClient(ClientHttpConnector connector,
Function<ClientHttpConnector, ExchangeFunction> exchangeFactory, UriBuilderFactory uriBuilderFactory,
@Nullable HttpHeaders headers, @Nullable MultiValueMap<String, String> cookies,
Consumer<EntityExchangeResult<?>> entityResultConsumer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* @author Rossen Stoyanchev
* @since 5.0
*/
class DefaultWebTestClientBuilder implements WebTestClient.Builder {
public class DefaultWebTestClientBuilder implements WebTestClient.Builder {

private static final boolean reactorNettyClientPresent;

Expand All @@ -77,54 +77,54 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {


@Nullable
private final WebHttpHandlerBuilder httpHandlerBuilder;
protected final WebHttpHandlerBuilder httpHandlerBuilder;

@Nullable
private final ClientHttpConnector connector;
protected final ClientHttpConnector connector;

@Nullable
private String baseUrl;
protected String baseUrl;

@Nullable
private UriBuilderFactory uriBuilderFactory;
protected UriBuilderFactory uriBuilderFactory;

@Nullable
private HttpHeaders defaultHeaders;
protected HttpHeaders defaultHeaders;

@Nullable
private MultiValueMap<String, String> defaultCookies;
protected MultiValueMap<String, String> defaultCookies;

@Nullable
private List<ExchangeFilterFunction> filters;
protected List<ExchangeFilterFunction> filters;

private Consumer<EntityExchangeResult<?>> entityResultConsumer = result -> {};
protected Consumer<EntityExchangeResult<?>> entityResultConsumer = result -> {};

@Nullable
private ExchangeStrategies strategies;
protected ExchangeStrategies strategies;

@Nullable
private List<Consumer<ExchangeStrategies.Builder>> strategiesConfigurers;
protected List<Consumer<ExchangeStrategies.Builder>> strategiesConfigurers;

@Nullable
private Duration responseTimeout;
protected Duration responseTimeout;


/** Determine connector via classpath detection. */
DefaultWebTestClientBuilder() {
protected DefaultWebTestClientBuilder() {
this(null, null);
}

/** Use HttpHandlerConnector with mock server. */
DefaultWebTestClientBuilder(WebHttpHandlerBuilder httpHandlerBuilder) {
protected DefaultWebTestClientBuilder(WebHttpHandlerBuilder httpHandlerBuilder) {
this(httpHandlerBuilder, null);
}

/** Use given connector. */
DefaultWebTestClientBuilder(ClientHttpConnector connector) {
protected DefaultWebTestClientBuilder(ClientHttpConnector connector) {
this(null, connector);
}

DefaultWebTestClientBuilder(
protected DefaultWebTestClientBuilder(
@Nullable WebHttpHandlerBuilder httpHandlerBuilder, @Nullable ClientHttpConnector connector) {

Assert.isTrue(httpHandlerBuilder == null || connector == null,
Expand All @@ -139,7 +139,7 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
}

/** Copy constructor. */
DefaultWebTestClientBuilder(DefaultWebTestClientBuilder other) {
protected DefaultWebTestClientBuilder(DefaultWebTestClientBuilder other) {
this.httpHandlerBuilder = (other.httpHandlerBuilder != null ? other.httpHandlerBuilder.clone() : null);
this.connector = other.connector;
this.responseTimeout = other.responseTimeout;
Expand Down Expand Up @@ -323,7 +323,7 @@ else if (httpComponentsClientPresent) {
}
}

private ExchangeStrategies initExchangeStrategies() {
protected ExchangeStrategies initExchangeStrategies() {
if (CollectionUtils.isEmpty(this.strategiesConfigurers)) {
return (this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults());
}
Expand All @@ -333,7 +333,7 @@ private ExchangeStrategies initExchangeStrategies() {
return builder.build();
}

private UriBuilderFactory initUriBuilderFactory() {
protected UriBuilderFactory initUriBuilderFactory() {
if (this.uriBuilderFactory != null) {
return this.uriBuilderFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import jakarta.servlet.Filter;

import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.DispatcherServletCustomizer;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
Expand Down Expand Up @@ -93,14 +91,13 @@ private <T extends B> T self() {
protected abstract ConfigurableMockMvcBuilder<?> getMockMvcBuilder();

@Override
public WebTestClient.Builder configureClient() {
public MockMvcWebTestClient.Builder configureClient() {
MockMvc mockMvc = getMockMvcBuilder().build();
ClientHttpConnector connector = new MockMvcHttpConnector(mockMvc);
return WebTestClient.bindToServer(connector);
return MockMvcWebTestClient.bindToMockMvc(mockMvc);
}

@Override
public WebTestClient build() {
public MockMvcWebTestClient build() {
return configureClient().build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2002-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.web.servlet.client;

import java.time.Duration;
import java.util.function.Consumer;
import java.util.function.Function;

import org.springframework.http.HttpHeaders;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.test.web.reactive.server.DefaultWebTestClient;
import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.util.UriBuilderFactory;

/**
* Default implementation of {@link MockMvcWebTestClient}.
*
* @author Justin Tay
*/
public class DefaultMockMvcWebTestClient extends DefaultWebTestClient implements MockMvcWebTestClient {
private final DefaultMockMvcWebTestClientBuilder builder;

DefaultMockMvcWebTestClient(ClientHttpConnector connector,
Function<ClientHttpConnector, ExchangeFunction> exchangeFactory, UriBuilderFactory uriBuilderFactory,
HttpHeaders headers, MultiValueMap<String, String> cookies,
Consumer<EntityExchangeResult<?>> entityResultConsumer, Duration responseTimeout,
DefaultMockMvcWebTestClientBuilder clientBuilder) {
super(connector, exchangeFactory, uriBuilderFactory, headers, cookies, entityResultConsumer, responseTimeout,
clientBuilder);
this.builder = clientBuilder;
}

@Override
public MockMvcWebTestClient.Builder mutate() {
return new DefaultMockMvcWebTestClientBuilder(this.builder);
}

@Override
public MockMvcWebTestClient mutateWith(RequestPostProcessor requestPostProcessor) {
return mutate().requestPostProcessor(requestPostProcessor).build();
}

@Override
public MockMvcWebTestClient mutateWith(MockMvcWebTestClientConfigurer configurer) {
return mutate().apply(configurer).build();
}
}
Loading