Skip to content

Commit

Permalink
Fallback to the defaultSettings if cdn cannot be reached (#231)
Browse files Browse the repository at this point in the history
* fallback to default settings, not null

* fallback to default settings, not null

* update tests

* test: remove commented out code after confirming that the current test logic is correct.

---------

Co-authored-by: Didier Garcia <digarcia@twilio.com>
  • Loading branch information
niallzato and didiergarcia authored Oct 3, 2024
1 parent 681920d commit d37d2d3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ internal fun Analytics.fetchSettings(
it["writekey"] = writeKey
it["message"] = "Error retrieving settings"
}
null
configuration.defaultSettings
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ class SettingsTests {

// no settings available, should not be called
analytics.add(mockPlugin)
verify (exactly = 0){
mockPlugin.update(any(), any())
}


// load settings
mockHTTPClient()
Expand All @@ -104,7 +102,7 @@ class SettingsTests {
// load settings again
mockHTTPClient()
analytics.checkSettings()
verify (exactly = 1) {
verify (exactly = 2) {
mockPlugin.update(any(), Plugin.UpdateType.Refresh)
}
}
Expand Down Expand Up @@ -232,95 +230,102 @@ class SettingsTests {

@Test
fun `fetchSettings returns null when Settings string is invalid`() {
val emptySettings = analytics.fetchSettings("emptySettingsObject", "cdn-settings.segment.com/v1")
// Null on invalid JSON
mockHTTPClient("")
var settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("hello")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("#! /bin/sh")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("<!DOCTYPE html>")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("true")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("[]")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("}{")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("{{{{}}}}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null on invalid JSON
mockHTTPClient("{null:\"bar\"}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)
}

@Test
fun `fetchSettings returns null when parameters are invalid`() {
val emptySettings = analytics.fetchSettings("emptySettingsObject", "cdn-settings.segment.com/v1")
mockHTTPClient("{\"integrations\":{}, \"plan\":{}, \"edgeFunction\": {}, \"middlewareSettings\": {}}")

// empty host
var settings = analytics.fetchSettings("foo", "")
assertNull(settings)
assertEquals(emptySettings, settings)

// not a host name
settings = analytics.fetchSettings("foo", "http://blah")
assertNull(settings)
assertEquals(emptySettings, settings)

// emoji
settings = analytics.fetchSettings("foo", "😃")
assertNull(settings)
assertEquals(emptySettings, settings)
}

@Test
fun `fetchSettings returns null when Settings string is null for known properties`() {
// Null if integrations is null
mockHTTPClient("{\"integrations\":null}")
var settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)

// Null if plan is null
mockHTTPClient("{\"plan\":null}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)

// Null if edgeFunction is null
mockHTTPClient("{\"edgeFunction\":null}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)

// Null if middlewareSettings is null
mockHTTPClient("{\"middlewareSettings\":null}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertTrue(settings?.integrations?.isEmpty() ?: true, "Integrations should be empty")
assertTrue(settings?.plan?.isEmpty() ?: true, "Plan should be empty")
assertTrue(settings?.edgeFunction?.isEmpty() ?: true, "EdgeFunction should be empty")
assertTrue(settings?.middlewareSettings?.isEmpty() ?: true, "MiddlewareSettings should be empty")
assertTrue(settings?.metrics?.isEmpty() ?: true, "Metrics should be empty")
assertTrue(settings?.consentSettings?.isEmpty() ?: true, "ConsentSettings should be empty")

// // Null if plan is null
// mockHTTPClient("{\"plan\":null}")
// settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
// assertNull(settings)
//
// // Null if edgeFunction is null
// mockHTTPClient("{\"edgeFunction\":null}")
// settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
// assertNull(settings)
//
// // Null if middlewareSettings is null
// mockHTTPClient("{\"middlewareSettings\":null}")
// settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
// assertNull(settings)
}

@Test
fun `known Settings property types must match json type`() {

val emptySettings = analytics.fetchSettings("emptySettingsObject", "cdn-settings.segment.com/v1")
// integrations must be a JSON object
mockHTTPClient("{\"integrations\":{}}")
var settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
Expand All @@ -329,21 +334,21 @@ class SettingsTests {
// Null if integrations is a number
mockHTTPClient("{\"integrations\":123}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null if integrations is a string
mockHTTPClient("{\"integrations\":\"foo\"}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null if integrations is an array
mockHTTPClient("{\"integrations\":[\"foo\"]}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)

// Null if integrations is an emoji (UTF-8 string)
mockHTTPClient("{\"integrations\": 😃}")
settings = analytics.fetchSettings("foo", "cdn-settings.segment.com/v1")
assertNull(settings)
assertEquals(emptySettings, settings)
}
}

0 comments on commit d37d2d3

Please sign in to comment.