diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ContextPathCompositeHandler.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ContextPathCompositeHandler.java index a5ff651b567..66a099a9b8f 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ContextPathCompositeHandler.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ContextPathCompositeHandler.java @@ -60,8 +60,7 @@ public class ContextPathCompositeHandler implements HttpHandler { }) .orElseGet(() -> { response.setStatusCode(HttpStatus.NOT_FOUND); - response.setComplete(); - return Mono.empty(); + return response.setComplete(); }); } diff --git a/spring-web/src/test/java/org/springframework/http/server/reactive/ContextPathCompositeHandlerTests.java b/spring-web/src/test/java/org/springframework/http/server/reactive/ContextPathCompositeHandlerTests.java index 26055d5dbdd..d7988f25451 100644 --- a/spring-web/src/test/java/org/springframework/http/server/reactive/ContextPathCompositeHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/reactive/ContextPathCompositeHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -16,10 +16,12 @@ package org.springframework.http.server.reactive; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.Test; import reactor.core.publisher.Mono; @@ -109,7 +111,7 @@ public class ContextPathCompositeHandlerTests { } @Test - public void notFound() throws Exception { + public void notFound() { TestHttpHandler handler1 = new TestHttpHandler(); TestHttpHandler handler2 = new TestHttpHandler(); @@ -123,11 +125,33 @@ public class ContextPathCompositeHandlerTests { assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } + @Test // SPR-17144 + public void notFoundWithCommitAction() { + + AtomicBoolean commitInvoked = new AtomicBoolean(false); + + ServerHttpRequest request = MockServerHttpRequest.get("/unknown/path").build(); + ServerHttpResponse response = new MockServerHttpResponse(); + response.beforeCommit(() -> { + commitInvoked.set(true); + return Mono.empty(); + }); + + Map map = new HashMap<>(); + TestHttpHandler handler = new TestHttpHandler(); + map.put("/path", handler); + new ContextPathCompositeHandler(map).handle(request, response).block(Duration.ofSeconds(5)); + + assertNotInvoked(handler); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertTrue(commitInvoked.get()); + } + private ServerHttpResponse testHandle(String pathToHandle, Map handlerMap) { ServerHttpRequest request = MockServerHttpRequest.get(pathToHandle).build(); ServerHttpResponse response = new MockServerHttpResponse(); - new ContextPathCompositeHandler(handlerMap).handle(request, response); + new ContextPathCompositeHandler(handlerMap).handle(request, response).block(Duration.ofSeconds(5)); return response; }