diff options
Diffstat (limited to 'jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty')
2 files changed, 34 insertions, 2 deletions
diff --git a/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpRequestFactoryTest.java b/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpRequestFactoryTest.java index 564475b5603..752e4bed119 100644 --- a/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpRequestFactoryTest.java +++ b/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpRequestFactoryTest.java @@ -10,6 +10,7 @@ import com.yahoo.jdisc.handler.RequestHandler; import com.yahoo.jdisc.http.HttpRequest; import com.yahoo.jdisc.service.CurrentContainer; import org.eclipse.jetty.server.HttpConnection; +import org.eclipse.jetty.server.ServerConnector; import org.junit.Test; import javax.servlet.http.HttpServletRequest; @@ -67,7 +68,10 @@ public class HttpRequestFactoryTest { private static HttpServletRequest createMockRequest(String scheme, String serverName, String path, String queryString) { HttpServletRequest request = mock(HttpServletRequest.class); HttpConnection connection = mock(HttpConnection.class); + ServerConnector connector = mock(ServerConnector.class); + when(connector.getLocalPort()).thenReturn(LOCAL_PORT); when(connection.getCreatedTimeStamp()).thenReturn(System.currentTimeMillis()); + when(connection.getConnector()).thenReturn(connector); when(request.getAttribute("org.eclipse.jetty.server.HttpConnection")).thenReturn(connection); when(request.getProtocol()).thenReturn("HTTP/1.1"); when(request.getScheme()).thenReturn(scheme); diff --git a/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java b/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java index dfa4fe37ef3..37f717437ee 100644 --- a/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java +++ b/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpServerTest.java @@ -103,6 +103,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.Mockito.mock; @@ -733,12 +734,36 @@ public class HttpServerTest { assertLogEntryHasRemote(accessLogMock.logEntries.get(1), proxiedRemoteAddress, 0); } - private void sendJettyClientRequest(TestDriver testDriver, HttpClient client, Object tag) + @Test + public void requireThatJdiscLocalPortPropertyIsNotOverriddenByProxyProtocol() throws Exception { + Path privateKeyFile = tmpFolder.newFile().toPath(); + Path certificateFile = tmpFolder.newFile().toPath(); + generatePrivateKeyAndCertificate(privateKeyFile, certificateFile); + AccessLogMock accessLogMock = new AccessLogMock(); + TestDriver driver = createSslWithProxyProtocolTestDriver(certificateFile, privateKeyFile, accessLogMock, /*mixedMode*/false); + HttpClient client = createJettyHttpClient(certificateFile); + + String proxiedRemoteAddress = "192.168.0.100"; + int proxiedRemotePort = 12345; + String proxyLocalAddress = "10.0.0.10"; + int proxyLocalPort = 23456; + V2.Tag v2Tag = new V2.Tag(V2.Tag.Command.PROXY, null, V2.Tag.Protocol.STREAM, + proxiedRemoteAddress, proxiedRemotePort, proxyLocalAddress, proxyLocalPort, null); + ContentResponse response = sendJettyClientRequest(driver, client, v2Tag); + client.stop(); + assertThat(driver.close(), is(true)); + + int clientPort = Integer.parseInt(response.getHeaders().get("Jdisc-Local-Port")); + assertNotEquals(proxyLocalPort, clientPort); + } + + private ContentResponse sendJettyClientRequest(TestDriver testDriver, HttpClient client, Object tag) throws InterruptedException, ExecutionException, TimeoutException { ContentResponse response = client.newRequest(URI.create("https://localhost:" + testDriver.server().getListenPort() + "/")) .tag(tag) .send(); assertEquals(200, response.getStatus()); + return response; } // Using Jetty's http client as Apache httpclient does not support the proxy-protocol v1/v2. @@ -963,7 +988,10 @@ public class HttpServerTest { @Override public ContentChannel handleRequest(final Request request, final ResponseHandler handler) { - return handler.handleResponse(new Response(OK)); + int port = request.getUri().getPort(); + Response response = new Response(OK); + response.headers().put("Jdisc-Local-Port", Integer.toString(port)); + return handler.handleResponse(response); } } |