Skip to content

Commit c4d9b19

Browse files
committed
Add test for malformed remote config JSON parsing
1 parent e152d6e commit c4d9b19

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt

+19-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import android.os.Build
55
import androidx.test.core.app.ApplicationProvider
66
import dev.hotwire.turbo.BaseRepositoryTest
77
import dev.hotwire.turbo.http.TurboHttpClient
8-
import dev.hotwire.turbo.util.toObject
9-
import com.google.gson.reflect.TypeToken
108
import kotlinx.coroutines.Dispatchers
119
import kotlinx.coroutines.ExperimentalCoroutinesApi
1210
import kotlinx.coroutines.launch
@@ -39,38 +37,49 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() {
3937
val json = repository.getRemoteConfiguration(baseUrl())
4038
assertThat(json).isNotNull()
4139

42-
val config = load(json)
40+
val config = repository.parseFromJson(json!!)
4341
assertThat(config?.rules?.size).isEqualTo(2)
4442
}
4543
}
4644
}
4745

46+
@Test
47+
fun getMalformedRemoteConfiguration() {
48+
enqueueResponse("test-configuration-malformed.json")
49+
50+
runBlocking {
51+
launch(Dispatchers.Main) {
52+
val json = repository.getRemoteConfiguration(baseUrl())
53+
assertThat(json).isNotNull()
54+
55+
val config = repository.parseFromJson(json!!)
56+
assertThat(config).isNull()
57+
}
58+
}
59+
}
60+
4861
@Test
4962
fun getBundledAssetConfiguration() {
5063
val json = repository.getBundledConfiguration(context, "json/test-configuration.json")
5164
assertThat(json).isNotNull()
5265

53-
val config = load(json)
66+
val config = repository.parseFromJson(json)
5467
assertThat(config?.rules?.size).isEqualTo(10)
5568
}
5669

5770
@Test
5871
fun getCachedConfiguration() {
5972
val url = "https://turbo.hotwired.dev/demo/configurations/android-v1.json"
60-
val config = requireNotNull(load(json()))
73+
val config = requireNotNull(repository.parseFromJson(json()))
6174
repository.cacheConfigurationForUrl(context, url, config)
6275

6376
val json = repository.getCachedConfigurationForUrl(context, url)
6477
assertThat(json).isNotNull()
6578

66-
val cachedConfig = load(json)
79+
val cachedConfig = repository.parseFromJson(json!!)
6780
assertThat(cachedConfig?.rules?.size).isEqualTo(1)
6881
}
6982

70-
private fun load(json: String?): TurboPathConfiguration? {
71-
return json?.toObject(object : TypeToken<TurboPathConfiguration>() {})
72-
}
73-
7483
private fun json(): String {
7584
return """
7685
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Not a valid path configuration

0 commit comments

Comments
 (0)