From 911c73ea529979ea67dbcfa0036f73eb9e66c9b6 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 3 Feb 2024 13:06:57 +0000 Subject: [PATCH 1/2] HTTP Proxy test --- .../java/okhttp3/containers/BasicProxyTest.kt | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt diff --git a/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt b/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt new file mode 100644 index 000000000000..994efa59e0d3 --- /dev/null +++ b/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt @@ -0,0 +1,199 @@ +package okhttp3.containers + +import assertk.assertThat +import assertk.assertions.contains +import assertk.assertions.isEqualTo +import java.net.HttpURLConnection +import java.net.Proxy +import java.net.URI +import javax.net.ssl.HttpsURLConnection +import okhttp3.HttpUrl.Companion.toHttpUrl +import okhttp3.OkHttpClient +import okhttp3.Protocol +import okhttp3.Request +import okhttp3.containers.BasicMockServerTest.Companion.MOCKSERVER_IMAGE +import okhttp3.containers.BasicMockServerTest.Companion.trustMockServer +import okio.buffer +import okio.source +import org.junit.jupiter.api.Test +import org.mockserver.client.MockServerClient +import org.mockserver.configuration.Configuration +import org.mockserver.logging.MockServerLogger +import org.mockserver.model.HttpRequest.request +import org.mockserver.model.HttpResponse.response +import org.mockserver.proxyconfiguration.ProxyConfiguration +import org.mockserver.socket.tls.KeyStoreFactory +import org.testcontainers.containers.MockServerContainer +import org.testcontainers.junit.jupiter.Container +import org.testcontainers.junit.jupiter.Testcontainers + +@Testcontainers +class BasicProxyTest { + @Container + val mockServer: MockServerContainer = + MockServerContainer(MOCKSERVER_IMAGE) + .withNetworkAliases("mockserver") + + @Test + fun testOkHttpDirect() { + testRequest { + val client = OkHttpClient() + + val response = + client.newCall( + Request((mockServer.endpoint + "/person?name=peter").toHttpUrl()), + ).execute() + + assertThat(response.body.string()).contains("Peter the person") + assertThat(response.protocol).isEqualTo(Protocol.HTTP_1_1) + } + } + + @Test + fun testOkHttpProxied() { + testRequest { + it.withProxyConfiguration(ProxyConfiguration.proxyConfiguration(ProxyConfiguration.Type.HTTP, it.remoteAddress())) + + val client = OkHttpClient.Builder() + .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) + .build() + + val response = + client.newCall( + Request((mockServer.endpoint + "/person?name=peter").toHttpUrl()), + ).execute() + + assertThat(response.body.string()).contains("Peter the person") + } + } + + @Test + fun testOkHttpSecureDirect() { + testRequest { + val client = OkHttpClient.Builder() + .trustMockServer() + .build() + + val response = + client.newCall( + Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()), + ).execute() + + assertThat(response.body.string()).contains("Peter the person") + assertThat(response.protocol).isEqualTo(Protocol.HTTP_2) + } + } + + @Test + fun testOkHttpSecureProxiedHttp1() { + testRequest { + val client = OkHttpClient.Builder() + .trustMockServer() + .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) + .protocols(listOf(Protocol.HTTP_1_1)) + .build() + + val response = + client.newCall( + Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()), + ).execute() + + assertThat(response.body.string()).contains("Peter the person") + assertThat(response.protocol).isEqualTo(Protocol.HTTP_1_1) + } + } + + @Test + fun testOkHttpSecureProxiedHttp2() { + testRequest { + val client = OkHttpClient.Builder() + .trustMockServer() + .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) + .protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1)) + .build() + + val response = + client.newCall( + Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()), + ).execute() + + assertThat(response.body.string()).contains("Peter the person") + assertThat(response.protocol).isEqualTo(Protocol.HTTP_2) + } + } + + @Test + fun testUrlConnectionDirect() { + testRequest { + val url = URI(mockServer.endpoint + "/person?name=peter").toURL() + + val connection = url.openConnection() as HttpURLConnection + + assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person") + } + } + + @Test + fun testUrlConnectionPlaintextProxied() { + testRequest { + val proxy = Proxy( + Proxy.Type.HTTP, + it.remoteAddress() + ) + + val url = URI(mockServer.endpoint + "/person?name=peter").toURL() + + val connection = url.openConnection(proxy) as HttpURLConnection + + assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person") + } + } + + @Test + fun testUrlConnectionSecureDirect() { + val keyStoreFactory = KeyStoreFactory(Configuration.configuration(), MockServerLogger()) + HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory) + + testRequest { + val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL() + + val connection = url.openConnection() as HttpURLConnection + + assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person") + } + } + + @Test + fun testUrlConnectionSecureProxied() { + val keyStoreFactory = KeyStoreFactory(Configuration.configuration(), MockServerLogger()) + HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory) + + testRequest { + val proxy = Proxy( + Proxy.Type.HTTP, + it.remoteAddress() + ) + + val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL() + + val connection = url.openConnection(proxy) as HttpURLConnection + + assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person") + } + } + + private fun testRequest(function: (MockServerClient) -> Unit) { + MockServerClient(mockServer.host, mockServer.serverPort).use { mockServerClient -> + val request = request().withPath("/person") + .withQueryStringParameter("name", "peter") + + mockServerClient + .`when`( + request, + ) + .respond(response().withBody("Peter the person!")) + + function(mockServerClient) + } + } +} From 9ee5c7cc9d830fe72b980b6811f8c7e75a91a7e7 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sat, 3 Feb 2024 13:08:38 +0000 Subject: [PATCH 2/2] HTTP Proxy test --- .../java/okhttp3/containers/BasicProxyTest.kt | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt b/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt index 994efa59e0d3..01f2b5e78690 100644 --- a/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt +++ b/container-tests/src/test/java/okhttp3/containers/BasicProxyTest.kt @@ -15,6 +15,7 @@ import okhttp3.containers.BasicMockServerTest.Companion.MOCKSERVER_IMAGE import okhttp3.containers.BasicMockServerTest.Companion.trustMockServer import okio.buffer import okio.source +import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.mockserver.client.MockServerClient import org.mockserver.configuration.Configuration @@ -54,9 +55,10 @@ class BasicProxyTest { testRequest { it.withProxyConfiguration(ProxyConfiguration.proxyConfiguration(ProxyConfiguration.Type.HTTP, it.remoteAddress())) - val client = OkHttpClient.Builder() - .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) - .build() + val client = + OkHttpClient.Builder() + .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) + .build() val response = client.newCall( @@ -70,9 +72,10 @@ class BasicProxyTest { @Test fun testOkHttpSecureDirect() { testRequest { - val client = OkHttpClient.Builder() - .trustMockServer() - .build() + val client = + OkHttpClient.Builder() + .trustMockServer() + .build() val response = client.newCall( @@ -87,11 +90,12 @@ class BasicProxyTest { @Test fun testOkHttpSecureProxiedHttp1() { testRequest { - val client = OkHttpClient.Builder() - .trustMockServer() - .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) - .protocols(listOf(Protocol.HTTP_1_1)) - .build() + val client = + OkHttpClient.Builder() + .trustMockServer() + .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) + .protocols(listOf(Protocol.HTTP_1_1)) + .build() val response = client.newCall( @@ -104,13 +108,15 @@ class BasicProxyTest { } @Test + @Disabled("https://github.com/square/okhttp/issues/8233") fun testOkHttpSecureProxiedHttp2() { testRequest { - val client = OkHttpClient.Builder() - .trustMockServer() - .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) - .protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1)) - .build() + val client = + OkHttpClient.Builder() + .trustMockServer() + .proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress())) + .protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1)) + .build() val response = client.newCall( @@ -136,10 +142,11 @@ class BasicProxyTest { @Test fun testUrlConnectionPlaintextProxied() { testRequest { - val proxy = Proxy( - Proxy.Type.HTTP, - it.remoteAddress() - ) + val proxy = + Proxy( + Proxy.Type.HTTP, + it.remoteAddress(), + ) val url = URI(mockServer.endpoint + "/person?name=peter").toURL() @@ -169,10 +176,11 @@ class BasicProxyTest { HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory) testRequest { - val proxy = Proxy( - Proxy.Type.HTTP, - it.remoteAddress() - ) + val proxy = + Proxy( + Proxy.Type.HTTP, + it.remoteAddress(), + ) val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL() @@ -184,8 +192,9 @@ class BasicProxyTest { private fun testRequest(function: (MockServerClient) -> Unit) { MockServerClient(mockServer.host, mockServer.serverPort).use { mockServerClient -> - val request = request().withPath("/person") - .withQueryStringParameter("name", "peter") + val request = + request().withPath("/person") + .withQueryStringParameter("name", "peter") mockServerClient .`when`(