From eacf1d35ee7aacba60f6a736b41731c61cd4a1d1 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 8 Aug 2018 16:35:47 +0300 Subject: [PATCH] Proper use of setComplete in ContextPathCompositeHandler Issue: SPR-17144 --- .../reactive/ContextPathCompositeHandler.java | 3 +- .../ContextPathCompositeHandlerTests.java | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) 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 6014ae0dc2..cab9f473d2 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 @@ -75,8 +75,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 26055d5dbd..d7988f2545 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; }