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 ;
65
68
import com .google .firebase .remoteconfig .internal .ConfigGetParameterHandler ;
66
69
import com .google .firebase .remoteconfig .internal .ConfigMetadataClient ;
67
70
import com .google .firebase .remoteconfig .internal .ConfigRealtimeHandler ;
71
+ import com .google .firebase .remoteconfig .internal .ConfigRealtimeHttpClient ;
68
72
import com .google .firebase .remoteconfig .internal .Personalization ;
69
73
import java .io .ByteArrayInputStream ;
70
74
import java .io .IOException ;
@@ -142,6 +146,7 @@ public final class FirebaseRemoteConfigTest {
142
146
@ Mock private ConfigMetadataClient metadataClient ;
143
147
144
148
@ Mock private ConfigRealtimeHandler mockConfigRealtimeHandler ;
149
+ @ Mock private ConfigAutoFetch mockConfigAutoFetch ;
145
150
@ Mock private ConfigUpdateListenerRegistration mockRealtimeRegistration ;
146
151
@ Mock private HttpURLConnection mockHttpURLConnection ;
147
152
@ Mock private ConfigUpdateListener mockListener ;
@@ -167,6 +172,7 @@ public final class FirebaseRemoteConfigTest {
167
172
private FetchResponse realtimeFetchedContainerResponse ;
168
173
private ConfigContainer realtimeFetchedContainer ;
169
174
private ConfigAutoFetch configAutoFetch ;
175
+ private ConfigRealtimeHttpClient configRealtimeHttpClient ;
170
176
171
177
private FetchResponse firstFetchedContainerResponse ;
172
178
@@ -271,6 +277,14 @@ public void setUp() throws Exception {
271
277
listeners .add (mockListener );
272
278
configAutoFetch =
273
279
new ConfigAutoFetch (mockHttpURLConnection , mockFetchHandler , listeners , mockRetryListener );
280
+ configRealtimeHttpClient =
281
+ new ConfigRealtimeHttpClient (
282
+ firebaseApp ,
283
+ mockFirebaseInstallations ,
284
+ mockFetchHandler ,
285
+ context ,
286
+ "firebase" ,
287
+ listeners );
274
288
}
275
289
276
290
@ Test
@@ -1109,7 +1123,6 @@ public void realtime_client_removeListener_success() {
1109
1123
1110
1124
@ Test
1111
1125
public void realtime_stream_listen_and_retry_success () throws Exception {
1112
- when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1113
1126
when (mockHttpURLConnection .getInputStream ())
1114
1127
.thenReturn (
1115
1128
new ByteArrayInputStream (
@@ -1123,17 +1136,60 @@ public void realtime_stream_listen_and_retry_success() throws Exception {
1123
1136
1124
1137
@ Test
1125
1138
public void realtime_stream_listen_fail () throws Exception {
1126
- when (mockHttpURLConnection .getResponseCode ()).thenReturn (400 );
1127
- when (mockHttpURLConnection .getInputStream ())
1128
- .thenReturn (
1129
- new ByteArrayInputStream (
1130
- "{\\ r\\ n \\ \" latestTemplateVersionNumber\\ \" : 1\\ r\\ n}"
1131
- .getBytes (StandardCharsets .UTF_8 )));
1132
- when (mockFetchHandler .getTemplateVersionNumber ()).thenReturn (1L );
1133
- when (mockFetchHandler .fetch (0 )).thenReturn (Tasks .forResult (realtimeFetchedContainerResponse ));
1139
+ when (mockHttpURLConnection .getInputStream ()).thenThrow (IOException .class );
1134
1140
configAutoFetch .listenForNotifications ();
1135
1141
1136
- verify (mockListener ).onError (any (FirebaseRemoteConfigRealtimeUpdateStreamException .class ));
1142
+ verify (mockListener ).onError (any (FirebaseRemoteConfigRealtimeUpdateFetchException .class ));
1143
+ }
1144
+
1145
+ @ Test
1146
+ public void realtime_redirectStatusCode_noRetries () throws Exception {
1147
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1148
+ doReturn (mockHttpURLConnection ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1149
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1150
+ when (mockHttpURLConnection .getResponseCode ()).thenReturn (301 );
1151
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1152
+ verify (configRealtimeHttpClientSpy , never ()).startAutoFetch (any ());
1153
+ verify (configRealtimeHttpClientSpy , never ()).retryHTTPConnection ();
1154
+ }
1155
+
1156
+ @ Test
1157
+ public void realtime_okStatusCode_startAutofetchAndRetries () throws Exception {
1158
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1159
+ doReturn (mockHttpURLConnection ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1160
+ doReturn (mockConfigAutoFetch ).when (configRealtimeHttpClientSpy ).startAutoFetch (any ());
1161
+ doNothing ().when (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1162
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1163
+ when (mockHttpURLConnection .getResponseCode ()).thenReturn (200 );
1164
+
1165
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1166
+ verify (mockConfigAutoFetch ).listenForNotifications ();
1167
+ verify (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1168
+ }
1169
+
1170
+ @ Test
1171
+ public void realtime_badGatewayStatusCode_noAutofetchButRetries () throws Exception {
1172
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1173
+ doReturn (mockHttpURLConnection ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1174
+ doNothing ().when (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1175
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1176
+ when (mockHttpURLConnection .getResponseCode ()).thenReturn (502 );
1177
+
1178
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1179
+ verify (configRealtimeHttpClientSpy , never ()).startAutoFetch (any ());
1180
+ verify (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1181
+ }
1182
+
1183
+ @ Test
1184
+ public void realtime_exceptionThrown_noAutofetchButRetries () throws Exception {
1185
+ ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy (configRealtimeHttpClient );
1186
+ doThrow (IOException .class ).when (configRealtimeHttpClientSpy ).createRealtimeConnection ();
1187
+ doNothing ().when (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1188
+ doNothing ().when (configRealtimeHttpClientSpy ).closeRealtimeHttpStream ();
1189
+
1190
+ configRealtimeHttpClientSpy .beginRealtimeHttpStream ();
1191
+ verify (configRealtimeHttpClientSpy , never ()).startAutoFetch (any ());
1192
+ verify (configRealtimeHttpClientSpy ).retryHTTPConnection ();
1137
1193
}
1138
1194
1139
1195
@ Test
0 commit comments