Browse Source

Proper use of setComplete in ContextPathCompositeHandler

Issue: SPR-17144
pull/1946/head
Rossen Stoyanchev 7 years ago
parent
commit
eaafcee077
  1. 3
      spring-web/src/main/java/org/springframework/http/server/reactive/ContextPathCompositeHandler.java
  2. 30
      spring-web/src/test/java/org/springframework/http/server/reactive/ContextPathCompositeHandlerTests.java

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

@ -60,8 +60,7 @@ public class ContextPathCompositeHandler implements HttpHandler { @@ -60,8 +60,7 @@ public class ContextPathCompositeHandler implements HttpHandler {
})
.orElseGet(() -> {
response.setStatusCode(HttpStatus.NOT_FOUND);
response.setComplete();
return Mono.empty();
return response.setComplete();
});
}

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

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -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 { @@ -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<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) {
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;
}

Loading…
Cancel
Save