Test handling of invalid remote port header
@@ -5,8 +5,10 @@ import com.yahoo.container.logging.AccessLog;
import com.yahoo.container.logging.AccessLogEntry;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.server.HttpChannel;
+import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
+import org.eclipse.jetty.server.ServerConnector;
import org.junit.Test;
import java.util.Optional;
@@ -95,9 +97,27 @@ public class AccessLogRequestLogTest {
assertThat(accessLogEntry.getRemotePort(), is(80));
}
+ @Test
+ public void defaults_to_peer_port_if_remote_port_header_is_invalid() {
+ final AccessLogEntry accessLogEntry = new AccessLogEntry();
+ final Request jettyRequest = createRequestMock(accessLogEntry);
+ when(jettyRequest.getRequestURI()).thenReturn("/search/");
+ when(jettyRequest.getHeader("X-Forwarded-Port")).thenReturn("8o8o");
+ when(jettyRequest.getRemotePort()).thenReturn(80);
+
+ new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());
+ assertThat(accessLogEntry.getRemotePort(), is(0));
+ assertThat(accessLogEntry.getPeerPort(), is(80));
+ }
+
private static Request createRequestMock(AccessLogEntry entry) {
+ ServerConnector serverConnector = mock(ServerConnector.class);
+ when(serverConnector.getLocalPort()).thenReturn(1234);
+ HttpConnection httpConnection = mock(HttpConnection.class);
+ when(httpConnection.getConnector()).thenReturn(serverConnector);
Request request = mock(Request.class);
when(request.getAttribute(JDiscHttpServlet.ATTRIBUTE_NAME_ACCESS_LOG_ENTRY)).thenReturn(entry);
+ when(request.getAttribute("org.eclipse.jetty.server.HttpConnection")).thenReturn(httpConnection);
return request;
}
