Apply attributes to the underlying HTTP request

See gh-29958
This commit is contained in:
PhilKes
2023-02-11 14:12:18 +01:00
committed by rstoyanchev
parent 282ee02419
commit 052b6357c8
26 changed files with 374 additions and 24 deletions

View File

@@ -119,6 +119,10 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
.forEach(cookie -> getHeaders().add(HttpHeaders.COOKIE, cookie.toString()));
}
@Override
protected void applyAttributes() {
}
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body))));

View File

@@ -94,6 +94,8 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
@Nullable
private MultiValueMap<String, String> defaultCookies;
private boolean applyAttributes;
@Nullable
private List<ExchangeFilterFunction> filters;
@@ -155,6 +157,7 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
}
this.defaultCookies = (other.defaultCookies != null ?
new LinkedMultiValueMap<>(other.defaultCookies) : null);
this.applyAttributes = other.applyAttributes;
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
this.entityResultConsumer = other.entityResultConsumer;
this.strategies = other.strategies;
@@ -213,6 +216,12 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
return this.defaultCookies;
}
@Override
public WebTestClient.Builder applyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
return this;
}
@Override
public WebTestClient.Builder filter(ExchangeFilterFunction filter) {
Assert.notNull(filter, "ExchangeFilterFunction is required");
@@ -312,22 +321,25 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
this.entityResultConsumer, this.responseTimeout, new DefaultWebTestClientBuilder(this));
}
private static ClientHttpConnector initConnector() {
private ClientHttpConnector initConnector() {
final ClientHttpConnector connector;
if (reactorNettyClientPresent) {
return new ReactorClientHttpConnector();
connector = new ReactorClientHttpConnector();
}
else if (reactorNetty2ClientPresent) {
return new ReactorNetty2ClientHttpConnector();
}
else if (jettyClientPresent) {
return new JettyClientHttpConnector();
connector = new JettyClientHttpConnector();
}
else if (httpComponentsClientPresent) {
return new HttpComponentsClientHttpConnector();
connector = new HttpComponentsClientHttpConnector();
}
else {
return new JdkClientHttpConnector();
connector = new JdkClientHttpConnector();
}
connector.setApplyAttributes(this.applyAttributes);
return connector;
}
private ExchangeStrategies initExchangeStrategies() {

View File

@@ -64,6 +64,8 @@ public class HttpHandlerConnector implements ClientHttpConnector {
private final HttpHandler handler;
private boolean applyAttributes = true;
/**
* Constructor with the {@link HttpHandler} to handle requests with.
@@ -82,6 +84,16 @@ public class HttpHandlerConnector implements ClientHttpConnector {
.subscribeOn(Schedulers.parallel());
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
private Mono<ClientHttpResponse> doConnect(
HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {

View File

@@ -425,6 +425,13 @@ public interface WebTestClient {
*/
Builder defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
/**
* Global option to specify whether or not attributes should be applied to every request,
* if the used {@link ClientHttpConnector} allows it.
* @param applyAttributes whether or not to apply attributes
*/
Builder applyAttributes(boolean applyAttributes);
/**
* Add the given filter to the filter chain.
* @param filter the filter to be added to the chain

View File

@@ -55,6 +55,8 @@ class WiretapConnector implements ClientHttpConnector {
private final Map<String, ClientExchangeInfo> exchanges = new ConcurrentHashMap<>();
private boolean applyAttributes = true;
WiretapConnector(ClientHttpConnector delegate) {
this.delegate = delegate;
@@ -84,6 +86,16 @@ class WiretapConnector implements ClientHttpConnector {
});
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
/**
* Create the {@link ExchangeResult} for the given "request-id" header value.
*/

View File

@@ -86,6 +86,8 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
private final List<RequestPostProcessor> requestPostProcessors;
private boolean applyAttributes = true;
public MockMvcHttpConnector(MockMvc mockMvc) {
this(mockMvc, Collections.emptyList());
@@ -115,6 +117,16 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
}
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
private RequestBuilder adaptRequest(
HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {