ARTEMIS-4176 Fix console custom root redirect

This commit is contained in:
Domenico Francesco Bruscino 2023-02-17 09:11:02 +01:00 committed by Robbie Gemmell
parent 7e1f6c1abe
commit 8bdffe872c
2 changed files with 42 additions and 1 deletions

View File

@ -40,7 +40,7 @@ public class DefaultHandler extends org.eclipse.jetty.server.handler.DefaultHand
HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
if (rootRedirectLocation != null && target.matches("^$|/")) {
response.sendRedirect("/console");
response.sendRedirect(rootRedirectLocation);
} else {
super.handle(target, baseRequest, request, response);
}

View File

@ -23,8 +23,10 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
@ -454,6 +456,45 @@ public class WebServerComponentTest extends Assert {
Assert.assertFalse(webServerComponent.isStarted());
}
@Test
public void testDefaultRootRedirect() throws Exception {
testRootRedirect(null, 404, null);
}
@Test
public void testCustomRootRedirect() throws Exception {
testRootRedirect("test-root-redirect", 302, "test-root-redirect");
}
public void testRootRedirect(String rootRedirectLocation, int expectedResponseCode, String expectedResponseLocation) throws Exception {
BindingDTO bindingDTO = new BindingDTO();
bindingDTO.uri = "http://localhost:0";
WebServerDTO webServerDTO = new WebServerDTO();
webServerDTO.setBindings(Collections.singletonList(bindingDTO));
webServerDTO.path = "";
webServerDTO.rootRedirectLocation = rootRedirectLocation;
WebServerComponent webServerComponent = new WebServerComponent();
webServerComponent.configure(webServerDTO, null, null);
webServerComponent.start();
try {
int port = webServerComponent.getPort(0);
java.net.URL url = new URL("http://localhost:" + port);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
try {
assertEquals(expectedResponseCode, conn.getResponseCode());
if (expectedResponseLocation != null) {
assertTrue(conn.getHeaderField("Location").endsWith(webServerDTO.rootRedirectLocation));
}
} finally {
conn.disconnect();
}
} finally {
webServerComponent.stop(true);
}
}
private void createTestWar(String warName) throws Exception {
File warFile = new File("target", warName);
File srcFile = new File("src/test/webapp");