From c435bfdbe65110546399df0b05b0c0f3ccbd8f97 Mon Sep 17 00:00:00 2001 From: gregw Date: Wed, 12 Jul 2023 14:38:44 +0200 Subject: [PATCH] Extra test for #10085 --- .../ee10/servlet/ServletContextHandler.java | 1 + .../org/acme/webapp/GetResourceServlet.java | 29 ++++++++++++ .../jetty/ee10/webapp/WebAppContextTest.java | 45 ++++++++++++++++++- .../webapp-with-resources/WEB-INF/web.xml | 11 ++++- .../src/test/webapp-with-resources/test.txt | 1 + 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 jetty-ee10/jetty-ee10-webapp/src/test/java/org/acme/webapp/GetResourceServlet.java create mode 100644 jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/test.txt diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextHandler.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextHandler.java index 45f4637c2f3..d1b43108293 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextHandler.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextHandler.java @@ -819,6 +819,7 @@ public class ServletContextHandler extends ContextHandler String entry = path + item.getFileName(); if (item.isDirectory()) entry = entry + '/'; + set.add(entry); } return set; diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/acme/webapp/GetResourceServlet.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/acme/webapp/GetResourceServlet.java new file mode 100644 index 00000000000..308bbf35d41 --- /dev/null +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/acme/webapp/GetResourceServlet.java @@ -0,0 +1,29 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.acme.webapp; + +import java.io.IOException; + +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +public class GetResourceServlet extends HttpServlet +{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException + { + resp.getWriter().printf("url=%s\n", req.getServletContext().getResource(req.getParameter("r"))); + } +} diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java index ad0b0f4937a..b9268a021c0 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java @@ -566,6 +566,46 @@ public class WebAppContextTest assertThat(results, containsInAnyOrder(expected)); } + public static Stream resourceTests() + { + return Stream.of( + Arguments.of("/test.txt", "/test.txt"), + Arguments.of("/WEB-INF/web.xml", "/WEB-INF/web.xml"), + Arguments.of("/WEB-INF/", "/WEB-INF/"), + Arguments.of("/WEB-INF", "/WEB-INF/") + // TODO the following assertion fails because of a bug in the JDK (see JDK-8311079 and MountedPathResourceTest.testJarFileResourceAccessBackSlash()) + // Arguments.of("/nested-reserved-!#\\\\$%&()*+,:=?@[]-meta-inf-resource.txt", "/nested-reserved-!#\\\\$%&()*+,:=?@[]-meta-inf-resource.txt") + ); + } + + @ParameterizedTest + @MethodSource("resourceTests") + public void testGetResource(String resource, String expected) throws Exception + { + Server server = newServer(); + LocalConnector connector = new LocalConnector(server); + server.addConnector(connector); + + WebAppContext context = new WebAppContext(MavenTestingUtils.getBasePath().resolve("src/test/webapp-with-resources").toString(), "/"); + server.setHandler(context); + server.start(); + + ServletContext servletContext = context.getServletContext(); + + URL url = servletContext.getResource(resource); + assertThat(url.toString(), endsWith(expected)); + + HttpTester.Response response1 = HttpTester.parseResponse(connector.getResponse(""" + GET /resource?r=%s HTTP/1.1\r + Host: local\r + Connection: close\r + \r + """.formatted(resource))); + + assertThat(response1.getStatus(), is(HttpStatus.OK_200)); + assertThat(response1.getContent(), containsString("url=" + url)); + } + @Test public void testGetResourcePaths() throws Exception { @@ -583,8 +623,9 @@ public class WebAppContextTest String[] expected = { "/WEB-INF/", "/nested-reserved-!#\\\\$%&()*+,:=?@[]-meta-inf-resource.txt", + "/test.txt" }; - assertThat(resourcePaths.size(), is(2)); + assertThat(resourcePaths.size(), is(expected.length)); assertThat(resourcePaths, containsInAnyOrder(expected)); String realPath = servletContext.getRealPath("/"); @@ -598,7 +639,7 @@ public class WebAppContextTest //assertThat(servletContext.getResource("/nested-reserved-!#\\\\$%&()*+,:=?@[]-meta-inf-resource.txt"), notNullValue()); HttpTester.Response response1 = HttpTester.parseResponse(connector.getResponse(""" - GET /resource HTTP/1.1\r + GET /resources HTTP/1.1\r Host: local\r Connection: close\r \r diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/WEB-INF/web.xml b/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/WEB-INF/web.xml index 612b39d55f5..5df402d05d0 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/WEB-INF/web.xml +++ b/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/WEB-INF/web.xml @@ -6,6 +6,11 @@ metadata-complete="false" version="6.0"> + + GetResourceServlet + org.acme.webapp.GetResourceServlet + 1 + GetResourcePathsServlet org.acme.webapp.GetResourcePathsServlet @@ -18,9 +23,13 @@ - GetResourcePathsServlet + GetResourceServlet /resource/* + + GetResourcePathsServlet + /resources/* + GetRealPathsServlet /real/* diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/test.txt b/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/test.txt new file mode 100644 index 00000000000..1a93f199922 --- /dev/null +++ b/jetty-ee10/jetty-ee10-webapp/src/test/webapp-with-resources/test.txt @@ -0,0 +1 @@ +2 to too two 1+1 \ No newline at end of file