|
| 1 | +package io.mincong.jetty; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpRequest; |
| 9 | +import java.net.http.HttpResponse.BodyHandlers; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import javax.servlet.http.HttpServlet; |
| 13 | +import javax.servlet.http.HttpServletRequest; |
| 14 | +import javax.servlet.http.HttpServletResponse; |
| 15 | +import org.eclipse.jetty.server.CustomRequestLog; |
| 16 | +import org.eclipse.jetty.server.Server; |
| 17 | +import org.eclipse.jetty.server.handler.RequestLogHandler; |
| 18 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 19 | +import org.eclipse.jetty.servlet.ServletHolder; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.io.TempDir; |
| 24 | + |
| 25 | +class RequestLogTest { |
| 26 | + |
| 27 | + private Server server; |
| 28 | + private Path logPath; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + void setUp(@TempDir Path tempDir) throws Exception { |
| 32 | + // Create servlet handler |
| 33 | + var servletHolder = new ServletHolder(MyServlet.class); |
| 34 | + var context = new ServletContextHandler(ServletContextHandler.SESSIONS); |
| 35 | + context.setContextPath("/"); |
| 36 | + context.addServlet(servletHolder, "/"); |
| 37 | + |
| 38 | + // Create log handler |
| 39 | + logPath = tempDir.resolve("request.log").toAbsolutePath(); |
| 40 | + var logHandler = new RequestLogHandler(); |
| 41 | + logHandler.setRequestLog(new CustomRequestLog(logPath.toString())); |
| 42 | + logHandler.setHandler(context); |
| 43 | + |
| 44 | + // Start server |
| 45 | + server = new Server(8080); |
| 46 | + server.setHandler(logHandler); |
| 47 | + server.start(); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void tearDown() throws Exception { |
| 52 | + server.stop(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Log all HTTP requests into file "request.log", such as: |
| 57 | + * |
| 58 | + * <pre> |
| 59 | + * 127.0.0.1 - - [04/Jul/2020:08:38:59 +0000] "GET / HTTP/1.1" 200 31 "-" "Java-http-client/11.0.2" |
| 60 | + * </pre> |
| 61 | + * |
| 62 | + * where: |
| 63 | + * |
| 64 | + * <ul> |
| 65 | + * <li>{@code 127.0.0.1} is the IP address of the client (remote host) which made the request to |
| 66 | + * the server. |
| 67 | + * <li>{@code -} is the user-identifier is the RFC 1413 identity of the client. Usually "-". |
| 68 | + * <li>{@code -} is the user-id of the person requesting the document. Usually "-" unless |
| 69 | + * .htaccess has requested authentication. |
| 70 | + * <li>{@code [04/Jul/2020:08:38:59 +0000]} is the date, time, and time zone that the request |
| 71 | + * was received, by default in strftime format "%d/%b/%Y:%H:%M:%S %z". |
| 72 | + * <li>{@code "GET / HTTP/1.1"} is the request line from the client. The method GET, / the |
| 73 | + * resource requested, and HTTP/1.1 the HTTP protocol. |
| 74 | + * <li>{@code 200} is the HTTP status code returned to the client. 2xx is a successful response, |
| 75 | + * 3xx a redirection, 4xx a client error, and 5xx a server error. |
| 76 | + * <li>{@code 31} is the size of the object returned to the client, measured in bytes. |
| 77 | + * <li>{@code "Java-http-client/11.0.2"} is the user-agent. |
| 78 | + * </ul> |
| 79 | + * |
| 80 | + * @see <a href="https://en.wikipedia.org/wiki/Common_Log_Format">Common Log Format | |
| 81 | + * Wikipedia</a> |
| 82 | + * @see CustomRequestLog |
| 83 | + */ |
| 84 | + @Test |
| 85 | + void requestLog() throws Exception { |
| 86 | + // When sending a HTTP request |
| 87 | + var httpClient = HttpClient.newHttpClient(); |
| 88 | + var request = HttpRequest.newBuilder(URI.create("http://localhost:8080")).build(); |
| 89 | + var response = httpClient.send(request, BodyHandlers.ofString()); |
| 90 | + |
| 91 | + // Then the response is OK |
| 92 | + assertThat(response.statusCode()).isEqualTo(200); |
| 93 | + |
| 94 | + // And the request is logged |
| 95 | + assertThat(Files.readString(logPath)) |
| 96 | + .startsWith("127.0.0.1 - - [") |
| 97 | + .contains("] \"GET / HTTP/1.1\" 200 31 \"-\" \"Java-http-client/"); |
| 98 | + } |
| 99 | + |
| 100 | + public static class MyServlet extends HttpServlet { |
| 101 | + @Override |
| 102 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 103 | + throws IOException { |
| 104 | + response.getWriter().write("{\"message\":\"Welcome to Jetty!\"}"); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments