ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/jakarta/servlet/http/TestHttpServletResponseSendError.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/jakarta/servlet/http/TestHttpServletResponseSendError.java
Status
scanned
Type
file
Name
TestHttpServletResponseSendError.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
12257 (12.0 KB)
MD5
3b4b5b5273a3b2bbe27522b87353342e
SHA1
54edef33c1adc96494d8b9426449401f8d076de4
SHA256
fafd1072d5f9be7d234d3e3cf4bfff05370b1edbc7b28940e2542361c9ba7311
SHA512

      
    
SHA1_git
8a434ec0232d11f6c8a3d13547799fcb8f93a996
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestHttpServletResponseSendError.java | 12.0 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jakarta.servlet.http; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import jakarta.servlet.AsyncContext; import jakarta.servlet.ServletException; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.apache.catalina.Context; import org.apache.catalina.Wrapper; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.descriptor.web.ErrorPage; /** * These tests evolved out of a discussion in the Jakarta Servlet project regarding the intended behaviour in various * error scenarios. Async requests and/or async error pages added additional complexity. */ @RunWith(Parameterized.class) public class TestHttpServletResponseSendError extends TomcatBaseTest { /* @formatter:off * * Implementation notes: * Original Request * - async * - error in original thread / new thread * - error before / after startAsync * - error before / after complete / dispatch * Error page * - sync * - async * - complete * - dispatch * * @formatter:on */ private enum AsyncErrorPoint { /* * Thread A is the container thread the processes the original request. Thread B is the async thread (may or may * not be a container thread) that is started by the async processing. */ THREAD_A_BEFORE_START_ASYNC, THREAD_A_AFTER_START_ASYNC, THREAD_A_AFTER_START_RUNNABLE, THREAD_B_BEFORE_COMPLETE /* * If the error is triggered after Thread B completes async processing there is essentially a race condition * between thread B making the change and the container checking to see if the error flag has been set. We can't * easily control the execution order here so we don't test it. */ } @Parameterized.Parameters(name = "{index}: async[{0}], throw[{1}], dispatch[{2}], errorPoint[{3}], useStart[{4}]") public static Collection<Object[]> parameters() { List<Object[]> parameterSets = new ArrayList<>(); for (Boolean async : booleans) { for (Boolean throwException : booleans) { if (async.booleanValue()) { for (Boolean useDispatch : booleans) { for (AsyncErrorPoint errorPoint : AsyncErrorPoint.values()) { for (Boolean useStart : booleans) { if (throwException.booleanValue() && !useStart.booleanValue() && errorPoint == AsyncErrorPoint.THREAD_B_BEFORE_COMPLETE) { // Skip this combination as exceptions that occur on application // managed threads are not visible to the container. continue; } parameterSets .add(new Object[] { async, throwException, useDispatch, errorPoint, useStart }); } } } } else { // Ignore the async specific parameters parameterSets.add(new Object[] { async, throwException, Boolean.FALSE, AsyncErrorPoint.THREAD_A_AFTER_START_ASYNC, Boolean.FALSE }); } } } return parameterSets; } @Parameter(0) public boolean async; @Parameter(1) public boolean throwException; @Parameter(2) public boolean useDispatch; @Parameter(3) public AsyncErrorPoint errorPoint; @Parameter(4) public boolean useStart; @Test public void testSendError() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = getProgrammaticRootContext(); if (async) { Wrapper w = Tomcat.addServlet(ctx, "target", new TesterAsyncServlet(throwException, useDispatch, errorPoint, useStart)); w.setAsyncSupported(true); } else { Tomcat.addServlet(ctx, "target", new TesterServlet(throwException)); } ctx.addServletMappingDecoded("/target", "target"); Tomcat.addServlet(ctx, "dispatch", new TesterDispatchServlet()); ctx.addServletMappingDecoded("/dispatch", "dispatch"); Tomcat.addServlet(ctx, "error599", new ErrorServletStatic599()); ctx.addServletMappingDecoded("/error599", "error599"); Tomcat.addServlet(ctx, "errorException", new ErrorServletStaticException()); ctx.addServletMappingDecoded("/errorException", "errorException"); ErrorPage ep1 = new ErrorPage(); ep1.setErrorCode(599); ep1.setLocation("/error599"); ctx.addErrorPage(ep1); ErrorPage ep2 = new ErrorPage(); ep2.setExceptionType(SendErrorException.class.getName()); ep2.setLocation("/errorException"); ctx.addErrorPage(ep2); tomcat.start(); ByteChunk bc = new ByteChunk(); int rc; rc = getUrl("http://localhost:" + getPort() + "/target", bc, null, null); String body = bc.toString(); if (throwException) { Assert.assertEquals(500, rc); Assert.assertEquals("FAIL-Exception", body); } else { Assert.assertEquals(599, rc); Assert.assertEquals("FAIL-599", body); } } public static class TesterServlet extends HttpServlet { private static final long serialVersionUID = 1L; private final boolean throwException; public TesterServlet(boolean throwException) { this.throwException = throwException; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (throwException) { throw new SendErrorException(); } else { // Custom 5xx code so we can detect if the correct error is // reported resp.sendError(599); } } } public static class TesterAsyncServlet extends HttpServlet { private static final long serialVersionUID = 1L; private final boolean throwException; private final boolean useDispatch; private final AsyncErrorPoint errorPoint; private final boolean useStart; public TesterAsyncServlet(boolean throwException, boolean useDispatch, AsyncErrorPoint errorPoint, boolean useStart) { this.throwException = throwException; this.useDispatch = useDispatch; this.errorPoint = errorPoint; this.useStart = useStart; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (errorPoint == AsyncErrorPoint.THREAD_A_BEFORE_START_ASYNC) { doError(resp); } AsyncContext ac = req.startAsync(); ac.setTimeout(2000); if (errorPoint == AsyncErrorPoint.THREAD_A_AFTER_START_ASYNC) { doError(resp); } AsyncRunnable r = new AsyncRunnable(ac, throwException, useDispatch, errorPoint); if (useStart) { ac.start(r); } else { Thread t = new Thread(r); t.start(); } if (errorPoint == AsyncErrorPoint.THREAD_A_AFTER_START_RUNNABLE) { doError(resp); } } private void doError(HttpServletResponse resp) throws IOException { if (throwException) { throw new SendErrorException(); } else { resp.sendError(599); } } } public static class AsyncRunnable implements Runnable { private final AsyncContext ac; private final boolean throwException; private final boolean useDispatch; private final AsyncErrorPoint errorPoint; public AsyncRunnable(AsyncContext ac, boolean throwException, boolean useDispatch, AsyncErrorPoint errorPoint) { this.ac = ac; this.throwException = throwException; this.useDispatch = useDispatch; this.errorPoint = errorPoint; } @Override public void run() { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } if (throwException) { throw new SendErrorException(); } else { // Custom 5xx code so we can detect if the correct error is // reported try { ((HttpServletResponse) ac.getResponse()).sendError(599); } catch (IOException ioe) { ioe.printStackTrace(); } } if (errorPoint == AsyncErrorPoint.THREAD_B_BEFORE_COMPLETE) { if (useDispatch) { ac.dispatch("/dispatch"); } else { ac.complete(); } } } } public static class TesterDispatchServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().write("DISPATCH"); } } public static class SendErrorException extends RuntimeException { private static final long serialVersionUID = 1L; } public static class ErrorServletStatic599 extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().write("FAIL-599"); } } public static class ErrorServletStaticException extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); resp.getWriter().write("FAIL-Exception"); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
11.3
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9