Loading app/autodiscovery/thunderbird/build.gradle.kts +1 −0 Original line number Diff line number Diff line Loading @@ -11,4 +11,5 @@ dependencies { implementation(libs.okhttp) testImplementation(libs.kxml2) testImplementation(libs.okhttp.mockwebserver) } app/autodiscovery/thunderbird/src/main/java/com/fsck/k9/autodiscovery/thunderbird/ThunderbirdAutoconfigFetcher.kt +12 −5 Original line number Diff line number Diff line package com.fsck.k9.autodiscovery.thunderbird import com.fsck.k9.logging.Timber import java.io.IOException import java.io.InputStream import okhttp3.HttpUrl import okhttp3.OkHttpClient Loading @@ -8,14 +10,19 @@ import okhttp3.Request class ThunderbirdAutoconfigFetcher(private val okHttpClient: OkHttpClient) { fun fetchAutoconfigFile(url: HttpUrl): InputStream? { return try { val request = Request.Builder().url(url).build() val response = okHttpClient.newCall(request).execute() return if (response.isSuccessful) { if (response.isSuccessful) { response.body?.byteStream() } else { null } } catch (e: IOException) { Timber.d(e, "Error fetching URL: %s", url) null } } } app/autodiscovery/thunderbird/src/test/java/com/fsck/k9/autodiscovery/thunderbird/ThunderbirdAutoconfigFetcherTest.kt 0 → 100644 +44 −0 Original line number Diff line number Diff line package com.fsck.k9.autodiscovery.thunderbird import assertk.assertThat import assertk.assertions.isEqualTo import assertk.assertions.isNull import kotlin.test.assertNotNull import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.OkHttpClient import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import org.junit.Test class ThunderbirdAutoconfigFetcherTest { private val fetcher = ThunderbirdAutoconfigFetcher(OkHttpClient.Builder().build()) @Test fun shouldHandleNonexistentUrl() { val nonExistentUrl = "https://autoconfig.domain.invalid/mail/config-v1.1.xml?emailaddress=test%40domain.example".toHttpUrl() val inputStream = fetcher.fetchAutoconfigFile(nonExistentUrl) assertThat(inputStream).isNull() } @Test fun shouldHandleEmptyResponse() { val server = MockWebServer().apply { this.enqueue( MockResponse() .setBody("") .setResponseCode(204), ) start() } val url = server.url("/empty/") val inputStream = fetcher.fetchAutoconfigFile(url) assertNotNull(inputStream) { inputStream -> assertThat(inputStream.available()).isEqualTo(0) } } } Loading
app/autodiscovery/thunderbird/build.gradle.kts +1 −0 Original line number Diff line number Diff line Loading @@ -11,4 +11,5 @@ dependencies { implementation(libs.okhttp) testImplementation(libs.kxml2) testImplementation(libs.okhttp.mockwebserver) }
app/autodiscovery/thunderbird/src/main/java/com/fsck/k9/autodiscovery/thunderbird/ThunderbirdAutoconfigFetcher.kt +12 −5 Original line number Diff line number Diff line package com.fsck.k9.autodiscovery.thunderbird import com.fsck.k9.logging.Timber import java.io.IOException import java.io.InputStream import okhttp3.HttpUrl import okhttp3.OkHttpClient Loading @@ -8,14 +10,19 @@ import okhttp3.Request class ThunderbirdAutoconfigFetcher(private val okHttpClient: OkHttpClient) { fun fetchAutoconfigFile(url: HttpUrl): InputStream? { return try { val request = Request.Builder().url(url).build() val response = okHttpClient.newCall(request).execute() return if (response.isSuccessful) { if (response.isSuccessful) { response.body?.byteStream() } else { null } } catch (e: IOException) { Timber.d(e, "Error fetching URL: %s", url) null } } }
app/autodiscovery/thunderbird/src/test/java/com/fsck/k9/autodiscovery/thunderbird/ThunderbirdAutoconfigFetcherTest.kt 0 → 100644 +44 −0 Original line number Diff line number Diff line package com.fsck.k9.autodiscovery.thunderbird import assertk.assertThat import assertk.assertions.isEqualTo import assertk.assertions.isNull import kotlin.test.assertNotNull import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.OkHttpClient import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import org.junit.Test class ThunderbirdAutoconfigFetcherTest { private val fetcher = ThunderbirdAutoconfigFetcher(OkHttpClient.Builder().build()) @Test fun shouldHandleNonexistentUrl() { val nonExistentUrl = "https://autoconfig.domain.invalid/mail/config-v1.1.xml?emailaddress=test%40domain.example".toHttpUrl() val inputStream = fetcher.fetchAutoconfigFile(nonExistentUrl) assertThat(inputStream).isNull() } @Test fun shouldHandleEmptyResponse() { val server = MockWebServer().apply { this.enqueue( MockResponse() .setBody("") .setResponseCode(204), ) start() } val url = server.url("/empty/") val inputStream = fetcher.fetchAutoconfigFile(url) assertNotNull(inputStream) { inputStream -> assertThat(inputStream.available()).isEqualTo(0) } } }