Fallback to HTTP if 443 is closed too

This commit is contained in:
Kirill Kamakin
2022-12-22 18:48:46 +01:00
parent 67eb1eca8b
commit f322112405
2 changed files with 5 additions and 6 deletions

View File

@@ -8,11 +8,11 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import gq.kirmanak.mealient.data.auth.AuthRepo import gq.kirmanak.mealient.data.auth.AuthRepo
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
import gq.kirmanak.mealient.data.recipes.RecipeRepo import gq.kirmanak.mealient.data.recipes.RecipeRepo
import gq.kirmanak.mealient.datasource.NetworkError
import gq.kirmanak.mealient.logging.Logger import gq.kirmanak.mealient.logging.Logger
import gq.kirmanak.mealient.ui.OperationUiState import gq.kirmanak.mealient.ui.OperationUiState
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import javax.net.ssl.SSLHandshakeException
@HiltViewModel @HiltViewModel
class BaseURLViewModel @Inject constructor( class BaseURLViewModel @Inject constructor(
@@ -47,11 +47,11 @@ class BaseURLViewModel @Inject constructor(
val result: Result<Unit> = serverInfoRepo.tryBaseURL(url).recoverCatching { val result: Result<Unit> = serverInfoRepo.tryBaseURL(url).recoverCatching {
logger.e(it) { "checkBaseURL: trying to recover, had prefix = $hasPrefix" } logger.e(it) { "checkBaseURL: trying to recover, had prefix = $hasPrefix" }
if (hasPrefix.not() && it.cause is SSLHandshakeException) { if (hasPrefix || it is NetworkError.NotMealie) {
throw it
} else {
val unencryptedUrl = url.replace("https", "http") val unencryptedUrl = url.replace("https", "http")
serverInfoRepo.tryBaseURL(unencryptedUrl).getOrThrow() serverInfoRepo.tryBaseURL(unencryptedUrl).getOrThrow()
} else {
throw it
} }
} }

View File

@@ -17,7 +17,6 @@ import kotlinx.coroutines.test.*
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import java.io.IOException import java.io.IOException
import java.net.UnknownHostException
import javax.net.ssl.SSLHandshakeException import javax.net.ssl.SSLHandshakeException
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
@@ -126,7 +125,7 @@ class BaseURLViewModelTest : BaseUnitTest() {
@Test @Test
fun `when saving base url with no prefix and https throws non ssl expect no http`() = runTest { fun `when saving base url with no prefix and https throws non ssl expect no http`() = runTest {
coEvery { serverInfoRepo.getUrl() } returns null coEvery { serverInfoRepo.getUrl() } returns null
val err = NetworkError.MalformedUrl(UnknownHostException()) val err = NetworkError.NotMealie(IOException())
coEvery { serverInfoRepo.tryBaseURL("https://test") } returns Result.failure(err) coEvery { serverInfoRepo.tryBaseURL("https://test") } returns Result.failure(err)
subject.saveBaseUrl("test") subject.saveBaseUrl("test")
coVerify(inverse = true) { serverInfoRepo.tryBaseURL("http://test") } coVerify(inverse = true) { serverInfoRepo.tryBaseURL("http://test") }