From 3618557b3f43d87535c54db7058054bd72158ecb Mon Sep 17 00:00:00 2001 From: Alexej Timonin Date: Wed, 28 Jul 2021 20:13:41 +0200 Subject: [PATCH 1/5] Clarify that from(..) also copies body --- .../web/reactive/function/client/ClientRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java index 9e7cc31ba6b4..8179c4d33c48 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java @@ -125,8 +125,8 @@ default Optional attribute(String name) { // Static builder methods /** - * Create a builder with the method, URI, headers, and cookies of the given request. - * @param other the request to copy the method, URI, headers, and cookies from + * Create a builder with the method, URI, headers, cookies and body of the given request. + * @param other the request to copy the method, URI, headers, cookies and body from * @return the created builder */ static Builder from(ClientRequest other) { From 0a0d6da0238a3ab874bf42f38b3abad00b445192 Mon Sep 17 00:00:00 2001 From: Alexej Timonin Date: Wed, 28 Jul 2021 21:09:57 +0200 Subject: [PATCH 2/5] Test to show that body will be copied --- .../DefaultClientRequestBuilderTests.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java index 47fdfdf430f8..8d4362d3cc3e 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java @@ -71,6 +71,35 @@ public void from() throws URISyntaxException { assertThat(result.httpRequest()).isNotNull(); } + @Test + public void fromCopiesBody() { + String body = "foo"; + BodyInserter inserter = (response, strategies) -> { + byte[] bodyBytes = body.getBytes(UTF_8); + DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes); + + return response.writeWith(Mono.just(buffer)); + }; + + ClientRequest other = ClientRequest.create(POST, URI.create("https://example.com")) + .body(inserter).build(); + + ClientRequest result = ClientRequest.from(other).build(); + + List> messageWriters = new ArrayList<>(); + messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes())); + + ExchangeStrategies strategies = mock(ExchangeStrategies.class); + given(strategies.messageWriters()).willReturn(messageWriters); + + MockClientHttpRequest request = new MockClientHttpRequest(POST, "/"); + result.writeTo(request, strategies).block(); + + String copiedBody = request.getBodyAsString().block(); + + assertThat(copiedBody).isEqualTo("foo"); + } + @Test public void method() throws URISyntaxException { URI url = new URI("https://example.com"); From 8c00d5fe50a687b8c96dffac700148e04b121131 Mon Sep 17 00:00:00 2001 From: Alexej Timonin Date: Fri, 30 Jul 2021 20:57:24 +0200 Subject: [PATCH 3/5] Include attributes in doc Co-authored-by: Sam Brannen --- .../web/reactive/function/client/ClientRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java index 8179c4d33c48..694749dc5cd1 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java @@ -125,8 +125,8 @@ default Optional attribute(String name) { // Static builder methods /** - * Create a builder with the method, URI, headers, cookies and body of the given request. - * @param other the request to copy the method, URI, headers, cookies and body from + * Create a builder with the method, URI, headers, cookies, attributes, and body of the given request. + * @param other the request to copy the method, URI, headers, cookies, attributes, and body from * @return the created builder */ static Builder from(ClientRequest other) { From bd3b3b8501f254654738d2e30113008e5e980f67 Mon Sep 17 00:00:00 2001 From: Alexej Timonin Date: Fri, 30 Jul 2021 20:59:27 +0200 Subject: [PATCH 4/5] Change copyright date --- .../web/reactive/function/client/ClientRequest.java | 2 +- .../function/client/DefaultClientRequestBuilderTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java index 694749dc5cd1..dc28e2bf7538 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java index 8d4362d3cc3e..b394b96dce18 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. From 023752f9edc6269413c4bd9ddaf919ebab551a40 Mon Sep 17 00:00:00 2001 From: Alexej Timonin Date: Fri, 30 Jul 2021 21:32:36 +0200 Subject: [PATCH 5/5] Update from() test to include attributes --- .../function/client/DefaultClientRequestBuilderTests.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java index b394b96dce18..0788264acf7f 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilderTests.java @@ -56,6 +56,8 @@ public void from() throws URISyntaxException { ClientRequest other = ClientRequest.create(GET, URI.create("https://example.com")) .header("foo", "bar") .cookie("baz", "qux") + .attribute("attributeKey", "attributeValue") + .attribute("anotherAttributeKey", "anotherAttributeValue") .httpRequest(request -> {}) .build(); ClientRequest result = ClientRequest.from(other) @@ -69,6 +71,8 @@ public void from() throws URISyntaxException { assertThat(result.cookies().size()).isEqualTo(1); assertThat(result.cookies().getFirst("baz")).isEqualTo("quux"); assertThat(result.httpRequest()).isNotNull(); + assertThat(result.attributes().get("attributeKey")).isEqualTo("attributeValue"); + assertThat(result.attributes().get("anotherAttributeKey")).isEqualTo("anotherAttributeValue"); } @Test