Skip to content
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

Fix RestClient to use defaultRequest on RestClient.Builder #32034

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
@@ -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.
Expand Down Expand Up @@ -71,6 +71,7 @@
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Injae Kim
* @since 6.1
* @see RestClient#create()
* @see RestClient#create(String)
Expand Down Expand Up @@ -99,6 +100,9 @@ final class DefaultRestClient implements RestClient {
@Nullable
private final HttpHeaders defaultHeaders;

@Nullable
private final Consumer<RequestHeadersSpec<?>> defaultRequest;

private final List<StatusHandler> defaultStatusHandlers;

private final DefaultRestClientBuilder builder;
Expand All @@ -116,6 +120,7 @@ final class DefaultRestClient implements RestClient {
@Nullable List<ClientHttpRequestInitializer> initializers,
UriBuilderFactory uriBuilderFactory,
@Nullable HttpHeaders defaultHeaders,
@Nullable Consumer<RequestHeadersSpec<?>> defaultRequest,
@Nullable List<StatusHandler> statusHandlers,
List<HttpMessageConverter<?>> messageConverters,
ObservationRegistry observationRegistry,
Expand All @@ -127,6 +132,7 @@ final class DefaultRestClient implements RestClient {
this.interceptors = interceptors;
this.uriBuilderFactory = uriBuilderFactory;
this.defaultHeaders = defaultHeaders;
this.defaultRequest = defaultRequest;
this.defaultStatusHandlers = (statusHandlers != null ? new ArrayList<>(statusHandlers) : new ArrayList<>());
this.messageConverters = messageConverters;
this.observationRegistry = observationRegistry;
Expand Down Expand Up @@ -452,6 +458,9 @@ private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean clo
URI uri = null;
try {
uri = initUri();
if (defaultRequest != null) {
defaultRequest.accept(this);
}
Comment on lines +461 to +463
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private ClientRequest.Builder initRequestBuilder() {
if (defaultRequest != null) {
defaultRequest.accept(this);
}

FYI) almost same usage of defaultRequest on WebClient~!

HttpHeaders headers = initHeaders();
ClientHttpRequest clientRequest = createRequest(uri);
clientRequest.getHeaders().addAll(headers);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -58,6 +58,7 @@
* Default implementation of {@link RestClient.Builder}.
*
* @author Arjen Poutsma
* @author Injae Kim
* @since 6.1
*/
final class DefaultRestClientBuilder implements RestClient.Builder {
Expand Down Expand Up @@ -371,6 +372,7 @@ public RestClient build() {
return new DefaultRestClient(requestFactory,
this.interceptors, this.initializers, uriBuilderFactory,
defaultHeaders,
this.defaultRequest,
this.statusHandlers,
messageConverters,
this.observationRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Injae Kim
*/
class RestClientIntegrationTests {

Expand Down Expand Up @@ -856,6 +857,35 @@ void filterForErrorHandling(ClientHttpRequestFactory requestFactory) {
expectRequestCount(2);
}

@ParameterizedRestClientTest
void defaultRequest(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);

prepareResponse(response ->
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));

String result = this.restClient.mutate()
.defaultRequest(spec -> spec
.header("X-Test-Header-Default", "testDefaultValue")
.header("X-Test-Header", "testDefaultValueShouldBeOverride"))
.build()
.get()
.uri("/greeting")
.header("X-Test-Header", "testHeaderValue")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.body(String.class);

assertThat(result).isEqualTo("Hello Spring!");

expectRequestCount(1);
expectRequest(request -> {
assertThat(request.getHeader("X-Test-Header-Default")).isEqualTo("testDefaultValue");
assertThat(request.getHeader("X-Test-Header")).isEqualTo("testHeaderValue");
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo("application/json");
assertThat(request.getPath()).isEqualTo("/greeting");
});
}

private void prepareResponse(Consumer<MockResponse> consumer) {
MockResponse response = new MockResponse();
Expand Down