Copy cookies and hints in built ServerResponse

Closes gh-22481
This commit is contained in:
Arjen Poutsma 2019-03-26 15:31:15 +01:00
parent 0ca8428603
commit 6324a1b3fa
4 changed files with 32 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -202,16 +202,14 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
private final BodyInserter<T, ? super ServerHttpResponse> inserter; private final BodyInserter<T, ? super ServerHttpResponse> inserter;
private final Map<String, Object> hints;
public DefaultEntityResponse(int statusCode, HttpHeaders headers, public DefaultEntityResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies, T entity, MultiValueMap<String, ResponseCookie> cookies, T entity,
BodyInserter<T, ? super ServerHttpResponse> inserter, Map<String, Object> hints) { BodyInserter<T, ? super ServerHttpResponse> inserter, Map<String, Object> hints) {
super(statusCode, headers, cookies); super(statusCode, headers, cookies, hints);
this.entity = entity; this.entity = entity;
this.inserter = inserter; this.inserter = inserter;
this.hints = hints;
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -168,7 +168,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
public DefaultRenderingResponse(int statusCode, HttpHeaders headers, public DefaultRenderingResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies, String name, Map<String, Object> model) { MultiValueMap<String, ResponseCookie> cookies, String name, Map<String, Object> model) {
super(statusCode, headers, cookies); super(statusCode, headers, cookies, Collections.emptyMap());
this.name = name; this.name = name;
this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model)); this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.net.URI;
import java.time.Instant; import java.time.Instant;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -73,9 +74,16 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
public DefaultServerResponseBuilder(ServerResponse other) { public DefaultServerResponseBuilder(ServerResponse other) {
Assert.notNull(other, "ServerResponse must not be null"); Assert.notNull(other, "ServerResponse must not be null");
this.statusCode = (other instanceof AbstractServerResponse ?
((AbstractServerResponse) other).statusCode : other.statusCode().value());
this.headers.addAll(other.headers()); this.headers.addAll(other.headers());
this.cookies.addAll(other.cookies());
if (other instanceof AbstractServerResponse) {
AbstractServerResponse abstractOther = (AbstractServerResponse) other;
this.statusCode = abstractOther.statusCode;
this.hints.putAll(abstractOther.hints);
}
else {
this.statusCode = other.statusCode().value();
}
} }
public DefaultServerResponseBuilder(HttpStatus status) { public DefaultServerResponseBuilder(HttpStatus status) {
@ -289,12 +297,17 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
private final MultiValueMap<String, ResponseCookie> cookies; private final MultiValueMap<String, ResponseCookie> cookies;
final Map<String, Object> hints;
protected AbstractServerResponse( protected AbstractServerResponse(
int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies) { int statusCode, HttpHeaders headers, MultiValueMap<String, ResponseCookie> cookies,
Map<String, Object> hints) {
this.statusCode = statusCode; this.statusCode = statusCode;
this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(cookies)); this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(cookies));
this.hints = hints;
} }
@Override @Override
@ -361,7 +374,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
MultiValueMap<String, ResponseCookie> cookies, MultiValueMap<String, ResponseCookie> cookies,
BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) { BiFunction<ServerWebExchange, Context, Mono<Void>> writeFunction) {
super(statusCode, headers, cookies); super(statusCode, headers, cookies, Collections.emptyMap());
Assert.notNull(writeFunction, "BiFunction must not be null"); Assert.notNull(writeFunction, "BiFunction must not be null");
this.writeFunction = writeFunction; this.writeFunction = writeFunction;
} }
@ -377,16 +390,14 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
private final BodyInserter<T, ? super ServerHttpResponse> inserter; private final BodyInserter<T, ? super ServerHttpResponse> inserter;
private final Map<String, Object> hints;
public BodyInserterResponse(int statusCode, HttpHeaders headers, public BodyInserterResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies, MultiValueMap<String, ResponseCookie> cookies,
BodyInserter<T, ? super ServerHttpResponse> body, Map<String, Object> hints) { BodyInserter<T, ? super ServerHttpResponse> body, Map<String, Object> hints) {
super(statusCode, headers, cookies); super(statusCode, headers, cookies, hints);
Assert.notNull(body, "BodyInserter must not be null"); Assert.notNull(body, "BodyInserter must not be null");
this.inserter = body; this.inserter = body;
this.hints = hints;
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -65,11 +65,17 @@ public class DefaultServerResponseBuilderTests {
@Test @Test
public void from() { public void from() {
ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block(); ResponseCookie cookie = ResponseCookie.from("foo", "bar").build();
ServerResponse other = ServerResponse.ok().header("foo", "bar")
.cookie(cookie)
.hint("foo", "bar")
.build().block();
Mono<ServerResponse> result = ServerResponse.from(other).build(); Mono<ServerResponse> result = ServerResponse.from(other).build();
StepVerifier.create(result) StepVerifier.create(result)
.expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) && .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) &&
"bar".equals(response.headers().getFirst("foo"))) "bar".equals(response.headers().getFirst("foo")) &&
cookie.equals(response.cookies().getFirst("foo")))
.expectComplete() .expectComplete()
.verify(); .verify();
} }