From c4d9b1985457ef0996004f1cda06011b38acaa31 Mon Sep 17 00:00:00 2001 From: Milan Barta Date: Mon, 16 Sep 2024 09:47:19 +0200 Subject: [PATCH] Add test for malformed remote config JSON parsing --- .../TurboPathConfigurationRepositoryTest.kt | 29 ++++++++++++------- .../test-configuration-malformed.json | 1 + 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 turbo/src/test/resources/http-responses/test-configuration-malformed.json diff --git a/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt b/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt index 553eb063..e05777df 100644 --- a/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt +++ b/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt @@ -5,8 +5,6 @@ import android.os.Build import androidx.test.core.app.ApplicationProvider import dev.hotwire.turbo.BaseRepositoryTest import dev.hotwire.turbo.http.TurboHttpClient -import dev.hotwire.turbo.util.toObject -import com.google.gson.reflect.TypeToken import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch @@ -39,38 +37,49 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() { val json = repository.getRemoteConfiguration(baseUrl()) assertThat(json).isNotNull() - val config = load(json) + val config = repository.parseFromJson(json!!) assertThat(config?.rules?.size).isEqualTo(2) } } } + @Test + fun getMalformedRemoteConfiguration() { + enqueueResponse("test-configuration-malformed.json") + + runBlocking { + launch(Dispatchers.Main) { + val json = repository.getRemoteConfiguration(baseUrl()) + assertThat(json).isNotNull() + + val config = repository.parseFromJson(json!!) + assertThat(config).isNull() + } + } + } + @Test fun getBundledAssetConfiguration() { val json = repository.getBundledConfiguration(context, "json/test-configuration.json") assertThat(json).isNotNull() - val config = load(json) + val config = repository.parseFromJson(json) assertThat(config?.rules?.size).isEqualTo(10) } @Test fun getCachedConfiguration() { val url = "https://turbo.hotwired.dev/demo/configurations/android-v1.json" - val config = requireNotNull(load(json())) + val config = requireNotNull(repository.parseFromJson(json())) repository.cacheConfigurationForUrl(context, url, config) val json = repository.getCachedConfigurationForUrl(context, url) assertThat(json).isNotNull() - val cachedConfig = load(json) + val cachedConfig = repository.parseFromJson(json!!) assertThat(cachedConfig?.rules?.size).isEqualTo(1) } - private fun load(json: String?): TurboPathConfiguration? { - return json?.toObject(object : TypeToken() {}) - } - private fun json(): String { return """ { diff --git a/turbo/src/test/resources/http-responses/test-configuration-malformed.json b/turbo/src/test/resources/http-responses/test-configuration-malformed.json new file mode 100644 index 00000000..7a0581c3 --- /dev/null +++ b/turbo/src/test/resources/http-responses/test-configuration-malformed.json @@ -0,0 +1 @@ +Not a valid path configuration \ No newline at end of file