Skip to content

Commit 97d020c

Browse files
committed
Merge branch '5.1.x'
2 parents 1f5f222 + 2aec175 commit 97d020c

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,15 @@ private <T extends Publisher<?>> T handleBody(ClientResponse response,
463463
for (StatusHandler handler : this.statusHandlers) {
464464
if (handler.test(response.statusCode())) {
465465
HttpRequest request = this.requestSupplier.get();
466-
Mono<? extends Throwable> exMono = handler.apply(response, request);
467-
exMono = exMono.flatMap(ex -> drainBody(response, ex));
468-
exMono = exMono.onErrorResume(ex -> drainBody(response, ex));
466+
Mono<? extends Throwable> exMono;
467+
try {
468+
exMono = handler.apply(response, request);
469+
exMono = exMono.flatMap(ex -> drainBody(response, ex));
470+
exMono = exMono.onErrorResume(ex -> drainBody(response, ex));
471+
}
472+
catch (Throwable ex2) {
473+
exMono = drainBody(response, ex2);
474+
}
469475
T result = errorFunction.apply(exMono);
470476
return insertCheckpoint(result, response.statusCode(), request);
471477
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ public void onStatusWithMonoErrorAndBodyConsumed() {
147147
testOnStatus(ex, response -> response.bodyToMono(Void.class).then(Mono.error(ex)));
148148
}
149149

150+
@Test // gh-23230
151+
public void onStatusWithImmediateErrorAndBodyNotConsumed() {
152+
RuntimeException ex = new RuntimeException("response error");
153+
testOnStatus(ex, response -> {
154+
throw ex;
155+
});
156+
}
157+
158+
150159
private void testOnStatus(Throwable expected,
151160
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
152161

src/docs/asciidoc/testing.adoc

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5079,20 +5079,43 @@ resulting links by using XPath expressions:
50795079
.andExpect(xpath("/person/ns:link[@rel='self']/@href", ns).string("http://localhost:8080/people"));
50805080
----
50815081

5082-
[[spring-mvc-test-server-filters]]
5083-
===== Filter Registrations
50845082

5085-
When setting up a `MockMvc` instance, you can register one or more Servlet `Filter`
5086-
instances, as the following example shows:
5083+
[[spring-mvc-test-async-requests]]
5084+
===== Async Requests
5085+
5086+
Servlet 3.0 asynchronous requests,
5087+
<<web.adoc#mvc-ann-async,supported in Spring MVC>>, work by exiting the Servlet container
5088+
thread and allowing the application to compute the response asynchronously, after which
5089+
an async dispatch is made to complete processing on a Servlet container thread.
5090+
5091+
In Spring MVC Test, async requests can be tested by asserting the produced async value
5092+
first, then manually performing the async dispatch, and finally verifying the response.
5093+
Below is an example test for controller methods that return `DeferredResult`, `Callable`,
5094+
or reactive type such as Reactor `Mono`:
50875095

50885096
[source,java,indent=0]
50895097
[subs="verbatim,quotes"]
50905098
----
5091-
mockMvc = standaloneSetup(new PersonController()).addFilters(new CharacterEncodingFilter()).build();
5099+
@Test
5100+
public void test() throws Exception {
5101+
MvcResult mvcResult = this.mockMvc.perform(get("/path"))
5102+
.andExpect(status().isOk()) <1>
5103+
.andExpect(request().asyncStarted()) <2>
5104+
.andExpect(request().asyncResult("body")) <3>
5105+
.andReturn();
5106+
5107+
this.mockMvc.perform(asyncDispatch(mvcResult)) <4>
5108+
.andExpect(status().isOk()) <5>
5109+
.andExpect(content().string("body"));
5110+
}
50925111
----
5112+
<1> Check response status is still unchanged
5113+
<2> Async processing must have started
5114+
<3> Wait and assert the async result
5115+
<4> Manually perform an ASYNC dispatch (as there is no running container)
5116+
<5> Verify the final response
50935117

5094-
Registered filters are invoked through the `MockFilterChain` from `spring-test`, and the
5095-
last filter delegates to the `DispatcherServlet`.
5118+
====
50965119
50975120
50985121
[[spring-mvc-test-vs-streaming-response]]
@@ -5108,13 +5131,31 @@ with `WebTestClient`. One extra advantage is the ability to use the `StepVerifie
51085131
project Reactor that allows declaring expectations on a stream of data.
51095132
51105133
5134+
[[spring-mvc-test-server-filters]]
5135+
===== Filter Registrations
5136+
5137+
When setting up a `MockMvc` instance, you can register one or more Servlet `Filter`
5138+
instances, as the following example shows:
5139+
5140+
====
5141+
[source,java,indent=0]
5142+
[subs="verbatim,quotes"]
5143+
----
5144+
mockMvc = standaloneSetup(new PersonController()).addFilters(new CharacterEncodingFilter()).build();
5145+
----
5146+
====
5147+
5148+
Registered filters are invoked through the `MockFilterChain` from `spring-test`, and the
5149+
last filter delegates to the `DispatcherServlet`.
5150+
5151+
51115152
[[spring-mvc-test-vs-end-to-end-integration-tests]]
5112-
===== Differences Between Out-of-Container and End-to-End Integration Tests
5153+
===== Spring MVC Test vs End-to-End Tests
51135154
5114-
As mentioned earlier Spring MVC Test is built on the Servlet API mock objects from the
5115-
`spring-test` module and does not use a running Servlet container. Therefore, there are
5116-
some important differences compared to full end-to-end integration tests with an actual
5117-
client and server running.
5155+
Spring MVC Test is built on Servlet API mock implementations from the
5156+
`spring-test` module and does not rely on a running container. Therefore, there are
5157+
some differences when compared to full end-to-end integration tests with an actual
5158+
client and a live server running.
51185159
51195160
The easiest way to think about this is by starting with a blank `MockHttpServletRequest`.
51205161
Whatever you add to it is what the request becomes. Things that may catch you by surprise
@@ -5155,7 +5196,7 @@ important thing to check. In short, there is room here for multiple styles and s
51555196
of testing even within the same project.
51565197
51575198
[[spring-mvc-test-server-resources]]
5158-
===== Further Server-Side Test Examples
5199+
===== Further Examples
51595200
51605201
The framework's own tests include
51615202
https://github.com/spring-projects/spring-framework/tree/master/spring-test/src/test/java/org/springframework/test/web/servlet/samples[many

0 commit comments

Comments
 (0)