/*
 *  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 org.apache.catalina.core;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;
import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Response;
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;

public class TestStandardHostValve extends TomcatBaseTest {

    @Test
    public void testErrorPageHandling() throws Exception {
        // Set up a container
        Tomcat tomcat = getTomcatInstance();

        // No file system docBase required
        Context ctx = tomcat.addContext("", null);

        // Add the error page
        Tomcat.addServlet(ctx, "error", new ErrorServlet());
        ctx.addServletMapping("/error", "error");

        // Add the error handling page
        Tomcat.addServlet(ctx, "report", new ReportServlet());
        ctx.addServletMapping("/report/*", "report");

        // And the handling for 500 responses
        ErrorPage errorPage500 = new ErrorPage();
        errorPage500.setErrorCode(Response.SC_INTERNAL_SERVER_ERROR);
        errorPage500.setLocation("/report/500");
        ctx.addErrorPage(errorPage500);

        // And the default error handling
        ErrorPage errorPageDefault = new ErrorPage();
        errorPageDefault.setLocation("/report/default");
        ctx.addErrorPage(errorPageDefault);

        tomcat.start();

        doTestErrorPageHandling(500, "/500");
        doTestErrorPageHandling(501, "/default");
    }

    private void doTestErrorPageHandling(int error, String report)
            throws Exception {

        // Request a page that triggers an error
        ByteChunk bc = new ByteChunk();
        int rc = getUrl("http://localhost:" + getPort() +
                "/error?errorCode=" + error, bc, null);

        Assert.assertEquals(error, rc);
        Assert.assertEquals(report, bc.toString());
    }

    private static class ErrorServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            int error =
                    Integer.valueOf(req.getParameter("errorCode")).intValue();
            resp.sendError(error);
        }
    }

    private static class ReportServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            String pathInfo = req.getPathInfo();
            resp.setContentType("text/plain");
            resp.getWriter().print(pathInfo);
        }
    }
}

@@ -874,46 +874,184 @@
 on;%0A
-%0Aimport javax.servlet.ServletException
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.ServletRequestEvent;%0Aimport javax.servlet.ServletRequestListener
 ;%0Aim
@@ -1206,16 +1206,41 @@
 Assert;%0A
+import org.junit.Ignore;%0A
 import o
@@ -2708,32 +2708,1416 @@
 fault%22);%0A    %7D%0A%0A
+    @Test%0A    @Ignore%0A    public void testSRLAfterError() throws Exception %7B%0A        // Set up a container%0A        Tomcat tomcat = getTomcatInstance();%0A%0A        // No file system docBase required%0A        Context ctx = tomcat.addContext(%22%22, null);%0A%0A        // Add the error page%0A        Tomcat.addServlet(ctx, %22error%22, new ErrorServlet());%0A        ctx.addServletMapping(%22/error%22, %22error%22);%0A%0A        final List%3CString%3E result = new ArrayList%3C%3E();%0A%0A        // Add the request listener%0A        ServletRequestListener servletRequestListener = new ServletRequestListener() %7B%0A%0A            @Override%0A            public void requestDestroyed(ServletRequestEvent sre) %7B%0A                result.add(%22Visit requestDestroyed%22);%0A            %7D%0A%0A            @Override%0A            public void requestInitialized(ServletRequestEvent sre) %7B%0A                result.add(%22Visit requestInitialized%22);%0A            %7D%0A%0A        %7D;%0A        ((StandardContext) ctx).addApplicationEventListener(servletRequestListener);%0A%0A        tomcat.start();%0A%0A        // Request a page that triggers an error%0A        ByteChunk bc = new ByteChunk();%0A        int rc = getUrl(%22http://localhost:%22 + getPort() + %22/error?errorCode=400%22, bc, null);%0A%0A        Assert.assertEquals(400, rc);%0A        Assert.assertTrue(result.contains(%22Visit requestInitialized%22));%0A        Assert.assertTrue(result.contains(%22Visit requestDestroyed%22));%0A    %7D%0A%0A
     private void

Test case that reproduces ServletRequestListener.requestDestroyed problem. Mark it with @Ignore in order to keep the build 'green'.