Remove applyAttributes flag from contribution

See gh-29958
This commit is contained in:
rstoyanchev
2024-03-11 17:36:45 +00:00
parent 052b6357c8
commit 8af1d8e842
21 changed files with 47 additions and 254 deletions

View File

@@ -94,8 +94,6 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
@Nullable
private MultiValueMap<String, String> defaultCookies;
private boolean applyAttributes;
@Nullable
private List<ExchangeFilterFunction> filters;
@@ -157,7 +155,6 @@ 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;
@@ -216,12 +213,6 @@ 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");
@@ -321,25 +312,22 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
this.entityResultConsumer, this.responseTimeout, new DefaultWebTestClientBuilder(this));
}
private ClientHttpConnector initConnector() {
final ClientHttpConnector connector;
private static ClientHttpConnector initConnector() {
if (reactorNettyClientPresent) {
connector = new ReactorClientHttpConnector();
return new ReactorClientHttpConnector();
}
else if (reactorNetty2ClientPresent) {
return new ReactorNetty2ClientHttpConnector();
}
else if (jettyClientPresent) {
connector = new JettyClientHttpConnector();
return new JettyClientHttpConnector();
}
else if (httpComponentsClientPresent) {
connector = new HttpComponentsClientHttpConnector();
return new HttpComponentsClientHttpConnector();
}
else {
connector = new JdkClientHttpConnector();
return new JdkClientHttpConnector();
}
connector.setApplyAttributes(this.applyAttributes);
return connector;
}
private ExchangeStrategies initExchangeStrategies() {

View File

@@ -64,8 +64,6 @@ public class HttpHandlerConnector implements ClientHttpConnector {
private final HttpHandler handler;
private boolean applyAttributes = true;
/**
* Constructor with the {@link HttpHandler} to handle requests with.
@@ -84,16 +82,6 @@ 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,13 +425,6 @@ 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,8 +55,6 @@ class WiretapConnector implements ClientHttpConnector {
private final Map<String, ClientExchangeInfo> exchanges = new ConcurrentHashMap<>();
private boolean applyAttributes = true;
WiretapConnector(ClientHttpConnector delegate) {
this.delegate = delegate;
@@ -86,16 +84,6 @@ 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,8 +86,6 @@ public class MockMvcHttpConnector implements ClientHttpConnector {
private final List<RequestPostProcessor> requestPostProcessors;
private boolean applyAttributes = true;
public MockMvcHttpConnector(MockMvc mockMvc) {
this(mockMvc, Collections.emptyList());
@@ -117,16 +115,6 @@ 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) {

View File

@@ -18,7 +18,6 @@ package org.springframework.test.web.reactive.server;
import java.net.URI;
import java.time.Duration;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
@@ -49,22 +48,7 @@ public class WiretapConnectorTests {
public void captureAndClaim() {
ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
ClientHttpConnector connector = new ClientHttpConnector() {
@Override
public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
return requestCallback.apply(request).then(Mono.just(response));
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
}
@Override
public boolean getApplyAttributes() {
return false;
}
};
ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));
ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create("/test"))
.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -60,8 +60,6 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
private final Map<String, Object> attributes;
private final boolean applyAttributes;
private final AtomicReference<State> state = new AtomicReference<>(State.NEW);
private final List<Supplier<? extends Publisher<Void>>> commitActions = new ArrayList<>(4);
@@ -71,19 +69,14 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
public AbstractClientHttpRequest() {
this(new HttpHeaders(), false);
this(new HttpHeaders());
}
public AbstractClientHttpRequest(boolean applyAttributes) {
this(new HttpHeaders(), applyAttributes);
}
public AbstractClientHttpRequest(HttpHeaders headers, boolean applyAttributes) {
public AbstractClientHttpRequest(HttpHeaders headers) {
Assert.notNull(headers, "HttpHeaders must not be null");
this.headers = headers;
this.cookies = new LinkedMultiValueMap<>();
this.attributes = new LinkedHashMap<>();
this.applyAttributes = applyAttributes;
}
@@ -161,9 +154,7 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
Mono.fromRunnable(() -> {
applyHeaders();
applyCookies();
if (this.applyAttributes) {
applyAttributes();
}
applyAttributes();
this.state.set(State.COMMITTED);
}));
@@ -193,9 +184,11 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
protected abstract void applyCookies();
/**
* Add additional attributes from {@link #getAttributes()} to the underlying request.
* Add attributes from {@link #getAttributes()} to the underlying request.
* This method is called once only.
* @since 6.2
*/
protected abstract void applyAttributes();
protected void applyAttributes() {
}
}

View File

@@ -48,14 +48,4 @@ public interface ClientHttpConnector {
Mono<ClientHttpResponse> connect(HttpMethod method, URI uri,
Function<? super ClientHttpRequest, Mono<Void>> requestCallback);
/**
* Set whether or not attributes should be applied to the underlying http-client library request.
*/
void setApplyAttributes(boolean applyAttributes);
/**
* Whether or not attributes should be applied to the underlying http-client library request.
*/
boolean getApplyAttributes();
}

View File

@@ -59,7 +59,6 @@ public class HttpComponentsClientHttpConnector implements ClientHttpConnector, C
private DataBufferFactory dataBufferFactory = DefaultDataBufferFactory.sharedInstance;
private boolean applyAttributes = true;
/**
* Default constructor that creates and starts a new instance of {@link CloseableHttpAsyncClient}.
@@ -68,7 +67,6 @@ public class HttpComponentsClientHttpConnector implements ClientHttpConnector, C
this(HttpAsyncClients.createDefault());
}
/**
* Constructor with a pre-configured {@link CloseableHttpAsyncClient} instance.
* @param client the client to use
@@ -113,22 +111,11 @@ public class HttpComponentsClientHttpConnector implements ClientHttpConnector, C
context.setCookieStore(new BasicCookieStore());
}
HttpComponentsClientHttpRequest request = new HttpComponentsClientHttpRequest(
method, uri, context, this.dataBufferFactory, this.applyAttributes);
HttpComponentsClientHttpRequest request =
new HttpComponentsClientHttpRequest(method, uri, context, this.dataBufferFactory);
return requestCallback.apply(request).then(Mono.defer(() -> execute(request, context)));
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
private Mono<ClientHttpResponse> execute(HttpComponentsClientHttpRequest request, HttpClientContext context) {
AsyncRequestProducer requestProducer = request.toRequestProducer();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -66,8 +66,8 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest {
public HttpComponentsClientHttpRequest(HttpMethod method, URI uri, HttpClientContext context,
DataBufferFactory dataBufferFactory, boolean applyAttributes) {
super(applyAttributes);
DataBufferFactory dataBufferFactory) {
this.context = context;
this.httpRequest = new BasicHttpRequest(method.name(), uri);
this.dataBufferFactory = dataBufferFactory;

View File

@@ -96,7 +96,7 @@ public class JdkClientHttpConnector implements ClientHttpConnector {
public Mono<ClientHttpResponse> connect(
HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
JdkClientHttpRequest jdkClientHttpRequest = new JdkClientHttpRequest(method, uri, this.bufferFactory, getApplyAttributes());
JdkClientHttpRequest jdkClientHttpRequest = new JdkClientHttpRequest(method, uri, this.bufferFactory);
return requestCallback.apply(jdkClientHttpRequest).then(Mono.defer(() -> {
HttpRequest httpRequest = jdkClientHttpRequest.getNativeRequest();
@@ -109,19 +109,4 @@ public class JdkClientHttpConnector implements ClientHttpConnector {
}));
}
/**
* Sets nothing, since {@link JdkClientHttpConnector} does not offer any possibility to add attributes.
*/
@Override
public void setApplyAttributes(boolean applyAttributes) {
}
/**
* Returns false, since {@link JdkClientHttpConnector} does not offer any possibility to add attributes.
*/
@Override
public boolean getApplyAttributes() {
return false;
}
}

View File

@@ -56,8 +56,7 @@ class JdkClientHttpRequest extends AbstractClientHttpRequest {
private final HttpRequest.Builder builder;
public JdkClientHttpRequest(HttpMethod httpMethod, URI uri, DataBufferFactory bufferFactory, boolean applyAttributes) {
super(applyAttributes);
public JdkClientHttpRequest(HttpMethod httpMethod, URI uri, DataBufferFactory bufferFactory) {
Assert.notNull(httpMethod, "HttpMethod is required");
Assert.notNull(uri, "URI is required");
Assert.notNull(bufferFactory, "DataBufferFactory is required");
@@ -113,15 +112,6 @@ class JdkClientHttpRequest extends AbstractClientHttpRequest {
.flatMap(List::stream).map(HttpCookie::toString).collect(Collectors.joining(";")));
}
/**
* Not implemented, since {@link HttpRequest} does not offer any possibility to add request attributes.
*/
@Override
protected void applyAttributes() {
// TODO
throw new RuntimeException(String.format("Using attributes is not available for %s", HttpRequest.class.getName()));
}
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return doCommit(() -> {

View File

@@ -52,8 +52,6 @@ public class JettyClientHttpConnector implements ClientHttpConnector {
private DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
private boolean applyAttributes = true;
/**
* Default constructor that creates a new instance of {@link HttpClient}.
@@ -128,21 +126,11 @@ public class JettyClientHttpConnector implements ClientHttpConnector {
}
Request jettyRequest = this.httpClient.newRequest(uri).method(method.toString());
JettyClientHttpRequest request = new JettyClientHttpRequest(jettyRequest, this.bufferFactory, getApplyAttributes());
JettyClientHttpRequest request = new JettyClientHttpRequest(jettyRequest, this.bufferFactory);
return requestCallback.apply(request).then(execute(request));
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
private Mono<ClientHttpResponse> execute(JettyClientHttpRequest request) {
return Mono.fromDirect(request.toReactiveRequest()
.response((reactiveResponse, chunkPublisher) -> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -55,8 +55,7 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
private final ReactiveRequest.Builder builder;
public JettyClientHttpRequest(Request jettyRequest, DataBufferFactory bufferFactory, boolean applyAttributes) {
super(applyAttributes);
public JettyClientHttpRequest(Request jettyRequest, DataBufferFactory bufferFactory) {
this.jettyRequest = jettyRequest;
this.bufferFactory = bufferFactory;
this.builder = ReactiveRequest.newBuilder(this.jettyRequest).abortOnCancel(true);

View File

@@ -66,7 +66,6 @@ public class ReactorClientHttpConnector implements ClientHttpConnector, SmartLif
private final Object lifecycleMonitor = new Object();
private boolean applyAttributes = true;
/**
* Default constructor. Initializes {@link HttpClient} via:
@@ -171,20 +170,10 @@ public class ReactorClientHttpConnector implements ClientHttpConnector, SmartLif
return requestSender.uri(uri.toString());
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
private ReactorClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpClientRequest request,
NettyOutbound nettyOutbound) {
return new ReactorClientHttpRequest(method, uri, request, nettyOutbound, this.applyAttributes);
return new ReactorClientHttpRequest(method, uri, request, nettyOutbound);
}
@Override

View File

@@ -61,8 +61,7 @@ class ReactorClientHttpRequest extends AbstractClientHttpRequest implements Zero
private final NettyDataBufferFactory bufferFactory;
public ReactorClientHttpRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound, boolean applyAttributes) {
super(applyAttributes);
public ReactorClientHttpRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound) {
this.httpMethod = method;
this.uri = uri;
this.request = request;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -46,8 +46,6 @@ public class ReactorNetty2ClientHttpConnector implements ClientHttpConnector {
private final HttpClient httpClient;
private boolean applyAttributes = true;
/**
* Default constructor. Initializes {@link HttpClient} via:
@@ -128,20 +126,10 @@ public class ReactorNetty2ClientHttpConnector implements ClientHttpConnector {
});
}
@Override
public void setApplyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
}
@Override
public boolean getApplyAttributes() {
return this.applyAttributes;
}
private ReactorNetty2ClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpClientRequest request,
NettyOutbound nettyOutbound) {
return new ReactorNetty2ClientHttpRequest(method, uri, request, nettyOutbound, getApplyAttributes());
return new ReactorNetty2ClientHttpRequest(method, uri, request, nettyOutbound);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -62,8 +62,9 @@ class ReactorNetty2ClientHttpRequest extends AbstractClientHttpRequest implement
private final Netty5DataBufferFactory bufferFactory;
public ReactorNetty2ClientHttpRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound, boolean applyAttributes) {
super(applyAttributes);
public ReactorNetty2ClientHttpRequest(
HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound) {
this.httpMethod = method;
this.uri = uri;
this.request = request;

View File

@@ -89,8 +89,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
@Nullable
private MultiValueMap<String, String> defaultCookies;
private boolean applyAttributes;
@Nullable
private Consumer<WebClient.RequestHeadersSpec<?>> defaultRequest;
@@ -139,7 +137,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
this.defaultCookies = (other.defaultCookies != null ?
new LinkedMultiValueMap<>(other.defaultCookies) : null);
this.applyAttributes = other.applyAttributes;
this.defaultRequest = other.defaultRequest;
this.statusHandlers = (other.statusHandlers != null ? new LinkedHashMap<>(other.statusHandlers) : null);
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
@@ -203,12 +200,6 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
return this;
}
@Override
public WebClient.Builder applyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
return this;
}
private MultiValueMap<String, String> initCookies() {
if (this.defaultCookies == null) {
this.defaultCookies = new LinkedMultiValueMap<>(3);
@@ -344,24 +335,21 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
}
private ClientHttpConnector initConnector() {
final ClientHttpConnector connector;
if (reactorNettyClientPresent) {
connector = new ReactorClientHttpConnector();
return new ReactorClientHttpConnector();
}
else if (reactorNetty2ClientPresent) {
return new ReactorNetty2ClientHttpConnector();
}
else if (jettyClientPresent) {
connector = new JettyClientHttpConnector();
return new JettyClientHttpConnector();
}
else if (httpComponentsClientPresent) {
connector = new HttpComponentsClientHttpConnector();
return new HttpComponentsClientHttpConnector();
}
else {
connector = new JdkClientHttpConnector();
return new JdkClientHttpConnector();
}
connector.setApplyAttributes(this.applyAttributes);
return connector;
}
private ExchangeStrategies initExchangeStrategies() {

View File

@@ -250,13 +250,6 @@ public interface WebClient {
*/
Builder defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
/**
* Global option to specify whether or not the request attributes should be applied
* to the underlying http-client request, if the used {@link ClientHttpConnector} allows it.
* @param applyAttributes whether or not to apply the attributes
*/
Builder applyAttributes(boolean applyAttributes);
/**
* Provide a consumer to customize every request being built.
* @param defaultRequest the consumer to use for modifying requests

View File

@@ -192,20 +192,8 @@ class WebClientIntegrationTests {
}
@ParameterizedWebClientTest
void applyAttributesInNativeRequest(ClientHttpConnector connector) {
void applyAttributesToNativeRequest(ClientHttpConnector connector) {
startServer(connector);
connector.setApplyAttributes(true);
checkAttributesInNativeRequest(true);
}
@ParameterizedWebClientTest
void dontApplyAttributesInNativeRequest(ClientHttpConnector connector) {
startServer(connector);
connector.setApplyAttributes(false);
checkAttributesInNativeRequest(false);
}
private void checkAttributesInNativeRequest(boolean expectAttributesApplied){
prepareResponse(response -> {});
final AtomicReference<Object> nativeRequest = new AtomicReference<>();
@@ -215,36 +203,22 @@ class WebClientIntegrationTests {
.httpRequest(clientHttpRequest -> nativeRequest.set(clientHttpRequest.getNativeRequest()))
.retrieve()
.bodyToMono(Void.class);
StepVerifier.create(result)
.expectComplete()
.verify();
StepVerifier.create(result).expectComplete().verify();
if (nativeRequest.get() instanceof ChannelOperations<?,?> nativeReq) {
Attribute<Map<String, Object>> attributes = nativeReq.channel().attr(AttributeKey.valueOf("attributes"));
if (expectAttributesApplied) {
assertThat(attributes.get()).isNotNull();
assertThat(attributes.get()).containsEntry("foo", "bar");
}
else {
assertThat(attributes.get()).isNull();
}
assertThat(attributes.get()).isNotNull();
assertThat(attributes.get()).containsEntry("foo", "bar");
}
else if (nativeRequest.get() instanceof reactor.netty5.channel.ChannelOperations<?,?> nativeReq) {
io.netty5.util.Attribute<Map<String, Object>> attributes = nativeReq.channel().attr(io.netty5.util.AttributeKey.valueOf("attributes"));
if (expectAttributesApplied) {
assertThat(attributes.get()).isNotNull();
assertThat(attributes.get()).containsEntry("foo", "bar");
}
else {
assertThat(attributes.get()).isNull();
}
io.netty5.util.Attribute<Map<String, Object>> attributes =
nativeReq.channel().attr(io.netty5.util.AttributeKey.valueOf("attributes"));
assertThat(attributes.get()).isNotNull();
assertThat(attributes.get()).containsEntry("foo", "bar");
}
else if (nativeRequest.get() instanceof Request nativeReq) {
if (expectAttributesApplied) {
assertThat(nativeReq.getAttributes()).containsEntry("foo", "bar");
}
else {
assertThat(nativeReq.getAttributes()).doesNotContainEntry("foo", "bar");
}
assertThat(nativeReq.getAttributes()).containsEntry("foo", "bar");
}
else if (nativeRequest.get() instanceof org.apache.hc.core5.http.HttpRequest nativeReq) {
// TODO get attributes from HttpClientContext