MockMvcHttpConnector supports RequestPostProcessor's
Closes gh-31298
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* 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.
|
||||
@@ -80,7 +80,7 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
|
||||
private final WebHttpHandlerBuilder httpHandlerBuilder;
|
||||
|
||||
@Nullable
|
||||
private final ClientHttpConnector connector;
|
||||
private ClientHttpConnector connector;
|
||||
|
||||
@Nullable
|
||||
private String baseUrl;
|
||||
@@ -277,6 +277,12 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebTestClient.Builder clientConnector(ClientHttpConnector connector) {
|
||||
this.connector = connector;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebTestClient build() {
|
||||
ClientHttpConnector connectorToUse = this.connector;
|
||||
|
||||
@@ -512,6 +512,18 @@ public interface WebTestClient {
|
||||
*/
|
||||
Builder responseTimeout(Duration timeout);
|
||||
|
||||
/**
|
||||
* Set the {@link ClientHttpConnector} to use.
|
||||
* <p>By default, this is initialized and set internally. However, the
|
||||
* connector may also be prepared externally and passed via
|
||||
* {@link WebTestClient#bindToServer(ClientHttpConnector)} such as for
|
||||
* {@code MockMvcWebTestClient} tests, and in that case you can use this
|
||||
* from {@link #mutateWith(WebTestClientConfigurer)} to replace it.
|
||||
* @param connector the connector to use
|
||||
* @since 6.1
|
||||
*/
|
||||
Builder clientConnector(ClientHttpConnector connector);
|
||||
|
||||
/**
|
||||
* Apply the given configurer to this builder instance.
|
||||
* <p>This can be useful for applying pre-packaged customizations.
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.client;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -56,6 +57,7 @@ import org.springframework.test.web.servlet.RequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -82,9 +84,16 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
|
||||
|
||||
private final MockMvc mockMvc;
|
||||
|
||||
private final List<RequestPostProcessor> requestPostProcessors;
|
||||
|
||||
|
||||
public MockMvcHttpConnector(MockMvc mockMvc) {
|
||||
this(mockMvc, Collections.emptyList());
|
||||
}
|
||||
|
||||
private MockMvcHttpConnector(MockMvc mockMvc, List<RequestPostProcessor> requestPostProcessors) {
|
||||
this.mockMvc = mockMvc;
|
||||
this.requestPostProcessors = new ArrayList<>(requestPostProcessors);
|
||||
}
|
||||
|
||||
|
||||
@@ -135,6 +144,8 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
|
||||
}
|
||||
}
|
||||
|
||||
this.requestPostProcessors.forEach(requestBuilder::with);
|
||||
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
@@ -207,6 +218,15 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
|
||||
return clientResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance that applies the given {@link RequestPostProcessor}s
|
||||
* to performed requests.
|
||||
* @since 6.1
|
||||
*/
|
||||
public MockMvcHttpConnector with(List<RequestPostProcessor> postProcessors) {
|
||||
return new MockMvcHttpConnector(this.mockMvc, postProcessors);
|
||||
}
|
||||
|
||||
|
||||
private static class MockMvcServerClientHttpResponse
|
||||
extends MockClientHttpResponse implements MockServerClientHttpResponse {
|
||||
|
||||
Reference in New Issue
Block a user