@@ -2,6 +2,7 @@ package com.onesignal.core.internal.http
2
2
3
3
import com.onesignal.common.OneSignalUtils
4
4
import com.onesignal.core.internal.http.impl.HttpClient
5
+ import com.onesignal.core.internal.time.impl.Time
5
6
import com.onesignal.debug.LogLevel
6
7
import com.onesignal.debug.internal.logging.Logging
7
8
import com.onesignal.mocks.MockHelper
@@ -12,14 +13,15 @@ import io.kotest.matchers.shouldBe
12
13
import io.kotest.matchers.shouldNotBe
13
14
import io.kotest.matchers.types.beInstanceOf
14
15
import kotlinx.coroutines.TimeoutCancellationException
16
+ import kotlinx.coroutines.withTimeoutOrNull
15
17
import org.json.JSONObject
16
18
17
19
class Mocks {
18
20
internal val mockConfigModel = MockHelper .configModelStore()
19
21
internal val response = MockHttpConnectionFactory .MockResponse ()
20
22
internal val factory = MockHttpConnectionFactory (response)
21
23
internal val httpClient by lazy {
22
- HttpClient (factory, MockPreferencesService (), mockConfigModel)
24
+ HttpClient (factory, MockPreferencesService (), mockConfigModel, Time () )
23
25
}
24
26
}
25
27
@@ -208,4 +210,43 @@ class HttpClientTests : FunSpec({
208
210
// Then
209
211
response.retryAfterSeconds shouldBe 60
210
212
}
213
+
214
+ // If the OneSignal server ever responses with a Retry-After we want to
215
+ // make sure we delay any future requests until that delay has past. To
216
+ // be safe we assume the server's Retry-After value means all traffic
217
+ // should be delayed until then. If we wanted finer grain control we
218
+ // could work with the the backend to decide which endpoints should be
219
+ // affected.
220
+ test("ensure next request is delayed by the Retry -After value") {
221
+ // Given
222
+ val mocks = Mocks ()
223
+
224
+ val mockRetryAfterResponse = MockHttpConnectionFactory .MockResponse ()
225
+ mockRetryAfterResponse.status = 429
226
+ mockRetryAfterResponse.mockProps[" Retry-After" ] = " 1"
227
+ mockRetryAfterResponse.errorResponseBody = " {}"
228
+
229
+ val mockSuccessfulResponse = MockHttpConnectionFactory .MockResponse ()
230
+ mockSuccessfulResponse.status = 200
231
+ mockSuccessfulResponse.responseBody = " {}"
232
+
233
+ // When
234
+ mocks.factory.mockResponse = mockRetryAfterResponse
235
+ mocks.httpClient.post("URL ", JSONObject ())
236
+
237
+ mocks.factory.mockResponse = mockSuccessfulResponse
238
+ val response2 =
239
+ withTimeoutOrNull(999) {
240
+ mocks.httpClient.post("URL ", JSONObject ())
241
+ }
242
+
243
+ val response3 =
244
+ withTimeoutOrNull(100) {
245
+ mocks.httpClient.post("URL ", JSONObject ())
246
+ }
247
+
248
+ // Then
249
+ response2 shouldBe null
250
+ response3 shouldNotBe null
251
+ }
211
252
})
0 commit comments