Proper use of setComplete in ContextPathCompositeHandler
Issue: SPR-17144
This commit is contained in:
@@ -60,8 +60,7 @@ public class ContextPathCompositeHandler implements HttpHandler {
|
|||||||
})
|
})
|
||||||
.orElseGet(() -> {
|
.orElseGet(() -> {
|
||||||
response.setStatusCode(HttpStatus.NOT_FOUND);
|
response.setStatusCode(HttpStatus.NOT_FOUND);
|
||||||
response.setComplete();
|
return response.setComplete();
|
||||||
return Mono.empty();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,10 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.http.server.reactive;
|
package org.springframework.http.server.reactive;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@@ -109,7 +111,7 @@ public class ContextPathCompositeHandlerTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void notFound() throws Exception {
|
public void notFound() {
|
||||||
TestHttpHandler handler1 = new TestHttpHandler();
|
TestHttpHandler handler1 = new TestHttpHandler();
|
||||||
TestHttpHandler handler2 = new TestHttpHandler();
|
TestHttpHandler handler2 = new TestHttpHandler();
|
||||||
|
|
||||||
@@ -123,11 +125,33 @@ public class ContextPathCompositeHandlerTests {
|
|||||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
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<String, HttpHandler> 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<String, HttpHandler> handlerMap) {
|
private ServerHttpResponse testHandle(String pathToHandle, Map<String, HttpHandler> handlerMap) {
|
||||||
ServerHttpRequest request = MockServerHttpRequest.get(pathToHandle).build();
|
ServerHttpRequest request = MockServerHttpRequest.get(pathToHandle).build();
|
||||||
ServerHttpResponse response = new MockServerHttpResponse();
|
ServerHttpResponse response = new MockServerHttpResponse();
|
||||||
new ContextPathCompositeHandler(handlerMap).handle(request, response);
|
new ContextPathCompositeHandler(handlerMap).handle(request, response).block(Duration.ofSeconds(5));
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user