30
30
import static org .mockito .ArgumentMatchers .eq ;
31
31
import static org .mockito .Matchers .any ;
32
32
import static org .mockito .Mockito .doAnswer ;
33
+ import static org .mockito .Mockito .doNothing ;
34
+ import static org .mockito .Mockito .doReturn ;
33
35
import static org .mockito .Mockito .doThrow ;
34
36
import static org .mockito .Mockito .never ;
37
+ import static org .mockito .Mockito .spy ;
35
38
import static org .mockito .Mockito .times ;
36
39
import static org .mockito .Mockito .verify ;
37
40
import static org .mockito .Mockito .when ;
@@ -141,7 +144,7 @@ public final class FirebaseRemoteConfigTest {
141
144
@ Mock private ConfigMetadataClient metadataClient ;
142
145
143
146
@ Mock private ConfigRealtimeHandler mockConfigRealtimeHandler ;
144
- @ Mock private ConfigRealtimeHttpClient mockConfigRealtimeHttpClient ;
147
+ @ Mock private ConfigAutoFetch mockConfigAutoFetch ;
145
148
@ Mock private ConfigUpdateListenerRegistration mockRealtimeRegistration ;
146
149
@ Mock private HttpURLConnection mockHttpURLConnection ;
147
150
@ Mock private ConfigUpdateListener mockListener ;
@@ -167,6 +170,7 @@ public final class FirebaseRemoteConfigTest {
167
170
private FetchResponse realtimeFetchedContainerResponse ;
168
171
private ConfigContainer realtimeFetchedContainer ;
169
172
private ConfigAutoFetch configAutoFetch ;
173
+ private ConfigRealtimeHttpClient configRealtimeHttpClient ;
170
174
171
175
private FetchResponse firstFetchedContainerResponse ;
172
176
@@ -271,6 +275,14 @@ public void setUp() throws Exception {
271
275
listeners .add (mockListener );
272
276
configAutoFetch =
273
277
new ConfigAutoFetch (mockHttpURLConnection , mockFetchHandler , listeners , mockRetryListener );
278
+ configRealtimeHttpClient =
279
+ new ConfigRealtimeHttpClient (
280
+ firebaseApp ,
281
+ mockFirebaseInstallations ,
282
+ mockFetchHandler ,
283
+ context ,
284
+ "firebase" ,
285
+ listeners );
274
286
}
275
287
276
288
@ Test
@@ -1206,11 +1218,53 @@ public void realtime_stream_autofetch_failure() {
1206
1218
}
1207
1219
1208
1220
@ Test
1209
- public void realtime_checkStatusCode_beforeRetryStream () throws Exception {
1210
- when (mockConfigRealtimeHttpClient .createRealtimeConnection ()).thenReturn (mockHttpURLConnection );
1221
+ public void realtime_redirectStatusCode_noRetries () throws Exception {
1222
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1223
+ doReturn (mockHttpURLConnection ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1224
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1225
+ when (mockHttpURLConnection .getResponseCode ()).thenReturn (301 );
1226
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1227
+ verify (configRealtimeHttpClientSpy , never ()).startAutoFetch (any ());
1228
+ verify (configRealtimeHttpClientSpy , never ()).retryHTTPConnection ();
1229
+ }
1230
+
1231
+ @ Test
1232
+ public void realtime_okStatusCode_startAutofetchAndRetries () throws Exception {
1233
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1234
+ doReturn (mockHttpURLConnection ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1235
+ doReturn (mockConfigAutoFetch ).when (configRealtimeHttpClientSpy ).startAutoFetch (any ());
1236
+ doNothing ().when (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1237
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1238
+ when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1239
+
1240
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1241
+ verify (mockConfigAutoFetch ).listenForNotifications ();
1242
+ verify (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1243
+ }
1244
+
1245
+ @ Test
1246
+ public void realtime_badGatewayStatusCode_noAutofetchButRetries () throws Exception {
1247
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1248
+ doReturn (mockHttpURLConnection ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1249
+ doNothing ().when (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1250
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1211
1251
when (mockHttpURLConnection .getResponseCode ()).thenReturn (502 );
1212
- mockConfigRealtimeHttpClient .beginRealtimeHttpStream ();
1213
- verify (mockConfigRealtimeHttpClient , never ()).retryHTTPConnection ();
1252
+
1253
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1254
+ verify (configRealtimeHttpClientSpy , never ()).startAutoFetch (any ());
1255
+ verify (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1256
+ }
1257
+
1258
+ @ Test
1259
+ public void realtime_exceptionThrown_noAutofetchButRetries () throws Exception {
1260
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1261
+ doThrow (IOException .class ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1262
+ doNothing ().when (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1263
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1264
+
1265
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1266
+ verify (configRealtimeHttpClientSpy , never ()).startAutoFetch (any ());
1267
+ verify (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1214
1268
}
1215
1269
1216
1270
private static void loadCacheWithConfig (
0 commit comments