-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Introduce Call.Decorator #8800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Introduce Call.Decorator #8800
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f39a193
Call decorator
yschimke 70535c2
Merge branch 'master' into factory_decorator
yschimke 0e66ec3
Merge branch 'master' into factory_decorator
yschimke 8ce1306
Merge branch 'master' into factory_decorator
yschimke cd9b0bf
Merge branch 'master' into factory_decorator
yschimke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
android-test/src/androidTest/java/okhttp/android/test/AlwaysHttps.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright (C) 2025 Block, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package okhttp.android.test | ||
|
|
||
| import android.os.Build | ||
| import android.security.NetworkSecurityPolicy | ||
| import okhttp3.Call | ||
| import okhttp3.Request | ||
|
|
||
| class AlwaysHttps( | ||
| policy: Policy, | ||
| ) : Call.Decorator { | ||
| val hostPolicy: HostPolicy = policy.hostPolicy | ||
|
|
||
| override fun newCall(chain: Call.Chain): Call { | ||
| val request = chain.request | ||
|
|
||
| val updatedRequest = | ||
| if (request.url.scheme == "http" && !hostPolicy.isCleartextTrafficPermitted(request)) { | ||
| request | ||
| .newBuilder() | ||
| .url( | ||
| request.url | ||
| .newBuilder() | ||
| .scheme("https") | ||
| .build(), | ||
| ).build() | ||
| } else { | ||
| request | ||
| } | ||
|
|
||
| return chain.proceed(updatedRequest) | ||
| } | ||
|
|
||
| fun interface HostPolicy { | ||
| fun isCleartextTrafficPermitted(request: Request): Boolean | ||
| } | ||
|
|
||
| enum class Policy { | ||
| Always { | ||
| override val hostPolicy: HostPolicy | ||
| get() = HostPolicy { false } | ||
| }, | ||
| Manifest { | ||
| override val hostPolicy: HostPolicy | ||
| get() = | ||
| if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { | ||
| val networkSecurityPolicy = NetworkSecurityPolicy.getInstance() | ||
|
|
||
| if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { | ||
| HostPolicy { networkSecurityPolicy.isCleartextTrafficPermitted(it.url.host) } | ||
| } else { | ||
| HostPolicy { networkSecurityPolicy.isCleartextTrafficPermitted } | ||
| } | ||
| } else { | ||
| HostPolicy { true } | ||
| } | ||
| }, ; | ||
|
|
||
| abstract val hostPolicy: HostPolicy | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
android-test/src/androidTest/java/okhttp/android/test/AndroidCallDecoratorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright (C) 2025 Block, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package okhttp.android.test | ||
|
|
||
| import java.util.logging.Logger | ||
| import mockwebserver3.MockResponse | ||
| import mockwebserver3.MockWebServer | ||
| import mockwebserver3.junit5.StartStop | ||
| import okhttp.android.test.AlwaysHttps.Policy | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.OkHttpClientTestRule | ||
| import okhttp3.Request | ||
| import okhttp3.tls.internal.TlsUtil.localhost | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Tag | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.extension.RegisterExtension | ||
|
|
||
| @Tag("Slow") | ||
| class AndroidCallDecoratorTest { | ||
| @Suppress("RedundantVisibilityModifier") | ||
| @JvmField | ||
| @RegisterExtension | ||
| public val clientTestRule = | ||
| OkHttpClientTestRule().apply { | ||
| logger = Logger.getLogger(AndroidCallDecoratorTest::class.java.name) | ||
| } | ||
|
|
||
| private var client: OkHttpClient = | ||
| clientTestRule | ||
| .newClientBuilder() | ||
| .addCallDecorator(AlwaysHttps(Policy.Always)) | ||
| .addCallDecorator(OffMainThread) | ||
| .build() | ||
|
|
||
| @StartStop | ||
| private val server = MockWebServer() | ||
|
|
||
| private val handshakeCertificates = localhost() | ||
|
|
||
| @Test | ||
| fun testSecureRequest() { | ||
| enableTls() | ||
|
|
||
| server.enqueue(MockResponse()) | ||
|
|
||
| val request = Request.Builder().url(server.url("/")).build() | ||
|
|
||
| client.newCall(request).execute().use { | ||
| assertEquals(200, it.code) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testInsecureRequestChangedToSecure() { | ||
| enableTls() | ||
|
|
||
| server.enqueue(MockResponse()) | ||
|
|
||
| val request = | ||
| Request | ||
| .Builder() | ||
| .url( | ||
| server | ||
| .url("/") | ||
| .newBuilder() | ||
| .scheme("http") | ||
| .build(), | ||
| ).build() | ||
|
|
||
| client.newCall(request).execute().use { | ||
| assertEquals(200, it.code) | ||
| assertEquals("https", it.request.url.scheme) | ||
| } | ||
| } | ||
|
|
||
| private fun enableTls() { | ||
| client = | ||
| client | ||
| .newBuilder() | ||
| .sslSocketFactory( | ||
| handshakeCertificates.sslSocketFactory(), | ||
| handshakeCertificates.trustManager, | ||
| ).build() | ||
| server.useHttps(handshakeCertificates.sslSocketFactory()) | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
android-test/src/androidTest/java/okhttp/android/test/OffMainThread.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright (C) 2025 Block, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package okhttp.android.test | ||
|
|
||
| import android.os.Looper | ||
| import okhttp3.Call | ||
| import okhttp3.Response | ||
|
|
||
| /** | ||
| * Sample of a Decorator that will fail any call on the Android Main thread. | ||
| */ | ||
| object OffMainThread : Call.Decorator { | ||
| override fun newCall(chain: Call.Chain): Call = StrictModeCall(chain.proceed(chain.request)) | ||
|
|
||
| private class StrictModeCall( | ||
| private val delegate: Call, | ||
| ) : Call by delegate { | ||
| override fun execute(): Response { | ||
| if (Looper.getMainLooper() === Looper.myLooper()) { | ||
| throw IllegalStateException("Network on main thread") | ||
| } | ||
|
|
||
| return delegate.execute() | ||
| } | ||
|
|
||
| override fun clone(): Call = StrictModeCall(delegate.clone()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.