Skip to content

Commit 911c73e

Browse files
committed
HTTP Proxy test
1 parent ee9ebba commit 911c73e

File tree

1 file changed

+199
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)