Copy cookies and hints to ServerResponse builders
Closes gh-22351
This commit is contained in:
		
							parent
							
								
									6324a1b3fa
								
							
						
					
					
						commit
						c152d246a8
					
				|  | @ -157,6 +157,12 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> { | |||
| 		return this; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public EntityResponse.Builder<T> hints(Consumer<Map<String, Object>> hintsConsumer) { | ||||
| 		hintsConsumer.accept(this.hints); | ||||
| 		return this; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public EntityResponse.Builder<T> lastModified(ZonedDateTime lastModified) { | ||||
| 		this.headers.setLastModified(lastModified); | ||||
|  |  | |||
|  | @ -165,6 +165,12 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { | |||
| 		return this; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public ServerResponse.BodyBuilder hints(Consumer<Map<String, Object>> hintsConsumer) { | ||||
| 		hintsConsumer.accept(this.hints); | ||||
| 		return this; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) { | ||||
| 		this.headers.setLastModified(lastModified); | ||||
|  | @ -222,8 +228,10 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { | |||
| 
 | ||||
| 		return new DefaultEntityResponseBuilder<>(publisher, | ||||
| 				BodyInserters.fromPublisher(publisher, elementClass)) | ||||
| 				.headers(this.headers) | ||||
| 				.status(this.statusCode) | ||||
| 				.headers(this.headers) | ||||
| 				.cookies(cookies -> cookies.addAll(this.cookies)) | ||||
| 				.hints(hints -> hints.putAll(this.hints)) | ||||
| 				.build() | ||||
| 				.map(entityResponse -> entityResponse); | ||||
| 	} | ||||
|  | @ -237,8 +245,10 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { | |||
| 
 | ||||
| 		return new DefaultEntityResponseBuilder<>(publisher, | ||||
| 				BodyInserters.fromPublisher(publisher, typeReference)) | ||||
| 				.headers(this.headers) | ||||
| 				.status(this.statusCode) | ||||
| 				.headers(this.headers) | ||||
| 				.cookies(cookies -> cookies.addAll(this.cookies)) | ||||
| 				.hints(hints -> hints.putAll(this.hints)) | ||||
| 				.build() | ||||
| 				.map(entityResponse -> entityResponse); | ||||
| 	} | ||||
|  | @ -251,8 +261,10 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { | |||
| 
 | ||||
| 		return new DefaultEntityResponseBuilder<>(body, | ||||
| 				BodyInserters.fromObject(body)) | ||||
| 				.headers(this.headers) | ||||
| 				.status(this.statusCode) | ||||
| 				.headers(this.headers) | ||||
| 				.cookies(cookies -> cookies.addAll(this.cookies)) | ||||
| 				.hints(hints -> hints.putAll(this.hints)) | ||||
| 				.build() | ||||
| 				.map(entityResponse -> entityResponse); | ||||
| 	} | ||||
|  | @ -266,8 +278,9 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { | |||
| 	@Override | ||||
| 	public Mono<ServerResponse> render(String name, Object... modelAttributes) { | ||||
| 		return new DefaultRenderingResponseBuilder(name) | ||||
| 				.headers(this.headers) | ||||
| 				.status(this.statusCode) | ||||
| 				.headers(this.headers) | ||||
| 				.cookies(cookies -> cookies.addAll(this.cookies)) | ||||
| 				.modelAttributes(modelAttributes) | ||||
| 				.build() | ||||
| 				.map(renderingResponse -> renderingResponse); | ||||
|  | @ -276,8 +289,9 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { | |||
| 	@Override | ||||
| 	public Mono<ServerResponse> render(String name, Map<String, ?> model) { | ||||
| 		return new DefaultRenderingResponseBuilder(name) | ||||
| 				.headers(this.headers) | ||||
| 				.status(this.statusCode) | ||||
| 				.headers(this.headers) | ||||
| 				.cookies(cookies -> cookies.addAll(this.cookies)) | ||||
| 				.modelAttributes(model) | ||||
| 				.build() | ||||
| 				.map(renderingResponse -> renderingResponse); | ||||
|  |  | |||
|  | @ -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"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  | @ -19,6 +19,7 @@ package org.springframework.web.reactive.function.server; | |||
| import java.net.URI; | ||||
| import java.time.Instant; | ||||
| import java.time.ZonedDateTime; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| import java.util.function.Consumer; | ||||
| 
 | ||||
|  | @ -262,6 +263,15 @@ public interface EntityResponse<T> extends ServerResponse { | |||
| 		 */ | ||||
| 		Builder<T> hint(String key, Object value); | ||||
| 
 | ||||
| 		/** | ||||
| 		 * Manipulate serialization hint with the given consumer. | ||||
| 		 * | ||||
| 		 * @param hintsConsumer a function that consumes the hints | ||||
| 		 * @return this builder | ||||
| 		 * @since 5.1.6 | ||||
| 		 */ | ||||
| 		Builder<T> hints(Consumer<Map<String, Object>> hintsConsumer); | ||||
| 
 | ||||
| 		/** | ||||
| 		 * Build the response. | ||||
| 		 * @return the built response | ||||
|  |  | |||
|  | @ -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"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  | @ -376,6 +376,15 @@ public interface ServerResponse { | |||
| 		 */ | ||||
| 		BodyBuilder hint(String key, Object value); | ||||
| 
 | ||||
| 		/** | ||||
| 		 * Manipulate serialization hint with the given consumer. | ||||
| 		 * | ||||
| 		 * @param hintsConsumer a function that consumes the hints | ||||
| 		 * @return this builder | ||||
| 		 * @since 5.1.6 | ||||
| 		 */ | ||||
| 		BodyBuilder hints(Consumer<Map<String, Object>> hintsConsumer); | ||||
| 
 | ||||
| 		/** | ||||
| 		 * Set the body of the response to the given asynchronous {@code Publisher} and return it. | ||||
| 		 * This convenience method combines {@link #body(BodyInserter)} and | ||||
|  |  | |||
|  | @ -41,6 +41,7 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse | |||
| import org.springframework.mock.web.test.server.MockServerWebExchange; | ||||
| import org.springframework.util.LinkedMultiValueMap; | ||||
| import org.springframework.util.MultiValueMap; | ||||
| import org.springframework.web.reactive.function.BodyInserters; | ||||
| import org.springframework.web.reactive.result.view.ViewResolver; | ||||
| 
 | ||||
| import static org.junit.Assert.*; | ||||
|  | @ -302,6 +303,23 @@ public class DefaultServerResponseBuilderTests { | |||
| 				.verify(); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void copyCookies() { | ||||
| 		Mono<ServerResponse> serverResponse = ServerResponse.ok() | ||||
| 				.cookie(ResponseCookie.from("foo", "bar").build()) | ||||
| 				.syncBody("body"); | ||||
| 
 | ||||
| 		assertFalse(serverResponse.block().cookies().isEmpty()); | ||||
| 
 | ||||
| 		serverResponse = ServerResponse.ok() | ||||
| 				.cookie(ResponseCookie.from("foo", "bar").build()) | ||||
| 				.body(BodyInserters.fromObject("body")); | ||||
| 
 | ||||
| 
 | ||||
| 		assertFalse(serverResponse.block().cookies().isEmpty()); | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void build() { | ||||
| 		ResponseCookie cookie = ResponseCookie.from("name", "value").build(); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue