@@ -18,6 +18,8 @@ import org.junit.jupiter.api.Test
1818import org.junit.jupiter.api.TestInstance
1919import org.junit.jupiter.api.*
2020import org.junit.jupiter.api.Assertions.*
21+ import java.io.ByteArrayInputStream
22+ import java.net.HttpURLConnection
2123import java.time.Instant
2224import java.util.Date
2325import java.util.UUID
@@ -43,12 +45,12 @@ class AnalyticsTests {
4345 every { Instant .now() } returns Date (0 ).toInstant()
4446 mockkStatic(UUID ::class )
4547 every { UUID .randomUUID().toString() } returns " qwerty-qwerty-123"
46- mockHTTPClient()
4748 }
4849
4950 @BeforeEach
5051 fun setup () {
5152 clearPersistentStorage()
53+ mockHTTPClient()
5254 val config = Configuration (
5355 writeKey = " 123" ,
5456 application = " Test"
@@ -84,6 +86,48 @@ class AnalyticsTests {
8486
8587 assertEquals(exception.message?.contains(" Android" ), true )
8688 }
89+
90+ @Test
91+ fun `analytics should respect remote apiHost` () {
92+ // need the following block in `init` to inject mock before analytics gets instantiate
93+ val settingsStream = ByteArrayInputStream (
94+ """
95+ {"integrations":{"Segment.io":{"apiKey":"1vNgUqwJeCHmqgI9S1sOm9UHCyfYqbaQ","apiHost":"remote"}},"plan":{},"edgeFunction":{}}
96+ """ .trimIndent().toByteArray()
97+ )
98+ val httpConnection: HttpURLConnection = mockk()
99+ val connection = object : Connection (httpConnection, settingsStream, null ) {}
100+ every { anyConstructed<HTTPClient >().settings(" cdn-settings.segment.com/v1" ) } returns connection
101+
102+ val config = Configuration (
103+ writeKey = " 123" ,
104+ application = " Test" ,
105+ apiHost = " local"
106+ )
107+ analytics = testAnalytics(config, testScope, testDispatcher)
108+ analytics.track(" test" )
109+ analytics.flush()
110+
111+ val apiHost = slot<String >()
112+ verify { anyConstructed<HTTPClient >().upload(capture(apiHost)) }
113+ assertEquals(" remote" , apiHost.captured)
114+ }
115+
116+ @Test
117+ fun `analytics should respect local modified apiHost if remote not presented` () {
118+ val config = Configuration (
119+ writeKey = " 123" ,
120+ application = " Test" ,
121+ apiHost = " local"
122+ )
123+ analytics = testAnalytics(config, testScope, testDispatcher)
124+ analytics.track(" test" )
125+ analytics.flush()
126+
127+ val apiHost = slot<String >()
128+ verify { anyConstructed<HTTPClient >().upload(capture(apiHost)) }
129+ assertEquals(" local" , apiHost.captured)
130+ }
87131 }
88132
89133 @Nested
0 commit comments