diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index 0b46ac391ffa..664640874844 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -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. @@ -370,12 +370,28 @@ public ResponseSpec exchange() { private ClientRequest.Builder initRequestBuilder() { ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri()) - .headers(this::initHeaders) - .cookies(this::initCookies) + .headers(headersToUse -> { + if (!CollectionUtils.isEmpty(defaultHeaders)) { + headersToUse.putAll(defaultHeaders); + } + if (!CollectionUtils.isEmpty(this.headers)) { + headersToUse.putAll(this.headers); + } + }) + .cookies(cookiesToUse -> { + if (!CollectionUtils.isEmpty(defaultCookies)) { + cookiesToUse.putAll(defaultCookies); + } + if (!CollectionUtils.isEmpty(this.cookies)) { + cookiesToUse.putAll(this.cookies); + } + }) .attributes(attributes -> attributes.putAll(this.attributes)); + if (this.httpRequestConsumer != null) { builder.httpRequest(this.httpRequestConsumer); } + return builder; } @@ -383,23 +399,6 @@ private URI initUri() { return (this.uri != null ? this.uri : uriBuilderFactory.expand("")); } - private void initHeaders(HttpHeaders out) { - if (!CollectionUtils.isEmpty(defaultHeaders)) { - out.putAll(defaultHeaders); - } - if (!CollectionUtils.isEmpty(this.headers)) { - out.putAll(this.headers); - } - } - - private void initCookies(MultiValueMap out) { - if (!CollectionUtils.isEmpty(defaultCookies)) { - out.putAll(defaultCookies); - } - if (!CollectionUtils.isEmpty(this.cookies)) { - out.putAll(this.cookies); - } - } } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index d79b51308076..cf3828cd70ed 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -30,7 +30,6 @@ import java.util.function.Function; import java.util.function.IntPredicate; import java.util.function.Predicate; -import java.util.function.Supplier; import io.micrometer.observation.Observation; import io.micrometer.observation.ObservationRegistry; @@ -43,7 +42,6 @@ import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpRequest; import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -394,30 +392,7 @@ public RequestHeadersSpec syncBody(Object body) { @Override public ResponseSpec retrieve() { return new DefaultResponseSpec( - exchange(), this::createRequest, DefaultWebClient.this.defaultStatusHandlers); - } - - private HttpRequest createRequest() { - return new HttpRequest() { - private final URI uri = initUri(); - - @Override - public HttpMethod getMethod() { - return httpMethod; - } - - @Override - public URI getURI() { - return this.uri; - } - - @Override - public HttpHeaders getHeaders() { - HttpHeaders headers = new HttpHeaders(); - initHeaders(headers); - return headers; - } - }; + this.httpMethod, initUri(), exchange(), DefaultWebClient.this.defaultStatusHandlers); } @Override @@ -528,9 +503,12 @@ private static class DefaultResponseSpec implements ResponseSpec { private static final StatusHandler DEFAULT_STATUS_HANDLER = new StatusHandler(STATUS_CODE_ERROR, ClientResponse::createException); - private final Mono responseMono; - private final Supplier requestSupplier; + private final HttpMethod httpMethod; + + private final URI uri; + + private final Mono responseMono; private final List statusHandlers = new ArrayList<>(1); @@ -538,11 +516,12 @@ private static class DefaultResponseSpec implements ResponseSpec { DefaultResponseSpec( - Mono responseMono, Supplier requestSupplier, + HttpMethod httpMethod, URI uri, Mono responseMono, List defaultStatusHandlers) { + this.httpMethod = httpMethod; + this.uri = uri; this.responseMono = responseMono; - this.requestSupplier = requestSupplier; this.statusHandlers.addAll(defaultStatusHandlers); this.statusHandlers.add(DEFAULT_STATUS_HANDLER); this.defaultStatusHandlerCount = this.statusHandlers.size(); @@ -696,21 +675,14 @@ private Mono applyStatusHandlers(ClientResponse response) { exMono = releaseIfNotConsumed(response, ex2); } Mono result = exMono.flatMap(Mono::error); - HttpRequest request = this.requestSupplier.get(); - return insertCheckpoint(result, statusCode, request); + return result.checkpoint(statusCode + " from " + + this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]"); } } return null; } - private Mono insertCheckpoint(Mono result, HttpStatusCode statusCode, HttpRequest request) { - HttpMethod method = request.getMethod(); - URI uri = getUriToLog(request); - return result.checkpoint(statusCode + " from " + method + " " + uri + " [DefaultWebClient]"); - } - - private static URI getUriToLog(HttpRequest request) { - URI uri = request.getURI(); + private static URI getUriToLog(URI uri) { if (StringUtils.hasText(uri.getQuery())) { try { uri = new URI(uri.getScheme(), uri.getHost(), uri.getPath(), null);