|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.catalina.valves; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | + |
| 21 | +import jakarta.servlet.http.HttpServletResponse; |
| 22 | + |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import org.apache.catalina.Context; |
| 27 | +import org.apache.catalina.startup.Tomcat; |
| 28 | +import org.apache.catalina.startup.TomcatBaseTest; |
| 29 | +import org.apache.tomcat.util.buf.ByteChunk; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link JsonAccessLogValve}. |
| 33 | + */ |
| 34 | +public class TestJsonAccessLogValve extends TomcatBaseTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testDefaultPatternProducesValidJson() throws Exception { |
| 38 | + Tomcat tomcat = getTomcatInstance(); |
| 39 | + Context ctx = getProgrammaticRootContext(); |
| 40 | + |
| 41 | + Tomcat.addServlet(ctx, "hello", new HelloWorldServlet()); |
| 42 | + ctx.addServletMappingDecoded("/", "hello"); |
| 43 | + |
| 44 | + JsonAccessLogValve valve = new JsonAccessLogValve(); |
| 45 | + valve.setDirectory(getTemporaryDirectory().getAbsolutePath()); |
| 46 | + valve.setPrefix("access_json_test"); |
| 47 | + valve.setSuffix(".log"); |
| 48 | + ctx.getParent().getPipeline().addValve(valve); |
| 49 | + |
| 50 | + tomcat.start(); |
| 51 | + |
| 52 | + ByteChunk res = new ByteChunk(); |
| 53 | + res.setCharset(StandardCharsets.UTF_8); |
| 54 | + int rc = getUrl("http://localhost:" + getPort() + "/", res, null); |
| 55 | + |
| 56 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 57 | + Assert.assertEquals(HelloWorldServlet.RESPONSE_TEXT, res.toString()); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testCommonPatternProducesValidJson() throws Exception { |
| 63 | + Tomcat tomcat = getTomcatInstance(); |
| 64 | + Context ctx = getProgrammaticRootContext(); |
| 65 | + |
| 66 | + Tomcat.addServlet(ctx, "hello", new HelloWorldServlet()); |
| 67 | + ctx.addServletMappingDecoded("/", "hello"); |
| 68 | + |
| 69 | + JsonAccessLogValve valve = new JsonAccessLogValve(); |
| 70 | + valve.setPattern("common"); |
| 71 | + valve.setDirectory(getTemporaryDirectory().getAbsolutePath()); |
| 72 | + valve.setPrefix("access_json_common"); |
| 73 | + valve.setSuffix(".log"); |
| 74 | + ctx.getParent().getPipeline().addValve(valve); |
| 75 | + |
| 76 | + tomcat.start(); |
| 77 | + |
| 78 | + ByteChunk res = new ByteChunk(); |
| 79 | + res.setCharset(StandardCharsets.UTF_8); |
| 80 | + int rc = getUrl("http://localhost:" + getPort() + "/", res, null); |
| 81 | + |
| 82 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testCombinedPatternProducesValidJson() throws Exception { |
| 88 | + Tomcat tomcat = getTomcatInstance(); |
| 89 | + Context ctx = getProgrammaticRootContext(); |
| 90 | + |
| 91 | + Tomcat.addServlet(ctx, "hello", new HelloWorldServlet()); |
| 92 | + ctx.addServletMappingDecoded("/", "hello"); |
| 93 | + |
| 94 | + JsonAccessLogValve valve = new JsonAccessLogValve(); |
| 95 | + valve.setPattern("combined"); |
| 96 | + valve.setDirectory(getTemporaryDirectory().getAbsolutePath()); |
| 97 | + valve.setPrefix("access_json_combined"); |
| 98 | + valve.setSuffix(".log"); |
| 99 | + ctx.getParent().getPipeline().addValve(valve); |
| 100 | + |
| 101 | + tomcat.start(); |
| 102 | + |
| 103 | + ByteChunk res = new ByteChunk(); |
| 104 | + res.setCharset(StandardCharsets.UTF_8); |
| 105 | + int rc = getUrl("http://localhost:" + getPort() + "/", res, null); |
| 106 | + |
| 107 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testCustomPatternWithRequestHeaders() throws Exception { |
| 113 | + Tomcat tomcat = getTomcatInstance(); |
| 114 | + Context ctx = getProgrammaticRootContext(); |
| 115 | + |
| 116 | + Tomcat.addServlet(ctx, "hello", new HelloWorldServlet()); |
| 117 | + ctx.addServletMappingDecoded("/", "hello"); |
| 118 | + |
| 119 | + JsonAccessLogValve valve = new JsonAccessLogValve(); |
| 120 | + // Pattern includes request header and response header elements |
| 121 | + valve.setPattern("%h %s %{User-Agent}i %{Content-Type}o"); |
| 122 | + valve.setDirectory(getTemporaryDirectory().getAbsolutePath()); |
| 123 | + valve.setPrefix("access_json_headers"); |
| 124 | + valve.setSuffix(".log"); |
| 125 | + ctx.getParent().getPipeline().addValve(valve); |
| 126 | + |
| 127 | + tomcat.start(); |
| 128 | + |
| 129 | + ByteChunk res = new ByteChunk(); |
| 130 | + res.setCharset(StandardCharsets.UTF_8); |
| 131 | + int rc = getUrl("http://localhost:" + getPort() + "/", res, null); |
| 132 | + |
| 133 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testPatternWithCookiesAndSessionAttributes() throws Exception { |
| 139 | + Tomcat tomcat = getTomcatInstance(); |
| 140 | + Context ctx = getProgrammaticRootContext(); |
| 141 | + |
| 142 | + Tomcat.addServlet(ctx, "hello", new HelloWorldServlet()); |
| 143 | + ctx.addServletMappingDecoded("/", "hello"); |
| 144 | + |
| 145 | + JsonAccessLogValve valve = new JsonAccessLogValve(); |
| 146 | + // Pattern includes cookies, request attributes, and session attributes |
| 147 | + valve.setPattern("%h %s %{JSESSIONID}c %{testAttr}r %{testSession}s"); |
| 148 | + valve.setDirectory(getTemporaryDirectory().getAbsolutePath()); |
| 149 | + valve.setPrefix("access_json_cookies"); |
| 150 | + valve.setSuffix(".log"); |
| 151 | + ctx.getParent().getPipeline().addValve(valve); |
| 152 | + |
| 153 | + tomcat.start(); |
| 154 | + |
| 155 | + ByteChunk res = new ByteChunk(); |
| 156 | + res.setCharset(StandardCharsets.UTF_8); |
| 157 | + int rc = getUrl("http://localhost:" + getPort() + "/", res, null); |
| 158 | + |
| 159 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testCreateLogElementsOutputFormat() { |
| 165 | + JsonAccessLogValve valve = new JsonAccessLogValve(); |
| 166 | + valve.setPattern("%h %s %b"); |
| 167 | + |
| 168 | + // Use createLogElements via the internal API |
| 169 | + AbstractAccessLogValve.AccessLogElement[] elements = |
| 170 | + valve.createLogElements(); |
| 171 | + |
| 172 | + // Verify that elements array is valid and has at least |
| 173 | + // opening brace + 3 fields + separators + closing brace |
| 174 | + Assert.assertNotNull(elements); |
| 175 | + Assert.assertTrue("Should have at least some elements", |
| 176 | + elements.length > 0); |
| 177 | + } |
| 178 | +} |
0 commit comments