Extra test for #10085

This commit is contained in:
gregw 2023-07-12 14:38:44 +02:00
parent 0ee0716d33
commit c435bfdbe6
5 changed files with 84 additions and 3 deletions

View File

@ -819,6 +819,7 @@ public class ServletContextHandler extends ContextHandler
String entry = path + item.getFileName();
if (item.isDirectory())
entry = entry + '/';
set.add(entry);
}
return set;

View File

@ -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")));
}
}

View File

@ -566,6 +566,46 @@ public class WebAppContextTest
assertThat(results, containsInAnyOrder(expected));
}
public static Stream<Arguments> 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

View File

@ -6,6 +6,11 @@
metadata-complete="false"
version="6.0">
<servlet>
<servlet-name>GetResourceServlet</servlet-name>
<servlet-class>org.acme.webapp.GetResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>GetResourcePathsServlet</servlet-name>
<servlet-class>org.acme.webapp.GetResourcePathsServlet</servlet-class>
@ -18,9 +23,13 @@
</servlet>
<servlet-mapping>
<servlet-name>GetResourcePathsServlet</servlet-name>
<servlet-name>GetResourceServlet</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetResourcePathsServlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GetRealPathsServlet</servlet-name>
<url-pattern>/real/*</url-pattern>

View File

@ -0,0 +1 @@
2 to too two 1+1