Add extra DefaultServlet include tests (#10154)

#10142 Added extra DefaultServlet include tests

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
Co-authored-by: gregw <gregw@webtide.com>
This commit is contained in:
Ludovic Orban 2023-07-26 23:36:31 +02:00 committed by GitHub
parent 7f8cb33518
commit 4a8b517e58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 22 deletions

View File

@ -1223,7 +1223,7 @@ public class DefaultServlet extends HttpServlet
return request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null;
}
private boolean isDefaultMapping(HttpServletRequest req)
protected boolean isDefaultMapping(HttpServletRequest req)
{
if (req.getHttpServletMapping().getMappingMatch() == MappingMatch.DEFAULT)
return true;

View File

@ -16,7 +16,7 @@ package org.eclipse.jetty.ee10.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
@ -92,10 +92,10 @@ public class DispatcherTest
ContextHandlerCollection contextCollection = new ContextHandlerCollection();
_contextHandler = new ServletContextHandler();
_contextHandler.setContextPath("/context");
_contextHandler.setBaseResourceAsPath(MavenTestingUtils.getTestResourcePathDir("contextResources"));
contextCollection.addHandler(_contextHandler);
ResourceHandler resourceHandler = new ResourceHandler();
Path basePath = MavenTestingUtils.getTestResourcePathDir("dispatchResourceTest");
resourceHandler.setBaseResource(ResourceFactory.root().newResource(basePath));
resourceHandler.setBaseResource(ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePathDir("dispatchResourceTest")));
ContextHandler resourceContextHandler = new ContextHandler("/resource");
resourceContextHandler.setHandler(resourceHandler);
contextCollection.addHandler(resourceContextHandler);
@ -388,6 +388,87 @@ public class DispatcherTest
assertEquals(expected, responses);
}
@Test
public void testIncludeStatic() throws Exception
{
_contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*");
_contextHandler.addServlet(new ServletHolder("default", DefaultServlet.class), "/");
_server.start();
String responses = _connector.getResponse("""
GET /context/IncludeServlet?do=static HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 26\r
Connection: close\r
\r
Include:
Test 2 to too two""";
assertEquals(expected, responses);
}
@Test
@Disabled("Bug #10155 - response misses the Content-Length header")
public void testIncludeStaticWithWriter() throws Exception
{
_contextHandler.addServlet(new ServletHolder(new IncludeServlet(true)), "/IncludeServlet/*");
_contextHandler.addServlet(new ServletHolder("default", DefaultServlet.class), "/");
_server.start();
String responses = _connector.getResponse("""
GET /context/IncludeServlet?do=static HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 26\r
Connection: close\r
\r
Include:
Test 2 to too two""";
assertEquals(expected, responses);
}
@Test
public void testForwardStatic() throws Exception
{
_contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*");
_contextHandler.addServlet(DefaultServlet.class, "/");
_server.start();
String responses = _connector.getResponse("""
GET /context/ForwardServlet?do=req.echo&uri=/test.txt HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
responses = responses.replaceFirst("Last-Modified: .*\r\n", "Last-Modified: xxx\r\n");
String expected = """
HTTP/1.1 200 OK\r
Last-Modified: xxx\r
Content-Type: text/plain\r
Accept-Ranges: bytes\r
Content-Length: 17\r
Connection: close\r
\r
Test 2 to too two""";
assertEquals(expected, responses);
}
@Test
public void testForwardSendError() throws Exception
{
@ -777,10 +858,6 @@ public class DispatcherTest
public static class WrappingFilter implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
@ -789,11 +866,6 @@ public class DispatcherTest
chain.doFilter(request, wrapper);
wrapper.sendResponse(response.getOutputStream());
}
@Override
public void destroy()
{
}
}
public static class ResponseWrapper extends HttpServletResponseWrapper
@ -960,12 +1032,6 @@ public class DispatcherTest
chain.doFilter(request, response);
}
}
@Override
public void destroy()
{
}
}
public static class DispatchServletServlet extends HttpServlet implements Servlet
@ -993,11 +1059,24 @@ public class DispatcherTest
public static class IncludeServlet extends HttpServlet implements Servlet
{
// The logic linked to this field be deleted and the writer always used once #10155 is fixed.
private final boolean useWriter;
public IncludeServlet()
{
this(false);
}
public IncludeServlet(boolean useWriter)
{
this.useWriter = useWriter;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
RequestDispatcher dispatcher = null;
Boolean headers = Boolean.valueOf(request.getParameter("headers"));
boolean headers = Boolean.parseBoolean(request.getParameter("headers"));
if (request.getParameter("do").equals("forward"))
dispatcher = getServletContext().getRequestDispatcher("/ForwardServlet/forwardpath?do=assertincludeforward");
@ -1005,6 +1084,14 @@ public class DispatcherTest
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardIncludeServlet/assertpath?do=end");
else if (request.getParameter("do").equals("assertinclude"))
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the&headers=" + headers);
else if (request.getParameter("do").equals("static"))
{
if (useWriter)
response.getWriter().println("Include:");
else
response.getOutputStream().write("Include:\n".getBytes(StandardCharsets.US_ASCII));
dispatcher = getServletContext().getRequestDispatcher("/test.txt");
}
assert dispatcher != null;
dispatcher.include(request, response);
}
@ -1334,7 +1421,7 @@ public class DispatcherTest
assertEquals("/IncludeServlet", request.getServletPath());
response.setContentType("text/html");
if (Boolean.valueOf(request.getParameter("headers")))
if (Boolean.parseBoolean(request.getParameter("headers")))
{
response.setHeader("org.eclipse.jetty.server.include.specialSetHeader", "specialSetHeader");
response.setHeader("org.eclipse.jetty.server.include.specialAddHeader", "specialAddHeader");

View File

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

View File

@ -115,8 +115,9 @@ public class DispatcherTest
_contextHandler = new ServletContextHandler();
_contextHandler.setContextPath("/context");
_contextCollection.addHandler(_contextHandler);
_contextHandler.setBaseResourceAsPath(MavenPaths.findTestResourceDir("contextResources"));
_resourceHandler = new ResourceHandler();
_resourceHandler.setResourceBase(MavenPaths.findTestResourceDir("dispatchResourceTest").toUri().toASCIIString());
_resourceHandler.setBaseResource(MavenPaths.findTestResourceDir("dispatchResourceTest"));
_resourceHandler.setPathInfoOnly(true);
ContextHandler resourceContextHandler = new ContextHandler("/resource");
resourceContextHandler.setHandler(_resourceHandler);
@ -417,6 +418,65 @@ public class DispatcherTest
assertEquals(expected, responses);
}
@Test
public void testIncludeStatic() throws Exception
{
createDefaultContextHandlerCollection();
_contextHandler.addServlet(IncludeServlet.class, "/IncludeServlet/*");
_contextHandler.addServlet(DefaultServlet.class, "/");
createServer(_contextCollection);
_server.start();
String responses = _connector.getResponse("""
GET /context/IncludeServlet?do=static HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
String expected = """
HTTP/1.1 200 OK\r
Content-Length: 26\r
Connection: close\r
\r
Include:
Test 2 to too two""";
assertEquals(expected, responses);
}
@Test
public void testForwardStatic() throws Exception
{
createDefaultContextHandlerCollection();
_contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*");
_contextHandler.addServlet(DefaultServlet.class, "/");
createServer(_contextCollection);
_server.start();
String responses = _connector.getResponse("""
GET /context/ForwardServlet?do=req.echo&uri=/test.txt HTTP/1.1\r
Host: local\r
Connection: close\r
\r
""");
responses = responses.replaceFirst("Last-Modified: .*\r\n", "Last-Modified: xxx\r\n");
String expected = """
HTTP/1.1 200 OK\r
Last-Modified: xxx\r
Content-Type: text/plain\r
Accept-Ranges: bytes\r
Content-Length: 17\r
Connection: close\r
\r
Test 2 to too two""";
assertEquals(expected, responses);
}
@Test
public void testForwardSendError() throws Exception
{
@ -949,6 +1009,12 @@ public class DispatcherTest
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardIncludeServlet/assertpath?do=end");
else if (request.getParameter("do").equals("assertinclude"))
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeServlet?do=end&do=the");
else if (request.getParameter("do").equals("static"))
{
response.getWriter().println("Include:");
dispatcher = getServletContext().getRequestDispatcher("/test.txt");
}
assert dispatcher != null;
dispatcher.include(request, response);
}
}

View File

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