Skip to content

Commit f07f4cc

Browse files
yschimkeswankjesse
andauthored
Call proxy selector on failure (#8415)
* Call proxy selector on failure --------- Co-authored-by: Jesse Wilson <[email protected]>
1 parent 4ba74c7 commit f07f4cc

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

okhttp-testing-support/src/main/kotlin/okhttp3/internal/http/RecordingProxySelector.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import okhttp3.internal.format
3030
class RecordingProxySelector : ProxySelector() {
3131
@JvmField val proxies = mutableListOf<Proxy>()
3232

33-
private val requestedUris = mutableListOf<URI>()
34-
private val failures = mutableListOf<String>()
33+
val requestedUris = mutableListOf<URI>()
34+
val failures = mutableListOf<String>()
3535

3636
override fun select(uri: URI): List<Proxy> {
3737
requestedUris.add(uri)

okhttp/src/main/kotlin/okhttp3/internal/connection/ConnectPlan.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ class ConnectPlan(
141141
success = true
142142
return ConnectResult(plan = this)
143143
} catch (e: IOException) {
144+
// If we used the ProxySelector, and got a IOException during connect, report the failure.
145+
if (route.address.proxy == null && route.proxy.type() != Proxy.Type.DIRECT) {
146+
route.address.proxySelector.connectFailed(
147+
route.address.url.toUri(),
148+
route.proxy.address(),
149+
e,
150+
)
151+
}
144152
user.connectFailed(route, null, e)
145153
return ConnectResult(plan = this, throwable = e)
146154
} finally {

okhttp/src/test/java/okhttp3/CallTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
*/
1616
package okhttp3
1717

18+
import assertk.all
1819
import assertk.assertThat
1920
import assertk.assertions.contains
2021
import assertk.assertions.containsExactly
2122
import assertk.assertions.doesNotContain
2223
import assertk.assertions.hasMessage
24+
import assertk.assertions.hasSize
25+
import assertk.assertions.index
2326
import assertk.assertions.isCloseTo
2427
import assertk.assertions.isEmpty
2528
import assertk.assertions.isEqualTo
@@ -30,6 +33,7 @@ import assertk.assertions.isNotNull
3033
import assertk.assertions.isNotSameAs
3134
import assertk.assertions.isNull
3235
import assertk.assertions.isTrue
36+
import assertk.assertions.matches
3337
import assertk.assertions.startsWith
3438
import assertk.fail
3539
import java.io.FileNotFoundException
@@ -1017,6 +1021,12 @@ open class CallTest {
10171021
executeSynchronously(request)
10181022
.assertCode(200)
10191023
.assertBody("success!")
1024+
1025+
assertThat(proxySelector.failures)
1026+
.all {
1027+
hasSize(1)
1028+
index(0).matches(".* Connect timed out".toRegex(RegexOption.IGNORE_CASE))
1029+
}
10201030
}
10211031

10221032
/** https://github.com/square/okhttp/issues/4875 */

0 commit comments

Comments
 (0)