Skip to content

Commit f9b633e

Browse files
authored
HTTP Proxy test (#8235)
1 parent ee9ebba commit f9b633e

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package okhttp3.containers
2+
3+
import assertk.assertThat
4+
import assertk.assertions.contains
5+
import assertk.assertions.isEqualTo
6+
import java.net.HttpURLConnection
7+
import java.net.Proxy
8+
import java.net.URI
9+
import javax.net.ssl.HttpsURLConnection
10+
import okhttp3.HttpUrl.Companion.toHttpUrl
11+
import okhttp3.OkHttpClient
12+
import okhttp3.Protocol
13+
import okhttp3.Request
14+
import okhttp3.containers.BasicMockServerTest.Companion.MOCKSERVER_IMAGE
15+
import okhttp3.containers.BasicMockServerTest.Companion.trustMockServer
16+
import okio.buffer
17+
import okio.source
18+
import org.junit.jupiter.api.Disabled
19+
import org.junit.jupiter.api.Test
20+
import org.mockserver.client.MockServerClient
21+
import org.mockserver.configuration.Configuration
22+
import org.mockserver.logging.MockServerLogger
23+
import org.mockserver.model.HttpRequest.request
24+
import org.mockserver.model.HttpResponse.response
25+
import org.mockserver.proxyconfiguration.ProxyConfiguration
26+
import org.mockserver.socket.tls.KeyStoreFactory
27+
import org.testcontainers.containers.MockServerContainer
28+
import org.testcontainers.junit.jupiter.Container
29+
import org.testcontainers.junit.jupiter.Testcontainers
30+
31+
@Testcontainers
32+
class BasicProxyTest {
33+
@Container
34+
val mockServer: MockServerContainer =
35+
MockServerContainer(MOCKSERVER_IMAGE)
36+
.withNetworkAliases("mockserver")
37+
38+
@Test
39+
fun testOkHttpDirect() {
40+
testRequest {
41+
val client = OkHttpClient()
42+
43+
val response =
44+
client.newCall(
45+
Request((mockServer.endpoint + "/person?name=peter").toHttpUrl()),
46+
).execute()
47+
48+
assertThat(response.body.string()).contains("Peter the person")
49+
assertThat(response.protocol).isEqualTo(Protocol.HTTP_1_1)
50+
}
51+
}
52+
53+
@Test
54+
fun testOkHttpProxied() {
55+
testRequest {
56+
it.withProxyConfiguration(ProxyConfiguration.proxyConfiguration(ProxyConfiguration.Type.HTTP, it.remoteAddress()))
57+
58+
val client =
59+
OkHttpClient.Builder()
60+
.proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress()))
61+
.build()
62+
63+
val response =
64+
client.newCall(
65+
Request((mockServer.endpoint + "/person?name=peter").toHttpUrl()),
66+
).execute()
67+
68+
assertThat(response.body.string()).contains("Peter the person")
69+
}
70+
}
71+
72+
@Test
73+
fun testOkHttpSecureDirect() {
74+
testRequest {
75+
val client =
76+
OkHttpClient.Builder()
77+
.trustMockServer()
78+
.build()
79+
80+
val response =
81+
client.newCall(
82+
Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()),
83+
).execute()
84+
85+
assertThat(response.body.string()).contains("Peter the person")
86+
assertThat(response.protocol).isEqualTo(Protocol.HTTP_2)
87+
}
88+
}
89+
90+
@Test
91+
fun testOkHttpSecureProxiedHttp1() {
92+
testRequest {
93+
val client =
94+
OkHttpClient.Builder()
95+
.trustMockServer()
96+
.proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress()))
97+
.protocols(listOf(Protocol.HTTP_1_1))
98+
.build()
99+
100+
val response =
101+
client.newCall(
102+
Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()),
103+
).execute()
104+
105+
assertThat(response.body.string()).contains("Peter the person")
106+
assertThat(response.protocol).isEqualTo(Protocol.HTTP_1_1)
107+
}
108+
}
109+
110+
@Test
111+
@Disabled("https://github.com/square/okhttp/issues/8233")
112+
fun testOkHttpSecureProxiedHttp2() {
113+
testRequest {
114+
val client =
115+
OkHttpClient.Builder()
116+
.trustMockServer()
117+
.proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress()))
118+
.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
119+
.build()
120+
121+
val response =
122+
client.newCall(
123+
Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()),
124+
).execute()
125+
126+
assertThat(response.body.string()).contains("Peter the person")
127+
assertThat(response.protocol).isEqualTo(Protocol.HTTP_2)
128+
}
129+
}
130+
131+
@Test
132+
fun testUrlConnectionDirect() {
133+
testRequest {
134+
val url = URI(mockServer.endpoint + "/person?name=peter").toURL()
135+
136+
val connection = url.openConnection() as HttpURLConnection
137+
138+
assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
139+
}
140+
}
141+
142+
@Test
143+
fun testUrlConnectionPlaintextProxied() {
144+
testRequest {
145+
val proxy =
146+
Proxy(
147+
Proxy.Type.HTTP,
148+
it.remoteAddress(),
149+
)
150+
151+
val url = URI(mockServer.endpoint + "/person?name=peter").toURL()
152+
153+
val connection = url.openConnection(proxy) as HttpURLConnection
154+
155+
assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
156+
}
157+
}
158+
159+
@Test
160+
fun testUrlConnectionSecureDirect() {
161+
val keyStoreFactory = KeyStoreFactory(Configuration.configuration(), MockServerLogger())
162+
HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory)
163+
164+
testRequest {
165+
val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL()
166+
167+
val connection = url.openConnection() as HttpURLConnection
168+
169+
assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
170+
}
171+
}
172+
173+
@Test
174+
fun testUrlConnectionSecureProxied() {
175+
val keyStoreFactory = KeyStoreFactory(Configuration.configuration(), MockServerLogger())
176+
HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory)
177+
178+
testRequest {
179+
val proxy =
180+
Proxy(
181+
Proxy.Type.HTTP,
182+
it.remoteAddress(),
183+
)
184+
185+
val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL()
186+
187+
val connection = url.openConnection(proxy) as HttpURLConnection
188+
189+
assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
190+
}
191+
}
192+
193+
private fun testRequest(function: (MockServerClient) -> Unit) {
194+
MockServerClient(mockServer.host, mockServer.serverPort).use { mockServerClient ->
195+
val request =
196+
request().withPath("/person")
197+
.withQueryStringParameter("name", "peter")
198+
199+
mockServerClient
200+
.`when`(
201+
request,
202+
)
203+
.respond(response().withBody("Peter the person!"))
204+
205+
function(mockServerClient)
206+
}
207+
}
208+
}

0 commit comments

Comments
 (0)