Skip to content

Commit

Permalink
Issue #9309 - Introducing test for requestlog format with spaces
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Feb 3, 2023
1 parent 016de2f commit 46a316d
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -1238,6 +1239,59 @@ public void testOpenID() throws Exception
}
}

@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<String> 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<String> loggedLines = Files.readAllLines(requestLog, StandardCharsets.UTF_8);
for (String loggedLine: loggedLines)
{
assertThat(loggedLine, containsString(" [foo space here] "));
}
}
}
}

@Test
public void testDryRunProperties() throws Exception
{
Expand Down

0 comments on commit 46a316d

Please sign in to comment.