From d0b22997ba8f5577d912402927bd99b7c7c329ba Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 19 May 2020 20:04:30 +0200 Subject: [PATCH] Include attributes in request built by DefaultServerRequestBuilder Closes gh-25106 --- .../server/DefaultServerRequestBuilder.java | 26 ++++++++++++------- .../DefaultServerRequestBuilderTests.java | 12 +++++++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java index a33ce8b7eb82..abd9266cd83b 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -63,6 +63,7 @@ * Default {@link ServerRequest.Builder} implementation. * * @author Arjen Poutsma + * @author Sam Brannen * @since 5.1 */ class DefaultServerRequestBuilder implements ServerRequest.Builder { @@ -84,7 +85,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { private Flux body = Flux.empty(); - public DefaultServerRequestBuilder(ServerRequest other) { + DefaultServerRequestBuilder(ServerRequest other) { Assert.notNull(other, "ServerRequest must not be null"); this.messageReaders = other.messageReaders(); this.exchange = other.exchange(); @@ -180,7 +181,7 @@ public ServerRequest build() { ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(), this.methodName, this.uri, this.headers, this.cookies, this.body); ServerWebExchange exchange = new DelegatingServerWebExchange( - serverHttpRequest, this.exchange, this.messageReaders); + serverHttpRequest, this.attributes, this.exchange, this.messageReaders); return new DefaultServerRequest(exchange, this.messageReaders); } @@ -301,16 +302,19 @@ private static class DelegatingServerWebExchange implements ServerWebExchange { private final ServerHttpRequest request; + private final Map attributes; + private final ServerWebExchange delegate; private final Mono> formDataMono; private final Mono> multipartDataMono; - public DelegatingServerWebExchange( - ServerHttpRequest request, ServerWebExchange delegate, List> messageReaders) { + DelegatingServerWebExchange(ServerHttpRequest request, Map attributes, + ServerWebExchange delegate, List> messageReaders) { this.request = request; + this.attributes = attributes; this.delegate = delegate; this.formDataMono = initFormData(request, messageReaders); this.multipartDataMono = initMultipartData(request, messageReaders); @@ -359,11 +363,17 @@ private static Mono> initMultipartData(ServerHttpReq } return EMPTY_MULTIPART_DATA; } + @Override public ServerHttpRequest getRequest() { return this.request; } + @Override + public Map getAttributes() { + return this.attributes; + } + @Override public Mono> getFormData() { return this.formDataMono; @@ -381,11 +391,6 @@ public ServerHttpResponse getResponse() { return this.delegate.getResponse(); } - @Override - public Map getAttributes() { - return this.delegate.getAttributes(); - } - @Override public Mono getSession() { return this.delegate.getSession(); @@ -442,4 +447,5 @@ public String getLogPrefix() { return this.delegate.getLogPrefix(); } } + } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilderTests.java index 8e4f483d5986..985ec48c20f2 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -31,9 +31,13 @@ import org.springframework.web.testfixture.server.MockServerWebExchange; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; /** + * Unit tests for {@link DefaultServerRequestBuilder}. + * * @author Arjen Poutsma + * @author Sam Brannen */ public class DefaultServerRequestBuilderTests { @@ -58,14 +62,18 @@ public void from() { .method(HttpMethod.HEAD) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())) + .attribute("attr1", "value1") + .attributes(attributes -> attributes.put("attr2", "value2")) .body(body) .build(); assertThat(result.method()).isEqualTo(HttpMethod.HEAD); assertThat(result.headers().asHttpHeaders().size()).isEqualTo(1); assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar"); - assertThat(result.cookies().size()).isEqualTo(1); + assertThat(result.cookies()).hasSize(1); assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux"); + assertThat(result.attributes()).hasSize(3); // 3rd one is the LOG_ID + assertThat(result.attributes()).contains(entry("attr1", "value1"), entry("attr2", "value2")); StepVerifier.create(result.bodyToFlux(String.class)) .expectNext("baz")