Remove headers(HttpHeaders)

This commit removes the headers(HttpHeaders) method on ClientRequest and
ServerResponse, in favor of headers(Consumer<HttpHeaders>), which is
more flexible.
This commit is contained in:
Arjen Poutsma
2017-06-12 14:14:40 +02:00
parent 9bf82dc18f
commit 424bc75fb1
6 changed files with 23 additions and 52 deletions

View File

@@ -88,8 +88,8 @@ public interface ClientRequest {
static Builder from(ClientRequest other) {
Assert.notNull(other, "'other' must not be null");
return new DefaultClientRequestBuilder(other.method(), other.url())
.headers(other.headers())
.cookies(other.cookies())
.headers(headers -> headers.addAll(other.headers()))
.cookies(cookies -> cookies.addAll(other.cookies()))
.body(other.body());
}
@@ -118,13 +118,6 @@ public interface ClientRequest {
*/
Builder header(String headerName, String... headerValues);
/**
* Add the given headers into this request's headers map.
* @param headers the existing HttpHeaders to add from
* @return this builder
*/
Builder headers(HttpHeaders headers);
/**
* Manipulate this request's headers with the given consumer. The
* headers provided to the consumer are "live", so that the consumer can be used to
@@ -144,13 +137,6 @@ public interface ClientRequest {
*/
Builder cookie(String name, String... values);
/**
* Add the given cookies into this request's cookies map.
* @param cookies the existing cookies to copy from
* @return this builder
*/
Builder cookies(MultiValueMap<String, String> cookies);
/**
* Manipulate this request's cookies with the given consumer. The
* map provided to the consumer is "live", so that the consumer can be used to

View File

@@ -72,18 +72,6 @@ class DefaultClientRequestBuilder implements ClientRequest.Builder {
return this;
}
@Override
public ClientRequest.Builder headers(HttpHeaders headers) {
Assert.notNull(headers, "'headers' must not be null");
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
this.headers.add(headerName, headerValue);
}
}
return this;
}
@Override
public ClientRequest.Builder headers(Consumer<HttpHeaders> headersConsumer) {
Assert.notNull(headersConsumer, "'headersConsumer' must not be null");
@@ -99,18 +87,6 @@ class DefaultClientRequestBuilder implements ClientRequest.Builder {
return this;
}
@Override
public ClientRequest.Builder cookies(MultiValueMap<String, String> cookies) {
Assert.notNull(cookies, "'cookies' must not be null");
for (Map.Entry<String, List<String>> entry : cookies.entrySet()) {
String cookieName = entry.getKey();
for (String cookieValue : entry.getValue()) {
this.cookies.add(cookieName, cookieValue);
}
}
return this;
}
@Override
public ClientRequest.Builder cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer) {
Assert.notNull(cookiesConsumer, "'cookiesConsumer' must not be null");

View File

@@ -22,7 +22,6 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@@ -292,7 +291,9 @@ class DefaultWebClient implements WebClient {
}
private ClientRequest.Builder initRequestBuilder() {
return ClientRequest.method(this.httpMethod, this.uri).headers(initHeaders()).cookies(initCookies());
return ClientRequest.method(this.httpMethod, this.uri)
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()));
}
private HttpHeaders initHeaders() {

View File

@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@@ -74,8 +75,9 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
}
@Override
public ServerResponse.BodyBuilder headers(HttpHeaders headers) {
this.headers.putAll(headers);
public ServerResponse.BodyBuilder headers(Consumer<HttpHeaders> headersConsumer) {
Assert.notNull(headersConsumer, "'headersConsumer' must not be null");
headersConsumer.accept(this.headers);
return this;
}

View File

@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
@@ -83,7 +84,7 @@ public interface ServerResponse {
static BodyBuilder from(ServerResponse other) {
Assert.notNull(other, "Other ServerResponse must not be null");
DefaultServerResponseBuilder builder = new DefaultServerResponseBuilder(other.statusCode());
return builder.headers(other.headers());
return builder.headers(headers -> headers.addAll(other.headers()));
}
/**
@@ -207,12 +208,15 @@ public interface ServerResponse {
B header(String headerName, String... headerValues);
/**
* Copy the given headers into the entity's headers map.
* @param headers the existing HttpHeaders to copy from
* Manipulate this response's headers with the given consumer. The
* headers provided to the consumer are "live", so that the consumer can be used to
* {@linkplain HttpHeaders#set(String, String) overwrite} existing header values,
* {@linkplain HttpHeaders#remove(Object) remove} values, or use any of the other
* {@link HttpHeaders} methods.
* @param headersConsumer a function that consumes the {@code HttpHeaders}
* @return this builder
* @see HttpHeaders#add(String, String)
*/
B headers(HttpHeaders headers);
B headers(Consumer<HttpHeaders> headersConsumer);
/**
* Set the set of allowed {@link HttpMethod HTTP methods}, as specified

View File

@@ -253,10 +253,12 @@ public class DefaultServerResponseBuilderTests {
@Test
public void headers() throws Exception {
HttpHeaders headers = new HttpHeaders();
Mono<ServerResponse> result = ServerResponse.ok().headers(headers).build();
HttpHeaders newHeaders = new HttpHeaders();
newHeaders.set("foo", "bar");
Mono<ServerResponse> result =
ServerResponse.ok().headers(headers -> headers.addAll(newHeaders)).build();
StepVerifier.create(result)
.expectNextMatches(response -> headers.equals(response.headers()))
.expectNextMatches(response -> newHeaders.equals(response.headers()))
.expectComplete()
.verify();