Skip to content

Commit

Permalink
Enable RestClient.defaultRequest
Browse files Browse the repository at this point in the history
This commit enables the defaultRequest setting in the RestClient
builder.

Closes gh-32028
  • Loading branch information
poutsma committed Jan 17, 2024
1 parent 682f471 commit 0ada78a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
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 @@ -99,6 +99,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 +119,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 +131,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 @@ -451,6 +456,9 @@ private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean clo
Observation observation = null;
URI uri = null;
try {
if (DefaultRestClient.this.defaultRequest != null) {
DefaultRestClient.this.defaultRequest.accept(this);
}
uri = initUri();
HttpHeaders headers = initHeaders();
ClientHttpRequest clientRequest = createRequest(uri);
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 @@ -371,6 +371,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 @@ -856,6 +856,50 @@ void filterForErrorHandling(ClientHttpRequestFactory requestFactory) {
expectRequestCount(2);
}

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

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

RestClient headersClient = this.restClient.mutate()
.defaultHeaders(headers -> headers.add("foo", "bar"))
.build();

String result = headersClient.get()
.uri("/greeting")
.retrieve()
.body(String.class);

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

expectRequestCount(1);
expectRequest(request -> assertThat(request.getHeader("foo")).isEqualTo("bar"));
}

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

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

RestClient headersClient = this.restClient.mutate()
.defaultRequest(request -> request.header("foo", "bar"))
.build();

String result = headersClient.get()
.uri("/greeting")
.retrieve()
.body(String.class);

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

expectRequestCount(1);
expectRequest(request -> assertThat(request.getHeader("foo")).isEqualTo("bar"));
}


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

0 comments on commit 0ada78a

Please sign in to comment.