okhttp
201 строка · 5.9 Кб
1/*
2* Copyright (C) 2024 Square, Inc.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16package okhttp3.containers
17
18import assertk.assertThat
19import assertk.assertions.contains
20import assertk.assertions.isEqualTo
21import java.net.HttpURLConnection
22import java.net.Proxy
23import java.net.URI
24import javax.net.ssl.HttpsURLConnection
25import okhttp3.HttpUrl.Companion.toHttpUrl
26import okhttp3.OkHttpClient
27import okhttp3.Protocol
28import okhttp3.Request
29import okhttp3.containers.BasicMockServerTest.Companion.MOCKSERVER_IMAGE
30import okhttp3.containers.BasicMockServerTest.Companion.trustMockServer
31import okio.buffer
32import okio.source
33import org.junit.jupiter.api.Test
34import org.mockserver.client.MockServerClient
35import org.mockserver.configuration.Configuration
36import org.mockserver.logging.MockServerLogger
37import org.mockserver.model.HttpRequest.request
38import org.mockserver.model.HttpResponse.response
39import org.mockserver.proxyconfiguration.ProxyConfiguration
40import org.mockserver.socket.tls.KeyStoreFactory
41import org.testcontainers.containers.MockServerContainer
42import org.testcontainers.junit.jupiter.Container
43import org.testcontainers.junit.jupiter.Testcontainers
44
45@Testcontainers
46class BasicProxyTest {
47@Container
48val mockServer: MockServerContainer =
49MockServerContainer(MOCKSERVER_IMAGE)
50.withNetworkAliases("mockserver")
51
52@Test
53fun testOkHttpDirect() {
54testRequest {
55val client = OkHttpClient()
56
57val response =
58client.newCall(
59Request((mockServer.endpoint + "/person?name=peter").toHttpUrl()),
60).execute()
61
62assertThat(response.body.string()).contains("Peter the person")
63assertThat(response.protocol).isEqualTo(Protocol.HTTP_1_1)
64}
65}
66
67@Test
68fun testOkHttpProxied() {
69testRequest {
70it.withProxyConfiguration(ProxyConfiguration.proxyConfiguration(ProxyConfiguration.Type.HTTP, it.remoteAddress()))
71
72val client =
73OkHttpClient.Builder()
74.proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress()))
75.build()
76
77val response =
78client.newCall(
79Request((mockServer.endpoint + "/person?name=peter").toHttpUrl()),
80).execute()
81
82assertThat(response.body.string()).contains("Peter the person")
83}
84}
85
86@Test
87fun testOkHttpSecureDirect() {
88testRequest {
89val client =
90OkHttpClient.Builder()
91.trustMockServer()
92.build()
93
94val response =
95client.newCall(
96Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()),
97).execute()
98
99assertThat(response.body.string()).contains("Peter the person")
100assertThat(response.protocol).isEqualTo(Protocol.HTTP_2)
101}
102}
103
104@Test
105fun testOkHttpSecureProxiedHttp1() {
106testRequest {
107val client =
108OkHttpClient.Builder()
109.trustMockServer()
110.proxy(Proxy(Proxy.Type.HTTP, it.remoteAddress()))
111.protocols(listOf(Protocol.HTTP_1_1))
112.build()
113
114val response =
115client.newCall(
116Request((mockServer.secureEndpoint + "/person?name=peter").toHttpUrl()),
117).execute()
118
119assertThat(response.body.string()).contains("Peter the person")
120assertThat(response.protocol).isEqualTo(Protocol.HTTP_1_1)
121}
122}
123
124@Test
125fun testUrlConnectionDirect() {
126testRequest {
127val url = URI(mockServer.endpoint + "/person?name=peter").toURL()
128
129val connection = url.openConnection() as HttpURLConnection
130
131assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
132}
133}
134
135@Test
136fun testUrlConnectionPlaintextProxied() {
137testRequest {
138val proxy =
139Proxy(
140Proxy.Type.HTTP,
141it.remoteAddress(),
142)
143
144val url = URI(mockServer.endpoint + "/person?name=peter").toURL()
145
146val connection = url.openConnection(proxy) as HttpURLConnection
147
148assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
149}
150}
151
152@Test
153fun testUrlConnectionSecureDirect() {
154val keyStoreFactory = KeyStoreFactory(Configuration.configuration(), MockServerLogger())
155HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory)
156
157testRequest {
158val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL()
159
160val connection = url.openConnection() as HttpURLConnection
161
162assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
163}
164}
165
166@Test
167fun testUrlConnectionSecureProxied() {
168val keyStoreFactory = KeyStoreFactory(Configuration.configuration(), MockServerLogger())
169HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().socketFactory)
170
171testRequest {
172val proxy =
173Proxy(
174Proxy.Type.HTTP,
175it.remoteAddress(),
176)
177
178val url = URI(mockServer.secureEndpoint + "/person?name=peter").toURL()
179
180val connection = url.openConnection(proxy) as HttpURLConnection
181
182assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
183}
184}
185
186private fun testRequest(function: (MockServerClient) -> Unit) {
187MockServerClient(mockServer.host, mockServer.serverPort).use { mockServerClient ->
188val request =
189request().withPath("/person")
190.withQueryStringParameter("name", "peter")
191
192mockServerClient
193.`when`(
194request,
195)
196.respond(response().withBody("Peter the person!"))
197
198function(mockServerClient)
199}
200}
201}
202