Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions test/org/apache/catalina/valves/TestErrorReportValve.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
Expand Down Expand Up @@ -263,4 +264,141 @@ public void testErrorPageServlet() throws Exception {
}


@Test
public void testShowReportFalse() throws Exception {
Tomcat tomcat = getTomcatInstance();

Context ctx = getProgrammaticRootContext();

Tomcat.addServlet(ctx, "error404", new Bug54220Servlet(true));
ctx.addServletMappingDecoded("/", "error404");

tomcat.start();

// Find ErrorReportValve in the host pipeline
ErrorReportValve erv = findErrorReportValve(tomcat);
Assert.assertNotNull("ErrorReportValve should exist", erv);
erv.setShowReport(false);

ByteChunk res = new ByteChunk();
res.setCharset(StandardCharsets.UTF_8);
int rc = getUrl("http://localhost:" + getPort(), res, null);

Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
String body = res.toString();
if (body != null) {
Assert.assertFalse("Report should be hidden",
body.contains(sm.getString("errorReportValve.description")));
}
Assert.assertFalse(erv.isShowReport());
}


@Test
public void testShowServerInfoFalse() throws Exception {
Tomcat tomcat = getTomcatInstance();

Context ctx = getProgrammaticRootContext();

Tomcat.addServlet(ctx, "error500", new ErrorServlet());
ctx.addServletMappingDecoded("/", "error500");

tomcat.start();

ErrorReportValve erv = findErrorReportValve(tomcat);
Assert.assertNotNull("ErrorReportValve should exist", erv);
erv.setShowServerInfo(false);

ByteChunk res = new ByteChunk();
res.setCharset(StandardCharsets.UTF_8);
getUrl("http://localhost:" + getPort(), res, null);

String body = res.toString();
if (body != null) {
Assert.assertFalse("Server info should be hidden",
body.contains("Apache Tomcat"));
}
Assert.assertFalse(erv.isShowServerInfo());
}


@Test
public void testSetPropertyErrorCode() {
ErrorReportValve valve = new ErrorReportValve();

Assert.assertTrue(valve.setProperty("errorCode.404", "/error404.html"));
Assert.assertEquals("/error404.html", valve.getProperty("errorCode.404"));
}


@Test
public void testSetPropertyExceptionType() {
ErrorReportValve valve = new ErrorReportValve();

Assert.assertTrue(valve.setProperty(
"exceptionType.java.lang.NullPointerException", "/npe.html"));
Assert.assertEquals("/npe.html", valve.getProperty(
"exceptionType.java.lang.NullPointerException"));
}


@Test
public void testGetPropertyNotFound() {
ErrorReportValve valve = new ErrorReportValve();

Assert.assertNull(valve.getProperty("errorCode.999"));
Assert.assertNull(valve.getProperty("exceptionType.com.example.Nope"));
}


@Test
public void testGetPropertyUnknownPrefix() {
ErrorReportValve valve = new ErrorReportValve();

Assert.assertFalse(valve.setProperty("unknownPrefix.something", "/x.html"));
Assert.assertNull(valve.getProperty("unknownPrefix.something"));
}


@Test
public void testExceptionWithRootCause() throws Exception {
Tomcat tomcat = getTomcatInstance();

Context ctx = getProgrammaticRootContext();

Tomcat.addServlet(ctx, "nestedError", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Throwable root = new IllegalStateException("root cause");
Throwable wrapper = new RuntimeException("wrapper", root);
req.setAttribute(RequestDispatcher.ERROR_EXCEPTION, wrapper);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
});
ctx.addServletMappingDecoded("/", "nestedError");

tomcat.start();

ByteChunk res = new ByteChunk();
res.setCharset(StandardCharsets.UTF_8);
int rc = getUrl("http://localhost:" + getPort(), res, null);

Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
String body = res.toString();
Assert.assertNotNull(body);
// Should contain root cause section
Assert.assertTrue(body.contains(sm.getString("errorReportValve.rootCause")));
}


private static ErrorReportValve findErrorReportValve(Tomcat tomcat) {
for (Valve v : tomcat.getHost().getPipeline().getValves()) {
if (v instanceof ErrorReportValve) {
return (ErrorReportValve) v;
}
}
return null;
}
}
135 changes: 135 additions & 0 deletions test/org/apache/catalina/valves/TestLoadBalancerDrainingValveUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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.valves;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

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

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

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;

/**
* Additional tests for {@link LoadBalancerDrainingValve} covering property
* getters/setters and URI edge cases not covered by the parameterized test.
*/
public class TestLoadBalancerDrainingValveUnit extends TomcatBaseTest {

@Test
public void testGetSetProperties() {
LoadBalancerDrainingValve valve = new LoadBalancerDrainingValve();

valve.setRedirectStatusCode(302);
// No getter for redirectStatusCode, so just verify no exception

valve.setIgnoreCookieName("testCookie");
Assert.assertEquals("testCookie", valve.getIgnoreCookieName());

valve.setIgnoreCookieValue("testValue");
Assert.assertEquals("testValue", valve.getIgnoreCookieValue());
}


@Test
public void testActiveNodePassesThrough() throws Exception {
Tomcat tomcat = getTomcatInstance();
Context ctx = getProgrammaticRootContext();

Tomcat.addServlet(ctx, "ok", new OkServlet());
ctx.addServletMappingDecoded("/", "ok");

LoadBalancerDrainingValve valve = new LoadBalancerDrainingValve();
ctx.getPipeline().addValve(valve);

tomcat.start();

ByteChunk res = new ByteChunk();
res.setCharset(StandardCharsets.UTF_8);
int rc = getUrl("http://localhost:" + getPort() + "/test", res, null);

// No JK_LB_ACTIVATION attribute set, so request passes through
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
Assert.assertEquals("OK", res.toString());
}


@Test
public void testCollapseMultipleSlashesInURI() throws Exception {
Tomcat tomcat = getTomcatInstance();
Context ctx = getProgrammaticRootContext();

Tomcat.addServlet(ctx, "ok", new OkServlet());
ctx.addServletMappingDecoded("/", "ok");

LoadBalancerDrainingValve valve = new LoadBalancerDrainingValve();
ctx.getPipeline().addValve(valve);

tomcat.start();

// Request with multiple leading slashes but no DIS activation
// should pass through normally
ByteChunk res = new ByteChunk();
res.setCharset(StandardCharsets.UTF_8);
int rc = getUrl("http://localhost:" + getPort() + "///test", res, null);

Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}


@Test
public void testSessionURIParam() throws Exception {
Tomcat tomcat = getTomcatInstance();
Context ctx = getProgrammaticRootContext();

Tomcat.addServlet(ctx, "ok", new OkServlet());
ctx.addServletMappingDecoded("/", "ok");

LoadBalancerDrainingValve valve = new LoadBalancerDrainingValve();
ctx.getPipeline().addValve(valve);

tomcat.start();

// Request with jsessionid parameter but no DIS activation
ByteChunk res = new ByteChunk();
res.setCharset(StandardCharsets.UTF_8);
int rc = getUrl("http://localhost:" + getPort() +
"/test;jsessionid=abc123", res, null);

Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}


private static class OkServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

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