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