Skip to content

Include default request options early on. #32250

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

Closed
wants to merge 1 commit into from
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 @@ -220,6 +220,11 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {

DefaultRequestBodyUriSpec(HttpMethod httpMethod) {
this.httpMethod = httpMethod;

// Add defaultRequest option to WebClient.Builder
if (defaultRequest != null) {
defaultRequest.accept(this);
}
}

@Override
Expand Down Expand Up @@ -479,9 +484,6 @@ public Mono<ClientResponse> exchange() {
}

private ClientRequest.Builder initRequestBuilder() {
if (defaultRequest != null) {
defaultRequest.accept(this);
}
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(this::initHeaders)
.cookies(this::initCookies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.function.Function;
import java.util.function.Predicate;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -524,6 +525,28 @@ public void onStatusHandlersApplyForToEntityMethods() {
testStatusHandlerForToEntity(spec.toEntityFlux(BodyExtractors.toFlux(String.class)));
}

@Test
public void onOverrideHeadersUsingBuilder() {
// Given
WebClient client = this.builder.defaultRequest(spec -> spec.accept(MediaType.APPLICATION_JSON))
.build();
client.get().uri("/path")
.accept(MediaType.IMAGE_PNG)
.retrieve()
.bodyToMono(Void.class)
.block(Duration.ofSeconds(10));

// When
ClientRequest request = verifyAndGetRequest();

// Then
assertThat(request)
.extracting(ClientRequest::headers)
.extracting(HttpHeaders::getAccept)
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsExactly(MediaType.IMAGE_PNG);
}

private void testStatusHandlerForToEntity(Publisher<?> responsePublisher) {
StepVerifier.create(responsePublisher).expectError(WebClientResponseException.class).verify();
}
Expand Down