Skip to content

Commit

Permalink
Add test for malformed remote config JSON parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarta committed Sep 16, 2024
1 parent e152d6e commit c4d9b19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TurboPathConfiguration>() {})
}

private fun json(): String {
return """
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not a valid path configuration

0 comments on commit c4d9b19

Please sign in to comment.