You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "onkar717 (via GitHub)" <gi...@apache.org> on 2026/03/21 16:55:52 UTC
[PR] Add test coverage for JsonErrorReportValve [tomcat]
onkar717 opened a new pull request, #965:
URL: https://github.com/apache/tomcat/pull/965
## Description
Add dedicated test coverage for JsonErrorReportValve.
JsonErrorReportValve currently lacks specific tests compared to ErrorReportValve. This PR introduces TestJsonErrorReportValve to verify JSON error reporting behavior.
## Motivation
JsonErrorReportValve does not currently have dedicated tests. Adding coverage helps ensure correctness and stability of JSON error reporting functionality.
## Tests added
• JSON response for HTTP 500 errors
• JSON response for HTTP 404 errors
• Throwable information when exceptions occur
• JSON escaping of special characters
• Custom status code handling
• Chained exception handling
## Implementation notes
Tests follow existing patterns from TestErrorReportValve and use TomcatBaseTest utilities. No new dependencies were introduced.
## Impact
Improves test coverage and helps prevent regressions.
No production code changes were made.
This is a test-only change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: [PR] Add test coverage for JsonErrorReportValve [tomcat]
Posted by "dsoumis (via GitHub)" <gi...@apache.org>.
dsoumis commented on code in PR #965:
URL: https://github.com/apache/tomcat/pull/965#discussion_r2975465741
##########
test/org/apache/catalina/valves/TestJsonErrorReportValve.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+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.core.StandardHost;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestJsonErrorReportValve extends TomcatBaseTest {
+
+ private static final String JSON_VALVE =
+ "org.apache.catalina.valves.JsonErrorReportValve";
+
+
+ @Test
+ public void testJsonErrorResponse500() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+ ((StandardHost) tomcat.getHost()).setErrorReportValveClass(JSON_VALVE);
+
+ Context ctx = getProgrammaticRootContext();
+
+ Tomcat.addServlet(ctx, "sendError", new SendErrorServlet(
+ HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server broke"));
+ ctx.addServletMappingDecoded("/", "sendError");
+
+ tomcat.start();
+
+ ByteChunk res = new ByteChunk();
+ res.setCharset(StandardCharsets.UTF_8);
+ Map<String, List<String>> resHead = new HashMap<>();
+ int rc = getUrl("http://localhost:" + getPort(), res, resHead);
+
+ Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
+
+ String body = res.toString();
+ Assert.assertNotNull(body);
+ // Verify JSON structure
+ Assert.assertTrue("Response should contain type field",
+ body.contains("\"type\":"));
+ Assert.assertTrue("Response should contain status 500",
+ body.contains("\"status\": 500"));
+ Assert.assertTrue("Response should contain message",
+ body.contains("\"message\": \"Server broke\""));
+ Assert.assertTrue("Response should contain description field",
+ body.contains("\"description\":"));
+
+ // Verify Content-Type
+ List<String> contentType = resHead.get("Content-Type");
+ Assert.assertNotNull("Content-Type header should be present", contentType);
+ Assert.assertTrue("Content-Type should be application/json",
+ contentType.get(0).contains("application/json"));
+ }
+
+
+ @Test
+ public void testJsonErrorWithThrowable() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+ ((StandardHost) tomcat.getHost()).setErrorReportValveClass(JSON_VALVE);
+
+ Context ctx = getProgrammaticRootContext();
+
+ Tomcat.addServlet(ctx, "exception",
+ new ExceptionServlet("Something went wrong"));
+ ctx.addServletMappingDecoded("/", "exception");
+
+ 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);
+ Assert.assertTrue("Response should contain throwable field",
+ body.contains("\"throwable\":"));
+ Assert.assertTrue("Response should contain exception class name",
+ body.contains("RuntimeException"));
+ Assert.assertTrue("Response should contain exception message",
+ body.contains("Something went wrong"));
+ }
+
+
+ @Test
+ public void testJsonErrorWithSpecialChars() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+ ((StandardHost) tomcat.getHost()).setErrorReportValveClass(JSON_VALVE);
+
+ Context ctx = getProgrammaticRootContext();
+
+ // Characters that require JSON escaping: quotes and backslashes
+ String specialMessage =
+ "Error with \"quotes\" and \\backslash\\ and <angle>";
Review Comment:
angle brackets are not escaped
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: [PR] Add test coverage for JsonErrorReportValve [tomcat]
Posted by "dsoumis (via GitHub)" <gi...@apache.org>.
dsoumis commented on code in PR #965:
URL: https://github.com/apache/tomcat/pull/965#discussion_r2975368636
##########
test/org/apache/catalina/valves/TestJsonErrorReportValve.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+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.core.StandardHost;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestJsonErrorReportValve extends TomcatBaseTest {
+
+ private static final String JSON_VALVE =
+ "org.apache.catalina.valves.JsonErrorReportValve";
+
+
+ @Test
+ public void testJsonErrorResponse500() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+ ((StandardHost) tomcat.getHost()).setErrorReportValveClass(JSON_VALVE);
+
+ Context ctx = getProgrammaticRootContext();
+
+ Tomcat.addServlet(ctx, "sendError", new SendErrorServlet(
+ HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server broke"));
+ ctx.addServletMappingDecoded("/", "sendError");
+
+ tomcat.start();
+
+ ByteChunk res = new ByteChunk();
+ res.setCharset(StandardCharsets.UTF_8);
+ Map<String, List<String>> resHead = new HashMap<>();
+ int rc = getUrl("http://localhost:" + getPort(), res, resHead);
+
+ Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
+
+ String body = res.toString();
+ Assert.assertNotNull(body);
+ // Verify JSON structure
+ Assert.assertTrue("Response should contain type field",
+ body.contains("\"type\":"));
Review Comment:
It needs a space after the ':'
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: [PR] Add test coverage for JsonErrorReportValve [tomcat]
Posted by "dsoumis (via GitHub)" <gi...@apache.org>.
dsoumis merged PR #965:
URL: https://github.com/apache/tomcat/pull/965
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: [PR] Add test coverage for JsonErrorReportValve [tomcat]
Posted by "rmaucher (via GitHub)" <gi...@apache.org>.
rmaucher commented on PR #965:
URL: https://github.com/apache/tomcat/pull/965#issuecomment-4111143935
I have a similar JSON test here: https://github.com/apache/tomcat/blob/main/test/org/apache/catalina/manager/TestStatusTransformer.java#L115
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: [PR] Add test coverage for JsonErrorReportValve [tomcat]
Posted by "dsoumis (via GitHub)" <gi...@apache.org>.
dsoumis commented on code in PR #965:
URL: https://github.com/apache/tomcat/pull/965#discussion_r2975410166
##########
test/org/apache/catalina/valves/TestJsonErrorReportValve.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+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.core.StandardHost;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestJsonErrorReportValve extends TomcatBaseTest {
+
+ private static final String JSON_VALVE =
+ "org.apache.catalina.valves.JsonErrorReportValve";
+
+
+ @Test
+ public void testJsonErrorResponse500() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+ ((StandardHost) tomcat.getHost()).setErrorReportValveClass(JSON_VALVE);
+
+ Context ctx = getProgrammaticRootContext();
+
+ Tomcat.addServlet(ctx, "sendError", new SendErrorServlet(
+ HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server broke"));
+ ctx.addServletMappingDecoded("/", "sendError");
+
+ tomcat.start();
+
+ ByteChunk res = new ByteChunk();
+ res.setCharset(StandardCharsets.UTF_8);
+ Map<String, List<String>> resHead = new HashMap<>();
+ int rc = getUrl("http://localhost:" + getPort(), res, resHead);
+
+ Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
+
+ String body = res.toString();
+ Assert.assertNotNull(body);
+ // Verify JSON structure
+ Assert.assertTrue("Response should contain type field",
+ body.contains("\"type\":"));
Review Comment:
You can also verify that type contains a value of "errorReportValve.exceptionReport" or "errorReportValve.statusReport" equivalently for each test scenario
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org
Re: [PR] Add test coverage for JsonErrorReportValve [tomcat]
Posted by "dsoumis (via GitHub)" <gi...@apache.org>.
dsoumis commented on PR #965:
URL: https://github.com/apache/tomcat/pull/965#issuecomment-4110988750
You can utilize org.apache.tomcat.util.json.JSONParser to parse the response instead of using the body.contains() method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org