Skip to content

Commit a8015cd

Browse files
committed
Add CacheControl to MainActivity and OkhttpTest
- Add CacheControl.FORCE_NETWORK to MainActivity - Update CustomCertManager to return emptyArray - Create new OkhttpTest with CacheControl
1 parent 74c9c17 commit a8015cd

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package at.bitfire.cert4android
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import okhttp3.Cache
5+
import okhttp3.CacheControl
6+
import okhttp3.OkHttpClient
7+
import okhttp3.Request
8+
import okhttp3.internal.tls.OkHostnameVerifier
9+
import org.junit.Assert.assertEquals
10+
import org.junit.Test
11+
import javax.net.ssl.SSLContext
12+
13+
class OkhttpTest {
14+
15+
private val context by lazy { InstrumentationRegistry.getInstrumentation().targetContext }
16+
17+
@Test
18+
fun testAccessICloudComWithCache() {
19+
val client = buildClient()
20+
21+
// access sample URL
22+
val call = client.newCall(
23+
Request.Builder()
24+
.get()
25+
.cacheControl(CacheControl.FORCE_NETWORK) // don't retrieve from cache, the problem is storing to cache
26+
.url("https://icloud.com")
27+
.build()
28+
)
29+
call.execute().use { response ->
30+
assertEquals(200, response.code)
31+
}
32+
}
33+
34+
35+
fun buildClient(): OkHttpClient {
36+
val builder = OkHttpClient.Builder()
37+
38+
// set cert4android TrustManager and HostnameVerifier
39+
val certManager = CustomCertManager(
40+
context,
41+
trustSystemCerts = true,
42+
appInForeground = null
43+
)
44+
45+
val sslContext = SSLContext.getInstance("TLS")
46+
sslContext.init(
47+
/* km = */ null,
48+
/* tm = */ arrayOf(certManager),
49+
/* random = */ null
50+
)
51+
builder
52+
.sslSocketFactory(sslContext.socketFactory, certManager)
53+
.hostnameVerifier(certManager.HostnameVerifier(OkHostnameVerifier))
54+
55+
// add cache
56+
builder.cache(Cache(context.cacheDir, 10 * 1024 * 1024))
57+
58+
return builder.build()
59+
}
60+
61+
}

lib/src/main/java/at/bitfire/cert4android/CustomCertManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CustomCertManager @JvmOverloads constructor(
5555
throw CertificateException("Certificate chain not trusted")
5656
}
5757

58-
override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
58+
override fun getAcceptedIssuers() = emptyArray<X509Certificate>()
5959

6060

6161
/**

sample-app/src/main/java/at/bitfire/cert4android/demo/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import at.bitfire.cert4android.ThemeManager
3737
import kotlinx.coroutines.Dispatchers
3838
import kotlinx.coroutines.flow.MutableStateFlow
3939
import kotlinx.coroutines.launch
40+
import okhttp3.CacheControl
4041
import okhttp3.OkHttpClient
4142
import okhttp3.Request
4243
import okhttp3.internal.tls.OkHostnameVerifier
@@ -155,6 +156,7 @@ class MainActivity : ComponentActivity() {
155156
val call = client.newCall(
156157
Request.Builder()
157158
.get()
159+
.cacheControl(CacheControl.FORCE_NETWORK)
158160
.url(url)
159161
.build()
160162
)

0 commit comments

Comments
 (0)