// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

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.Request;
import org.eclipse.jetty.server.Response;
import org.junit.Test;

import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Oyvind Bakksjo
 * @author bjorncs
 */
public class AccessLogRequestLogTest {
    @Test
    public void requireThatQueryWithUnquotedSpecialCharactersIsHandled() {
        final AccessLogEntry accessLogEntry = new AccessLogEntry();
        final Request jettyRequest = createRequestMock(accessLogEntry);
        when(jettyRequest.getRequestURI()).thenReturn("/search/");
        when(jettyRequest.getQueryString()).thenReturn("query=year:>2010");

        new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());

        assertThat(accessLogEntry.getRawPath(), is(not(nullValue())));
        assertTrue(accessLogEntry.getRawQuery().isPresent());
    }

    @Test
    public void requireThatDoubleQuotingIsNotPerformed() {
        final AccessLogEntry accessLogEntry = new AccessLogEntry();
        final Request jettyRequest = createRequestMock(accessLogEntry);
        final String path = "/search/";
        when(jettyRequest.getRequestURI()).thenReturn(path);
        final String query = "query=year%252010+%3B&customParameter=something";
        when(jettyRequest.getQueryString()).thenReturn(query);

        new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());

        assertThat(accessLogEntry.getRawPath(), is(path));
        assertThat(accessLogEntry.getRawQuery().get(), is(query));

    }

    @Test
    public void raw_path_and_query_are_set_from_request() {
        AccessLogEntry accessLogEntry = new AccessLogEntry();
        Request jettyRequest = createRequestMock(accessLogEntry);
        String rawPath = "//search/";
        when(jettyRequest.getRequestURI()).thenReturn(rawPath);
        String rawQuery = "q=%%2";
        when(jettyRequest.getQueryString()).thenReturn(rawQuery);

        new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());
        assertThat(accessLogEntry.getRawPath(), is(rawPath));
        Optional<String> actualRawQuery = accessLogEntry.getRawQuery();
        assertThat(actualRawQuery.isPresent(), is(true));
        assertThat(actualRawQuery.get(), is(rawQuery));
    }

    @Test
    public void verify_x_forwarded_for_precedence () {
        AccessLogEntry accessLogEntry = new AccessLogEntry();
        Request jettyRequest = createRequestMock(accessLogEntry);
        when(jettyRequest.getRequestURI()).thenReturn("//search/");
        when(jettyRequest.getQueryString()).thenReturn("q=%%2");
        when(jettyRequest.getHeader("x-forwarded-for")).thenReturn("1.2.3.4");
        when(jettyRequest.getHeader("y-ra")).thenReturn("2.3.4.5");

        new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());
        assertThat(accessLogEntry.getRemoteAddress(), is("1.2.3.4"));
    }

    @Test
    public void verify_x_forwarded_port_precedence () {
        AccessLogEntry accessLogEntry = new AccessLogEntry();
        Request jettyRequest = createRequestMock(accessLogEntry);
        when(jettyRequest.getRequestURI()).thenReturn("//search/");
        when(jettyRequest.getQueryString()).thenReturn("q=%%2");
        when(jettyRequest.getHeader("X-Forwarded-Port")).thenReturn("80");
        when(jettyRequest.getHeader("y-rp")).thenReturn("8080");

        new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());
        assertThat(accessLogEntry.getRemotePort(), is(80));
    }

    private static Request createRequestMock(AccessLogEntry entry) {
        Request request = mock(Request.class);
        when(request.getAttribute(JDiscHttpServlet.ATTRIBUTE_NAME_ACCESS_LOG_ENTRY)).thenReturn(entry);
        return request;
    }

    private Response createResponseMock() {
        Response response = mock(Response.class);
        when(response.getHttpChannel()).thenReturn(mock(HttpChannel.class));
        when(response.getCommittedMetaData()).thenReturn(mock(MetaData.Response.class));
        return response;
    }
}

@@ -338,16 +338,64 @@
 hannel;%0A
+import org.eclipse.jetty.server.HttpConnection;%0A
 import o
@@ -469,16 +469,65 @@
 sponse;%0A
+import org.eclipse.jetty.server.ServerConnector;%0A
 import o
@@ -4309,72 +4309,996 @@
     
-private static Request createRequestMock(AccessLogEntry entry) %7B
+@Test%0A    public void defaults_to_peer_port_if_remote_port_header_is_invalid() %7B%0A        final AccessLogEntry accessLogEntry = new AccessLogEntry();%0A        final Request jettyRequest = createRequestMock(accessLogEntry);%0A        when(jettyRequest.getRequestURI()).thenReturn(%22/search/%22);%0A        when(jettyRequest.getHeader(%22X-Forwarded-Port%22)).thenReturn(%228o8o%22);%0A        when(jettyRequest.getRemotePort()).thenReturn(80);%0A%0A        new AccessLogRequestLog(mock(AccessLog.class)).log(jettyRequest, createResponseMock());%0A        assertThat(accessLogEntry.getRemotePort(), is(0));%0A        assertThat(accessLogEntry.getPeerPort(), is(80));%0A    %7D%0A%0A    private static Request createRequestMock(AccessLogEntry entry) %7B%0A        ServerConnector serverConnector = mock(ServerConnector.class);%0A        when(serverConnector.getLocalPort()).thenReturn(1234);%0A        HttpConnection httpConnection = mock(HttpConnection.class);%0A        when(httpConnection.getConnector()).thenReturn(serverConnector);
 %0A   
@@ -5441,24 +5441,130 @@
 urn(entry);%0A
+        when(request.getAttribute(%22org.eclipse.jetty.server.HttpConnection%22)).thenReturn(httpConnection);%0A
         retu

Test handling of invalid remote port header