Polish + minor HttpHandler refactoring
CompositeHttpHandler is public and called ContextPathCompositeHandler. Also an overhaul of the Javadoc on HttpHandler, WebHttpHandlerAdapter, and ContextPathCompositeHandler.
This commit is contained in:
@@ -34,26 +34,27 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link HttpHandler}.
|
||||
* Unit tests for {@link ContextPathCompositeHandler}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class HttpHandlerTests {
|
||||
public class ContextPathCompositeHandlerTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void invalidContextPath() {
|
||||
testInvalidContextPath(" ", "Context path must not be empty");
|
||||
testInvalidContextPath("path", "Context path must begin with '/'");
|
||||
testInvalidContextPath("/path/", "Context path must not end with '/'");
|
||||
testInvalid(" ", "Context path must not be empty");
|
||||
testInvalid("path", "Context path must begin with '/'");
|
||||
testInvalid("/path/", "Context path must not end with '/'");
|
||||
}
|
||||
|
||||
private void testInvalidContextPath(String contextPath, String errorMessage) {
|
||||
private void testInvalid(String contextPath, String expectedError) {
|
||||
try {
|
||||
new TestHttpHandlerAdapter(Collections.singletonMap(contextPath, new TestHttpHandler()));
|
||||
new ContextPathCompositeHandler(Collections.singletonMap(contextPath, new TestHttpHandler()));
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
assertEquals(errorMessage, ex.getMessage());
|
||||
assertEquals(expectedError, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +69,7 @@ public class HttpHandlerTests {
|
||||
map.put("/another/path", handler2);
|
||||
map.put("/yet/another/path", handler3);
|
||||
|
||||
testPath("/another/path/and/more", map);
|
||||
testHandle("/another/path/and/more", map);
|
||||
|
||||
assertInvoked(handler2, "/another/path");
|
||||
assertNotInvoked(handler1, handler3);
|
||||
@@ -85,7 +86,7 @@ public class HttpHandlerTests {
|
||||
map.put("/another/path", handler2);
|
||||
map.put("/yet/another/path", handler3);
|
||||
|
||||
testPath("/path", map);
|
||||
testHandle("/path", map);
|
||||
|
||||
assertInvoked(handler1, "/path");
|
||||
assertNotInvoked(handler2, handler3);
|
||||
@@ -101,7 +102,7 @@ public class HttpHandlerTests {
|
||||
TestHttpHandler handler = new TestHttpHandler();
|
||||
Map<String, HttpHandler> map = Collections.singletonMap("/another/path", handler);
|
||||
|
||||
new TestHttpHandlerAdapter(map).handle(request);
|
||||
new ContextPathCompositeHandler(map).handle(request, new MockServerHttpResponse());
|
||||
|
||||
assertTrue(handler.wasInvoked());
|
||||
assertEquals("/yet/another/path", handler.getRequest().getContextPath());
|
||||
@@ -116,16 +117,18 @@ public class HttpHandlerTests {
|
||||
map.put("/path", handler1);
|
||||
map.put("/another/path", handler2);
|
||||
|
||||
ServerHttpResponse response = testPath("/yet/another/path", map);
|
||||
ServerHttpResponse response = testHandle("/yet/another/path", map);
|
||||
|
||||
assertNotInvoked(handler1, handler2);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
private ServerHttpResponse testPath(String path, Map<String, HttpHandler> handlerMap) {
|
||||
TestHttpHandlerAdapter adapter = new TestHttpHandlerAdapter(handlerMap);
|
||||
return adapter.handle(path);
|
||||
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);
|
||||
return response;
|
||||
}
|
||||
|
||||
private void assertInvoked(TestHttpHandler handler, String contextPath) {
|
||||
@@ -138,30 +141,6 @@ public class HttpHandlerTests {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
private static class TestHttpHandlerAdapter {
|
||||
|
||||
|
||||
private final HttpHandler httpHandler;
|
||||
|
||||
|
||||
public TestHttpHandlerAdapter(Map<String, HttpHandler> handlerMap) {
|
||||
this.httpHandler = HttpHandler.of(handlerMap);
|
||||
}
|
||||
|
||||
public ServerHttpResponse handle(String path) {
|
||||
ServerHttpRequest request = MockServerHttpRequest.get(path).build();
|
||||
return handle(request);
|
||||
}
|
||||
|
||||
public ServerHttpResponse handle(ServerHttpRequest request) {
|
||||
ServerHttpResponse response = new MockServerHttpResponse();
|
||||
this.httpHandler.handle(request, response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
private static class TestHttpHandler implements HttpHandler {
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.server.reactive.bootstrap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -79,6 +80,11 @@ public abstract class AbstractHttpServer implements HttpServer {
|
||||
return this.handlerMap;
|
||||
}
|
||||
|
||||
protected HttpHandler resolveHttpHandler() {
|
||||
return getHttpHandlerMap() != null ?
|
||||
new ContextPathCompositeHandler(getHttpHandlerMap()) : getHttpHandler();
|
||||
}
|
||||
|
||||
|
||||
// InitializingBean
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
|
||||
import org.springframework.http.server.reactive.JettyHttpHandlerAdapter;
|
||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||
|
||||
@@ -53,8 +53,7 @@ public class JettyHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
private ServletHttpHandlerAdapter createServletAdapter() {
|
||||
return new JettyHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
return new JettyHttpHandlerAdapter(resolveHttpHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import reactor.core.Loopback;
|
||||
import reactor.ipc.netty.NettyContext;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
|
||||
|
||||
/**
|
||||
@@ -43,8 +42,7 @@ public class ReactorHttpServer extends AbstractHttpServer implements Loopback {
|
||||
}
|
||||
|
||||
private ReactorHttpHandlerAdapter createHttpHandlerAdapter() {
|
||||
return new ReactorHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
return new ReactorHttpHandlerAdapter(resolveHttpHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.http.server.reactive.bootstrap;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter;
|
||||
|
||||
/**
|
||||
@@ -38,8 +37,7 @@ public class RxNettyHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
private RxNettyHttpHandlerAdapter createHttpHandlerAdapter() {
|
||||
return new RxNettyHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
return new RxNettyHttpHandlerAdapter(resolveHttpHandler());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.io.File;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
|
||||
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
|
||||
import org.springframework.http.server.reactive.TomcatHttpHandlerAdapter;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -68,8 +68,7 @@ public class TomcatHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
private ServletHttpHandlerAdapter initServletAdapter() {
|
||||
return new TomcatHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
return new TomcatHttpHandlerAdapter(resolveHttpHandler());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.net.InetSocketAddress;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
|
||||
|
||||
/**
|
||||
@@ -39,8 +38,7 @@ public class UndertowHttpServer extends AbstractHttpServer {
|
||||
}
|
||||
|
||||
private UndertowHttpHandlerAdapter initHttpHandlerAdapter() {
|
||||
return new UndertowHttpHandlerAdapter(getHttpHandlerMap() != null
|
||||
? HttpHandler.of(getHttpHandlerMap()) : getHttpHandler());
|
||||
return new UndertowHttpHandlerAdapter(resolveHttpHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user