From 46a316d4dfe2dafd608ccdbb610523ca87e35147 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 3 Feb 2023 08:45:35 -0600 Subject: [PATCH] Issue #9309 - Introducing test for requestlog format with spaces Signed-off-by: Joakim Erdfelt --- .../tests/distribution/DistributionTests.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java b/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java index 3a507840503..db6c49bd067 100644 --- a/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java +++ b/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java @@ -23,6 +23,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Queue; @@ -1238,6 +1239,59 @@ public class DistributionTests extends AbstractJettyHomeTest } } + @Test + public void testRequestLogFormatWithSpaces() throws Exception + { + Path jettyBase = newTestJettyBaseDirectory(); + String jettyVersion = System.getProperty("jettyVersion"); + JettyHomeTester distribution = JettyHomeTester.Builder.newInstance() + .jettyVersion(jettyVersion) + .jettyBase(jettyBase) + .mavenLocalRepository(System.getProperty("mavenRepoPath")) + .build(); + + String[] args1 = {"--add-module=server,http,deploy,requestlog"}; + try (JettyHomeTester.Run run1 = distribution.start(args1)) + { + assertTrue(run1.awaitFor(10, TimeUnit.SECONDS)); + assertEquals(0, run1.getExitValue()); + + // Setup custom format string with spaces + Path requestLogIni = distribution.getJettyBase().resolve("start.d/requestlog.ini"); + List lines = List.of( + "--module=requestlog", + "jetty.requestlog.filePath=logs/test.request.log", + "jetty.requestlog.formatString=%{client}a - %u %{dd/MMM/yyyy:HH:mm:ss ZZZ|GMT}t [foo space here] \"%r\" %s %O \"%{Referer}i\" \"%{User-Agent}i\"" + ); + Files.write(requestLogIni, lines, StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); + + int port = distribution.freePort(); + String[] args2 = { + "jetty.http.port=" + port, + }; + try (JettyHomeTester.Run run2 = distribution.start(args2)) + { + assertTrue(run2.awaitConsoleLogsFor("Started Server@", 10, TimeUnit.SECONDS)); + startHttpClient(false); + + String uri = "http://localhost:" + port + "/test"; + + // Generate a request + ContentResponse response = client.GET(uri + "/"); + // Don't really care about the result, as any request should be logged in the requestlog + // We are just asserting a status here to ensure that the request is complete + assertThat(response.getStatus(), is(HttpStatus.NOT_FOUND_404)); + + Path requestLog = distribution.getJettyBase().resolve("logs/test.request.log"); + List loggedLines = Files.readAllLines(requestLog, StandardCharsets.UTF_8); + for (String loggedLine: loggedLines) + { + assertThat(loggedLine, containsString(" [foo space here] ")); + } + } + } + } + @Test public void testDryRunProperties() throws Exception {