Skip to content

Commit d742fc1

Browse files
committed
Add consumeWith to FluxExchangeResult
Issue: SPR-15959
1 parent 1320228 commit d742fc1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.springframework.core.ParameterizedTypeReference;
3636
import org.springframework.core.io.ByteArrayResource;
37+
import org.springframework.core.io.buffer.DataBuffer;
3738
import org.springframework.http.HttpHeaders;
3839
import org.springframework.http.HttpMethod;
3940
import org.springframework.http.MediaType;
@@ -46,6 +47,7 @@
4647
import org.springframework.util.MimeType;
4748
import org.springframework.util.MultiValueMap;
4849
import org.springframework.web.reactive.function.BodyExtractor;
50+
import org.springframework.web.reactive.function.BodyExtractors;
4951
import org.springframework.web.reactive.function.BodyInserter;
5052
import org.springframework.web.reactive.function.client.ClientResponse;
5153
import org.springframework.web.reactive.function.client.WebClient;
@@ -379,6 +381,11 @@ public BodyContentSpec expectBody() {
379381
return new DefaultBodyContentSpec(this.result.decodeToByteArray());
380382
}
381383

384+
@Override
385+
public FluxExchangeResult<DataBuffer> returnResult() {
386+
return this.result.decodeToFlux(BodyExtractors.toDataBuffers());
387+
}
388+
382389
@Override
383390
public <T> FluxExchangeResult<T> returnResult(Class<T> elementType) {
384391
return this.result.decodeToFlux(toFlux(elementType));

spring-test/src/main/java/org/springframework/test/web/reactive/server/FluxExchangeResult.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.web.reactive.server;
1818

1919
import java.time.Duration;
20+
import java.util.function.Consumer;
2021

2122
import reactor.core.publisher.Flux;
2223
import reactor.core.publisher.Mono;
@@ -97,4 +98,23 @@ public byte[] getResponseBodyContent() {
9798
.block();
9899
}
99100

101+
/**
102+
* Invoke the given consumer within {@link #assertWithDiagnostics(Runnable)}
103+
* passing {@code "this"} instance to it. This method allows the following,
104+
* without leaving the {@code WebTestClient} chain of calls:
105+
* <pre class="code">
106+
* client.get()
107+
* .uri("/persons")
108+
* .accept(TEXT_EVENT_STREAM)
109+
* .exchange()
110+
* .expectStatus().isOk()
111+
* .returnResult()
112+
* .consumeWith(result -> assertThat(...);
113+
* </pre>
114+
* @param consumer consumer for {@code "this"} instance
115+
*/
116+
public void consumeWith(Consumer<FluxExchangeResult<T>> consumer) {
117+
assertWithDiagnostics(() -> consumer.accept(this));
118+
}
119+
100120
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.springframework.context.ApplicationContext;
3131
import org.springframework.core.ParameterizedTypeReference;
32+
import org.springframework.core.io.buffer.DataBuffer;
3233
import org.springframework.format.FormatterRegistry;
3334
import org.springframework.http.HttpHeaders;
3435
import org.springframework.http.HttpMethod;
@@ -663,6 +664,15 @@ interface ResponseSpec {
663664
* Variant of {@link #returnResult(Class)} for element types with generics.
664665
*/
665666
<T> FluxExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementType);
667+
668+
/**
669+
* Return the exchange result with the body decoded to
670+
* {@code Flux<DataBuffer>}. Use this option for infinite streams and
671+
* consume the stream with the {@code StepVerifier} from the Reactor Add-Ons.
672+
*
673+
* @return
674+
*/
675+
FluxExchangeResult<DataBuffer> returnResult();
666676
}
667677

668678
/**

0 commit comments

Comments
 (0)