@@ -18,6 +18,8 @@ import org.junit.jupiter.api.Test
18
18
import org.junit.jupiter.api.TestInstance
19
19
import org.junit.jupiter.api.*
20
20
import org.junit.jupiter.api.Assertions.*
21
+ import java.io.ByteArrayInputStream
22
+ import java.net.HttpURLConnection
21
23
import java.time.Instant
22
24
import java.util.Date
23
25
import java.util.UUID
@@ -43,12 +45,12 @@ class AnalyticsTests {
43
45
every { Instant .now() } returns Date (0 ).toInstant()
44
46
mockkStatic(UUID ::class )
45
47
every { UUID .randomUUID().toString() } returns " qwerty-qwerty-123"
46
- mockHTTPClient()
47
48
}
48
49
49
50
@BeforeEach
50
51
fun setup () {
51
52
clearPersistentStorage()
53
+ mockHTTPClient()
52
54
val config = Configuration (
53
55
writeKey = " 123" ,
54
56
application = " Test"
@@ -84,6 +86,48 @@ class AnalyticsTests {
84
86
85
87
assertEquals(exception.message?.contains(" Android" ), true )
86
88
}
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
+ }
87
131
}
88
132
89
133
@Nested
0 commit comments