Skip to content

Commit eaafcee

Browse files
committed
Proper use of setComplete in ContextPathCompositeHandler
Issue: SPR-17144
1 parent a45ef35 commit eaafcee

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/ContextPathCompositeHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response)
6060
})
6161
.orElseGet(() -> {
6262
response.setStatusCode(HttpStatus.NOT_FOUND);
63-
response.setComplete();
64-
return Mono.empty();
63+
return response.setComplete();
6564
});
6665
}
6766

spring-web/src/test/java/org/springframework/http/server/reactive/ContextPathCompositeHandlerTests.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,12 @@
1616

1717
package org.springframework.http.server.reactive;
1818

19+
import java.time.Duration;
1920
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.HashMap;
2223
import java.util.Map;
24+
import java.util.concurrent.atomic.AtomicBoolean;
2325

2426
import org.junit.Test;
2527
import reactor.core.publisher.Mono;
@@ -109,7 +111,7 @@ public void matchWithNativeContextPath() {
109111
}
110112

111113
@Test
112-
public void notFound() throws Exception {
114+
public void notFound() {
113115
TestHttpHandler handler1 = new TestHttpHandler();
114116
TestHttpHandler handler2 = new TestHttpHandler();
115117

@@ -123,11 +125,33 @@ public void notFound() throws Exception {
123125
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
124126
}
125127

128+
@Test // SPR-17144
129+
public void notFoundWithCommitAction() {
130+
131+
AtomicBoolean commitInvoked = new AtomicBoolean(false);
132+
133+
ServerHttpRequest request = MockServerHttpRequest.get("/unknown/path").build();
134+
ServerHttpResponse response = new MockServerHttpResponse();
135+
response.beforeCommit(() -> {
136+
commitInvoked.set(true);
137+
return Mono.empty();
138+
});
139+
140+
Map<String, HttpHandler> map = new HashMap<>();
141+
TestHttpHandler handler = new TestHttpHandler();
142+
map.put("/path", handler);
143+
new ContextPathCompositeHandler(map).handle(request, response).block(Duration.ofSeconds(5));
144+
145+
assertNotInvoked(handler);
146+
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
147+
assertTrue(commitInvoked.get());
148+
}
149+
126150

127151
private ServerHttpResponse testHandle(String pathToHandle, Map<String, HttpHandler> handlerMap) {
128152
ServerHttpRequest request = MockServerHttpRequest.get(pathToHandle).build();
129153
ServerHttpResponse response = new MockServerHttpResponse();
130-
new ContextPathCompositeHandler(handlerMap).handle(request, response);
154+
new ContextPathCompositeHandler(handlerMap).handle(request, response).block(Duration.ofSeconds(5));
131155
return response;
132156
}
133157

0 commit comments

Comments
 (0)