33
33
import com .google .api .client .testing .http .MockHttpTransport ;
34
34
import com .google .api .client .testing .http .MockLowLevelHttpResponse ;
35
35
import com .google .auth .oauth2 .GoogleCredentials ;
36
+ import com .google .common .base .Supplier ;
36
37
import com .google .common .collect .ImmutableList ;
37
38
import com .google .common .collect .ImmutableMap ;
38
39
import com .google .common .collect .Iterables ;
@@ -550,14 +551,7 @@ public void call(FirebaseAuth auth) throws Exception {
550
551
.build ();
551
552
552
553
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse ();
553
- MockHttpTransport transport = new MockHttpTransport .Builder ()
554
- .setLowLevelHttpResponse (response )
555
- .build ();
556
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
557
- .setCredentials (credentials )
558
- .setProjectId ("test-project-id" )
559
- .setHttpTransport (transport )
560
- .build ());
554
+ FirebaseAuth auth = getRetryDisabledAuth (response );
561
555
562
556
// Test for common HTTP error codes
563
557
for (int code : ImmutableList .of (302 , 400 , 401 , 404 , 500 )) {
@@ -566,7 +560,7 @@ public void call(FirebaseAuth auth) throws Exception {
566
560
response .setContent ("{}" );
567
561
response .setStatusCode (code );
568
562
try {
569
- operation .call (FirebaseAuth . getInstance () );
563
+ operation .call (auth );
570
564
fail ("No error thrown for HTTP error: " + code );
571
565
} catch (ExecutionException e ) {
572
566
assertTrue (e .getCause () instanceof FirebaseAuthException );
@@ -584,7 +578,7 @@ public void call(FirebaseAuth auth) throws Exception {
584
578
response .setContent ("{\" error\" : {\" message\" : \" USER_NOT_FOUND\" }}" );
585
579
response .setStatusCode (500 );
586
580
try {
587
- operation .call (FirebaseAuth . getInstance () );
581
+ operation .call (auth );
588
582
fail ("No error thrown for HTTP error" );
589
583
} catch (ExecutionException e ) {
590
584
assertTrue (e .getCause ().toString (), e .getCause () instanceof FirebaseAuthException );
@@ -615,16 +609,9 @@ public void testGetUserUnexpectedHttpError() throws Exception {
615
609
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse ();
616
610
response .setContent ("{\" not\" json}" );
617
611
response .setStatusCode (500 );
618
- MockHttpTransport transport = new MockHttpTransport .Builder ()
619
- .setLowLevelHttpResponse (response )
620
- .build ();
621
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
622
- .setCredentials (credentials )
623
- .setProjectId ("test-project-id" )
624
- .setHttpTransport (transport )
625
- .build ());
612
+ FirebaseAuth auth = getRetryDisabledAuth (response );
626
613
try {
627
- FirebaseAuth . getInstance () .getUserAsync ("testuser" ).get ();
614
+ auth .getUserAsync ("testuser" ).get ();
628
615
fail ("No error thrown for JSON error" );
629
616
} catch (ExecutionException e ) {
630
617
assertTrue (e .getCause () instanceof FirebaseAuthException );
@@ -1173,15 +1160,10 @@ public void testGenerateSignInWithEmailLinkWithSettings() throws Exception {
1173
1160
1174
1161
@ Test
1175
1162
public void testHttpErrorWithCode () {
1176
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
1177
- .setCredentials (credentials )
1178
- .setHttpTransport (new MultiRequestMockHttpTransport (ImmutableList .of (
1179
- new MockLowLevelHttpResponse ()
1180
- .setContent ("{\" error\" : {\" message\" : \" UNAUTHORIZED_DOMAIN\" }}" )
1181
- .setStatusCode (500 ))))
1182
- .setProjectId ("test-project-id" )
1183
- .build ());
1184
- FirebaseAuth auth = FirebaseAuth .getInstance ();
1163
+ MockLowLevelHttpResponse response = new MockLowLevelHttpResponse ()
1164
+ .setContent ("{\" error\" : {\" message\" : \" UNAUTHORIZED_DOMAIN\" }}" )
1165
+ .setStatusCode (500 );
1166
+ FirebaseAuth auth = getRetryDisabledAuth (response );
1185
1167
FirebaseUserManager userManager = auth .getUserManager ();
1186
1168
try {
1187
1169
userManager .
getEmailActionLink (
EmailLinkType .
PASSWORD_RESET ,
"[email protected] " ,
null );
@@ -1194,15 +1176,10 @@ public void testHttpErrorWithCode() {
1194
1176
1195
1177
@ Test
1196
1178
public void testUnexpectedHttpError () {
1197
- FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
1198
- .setCredentials (credentials )
1199
- .setHttpTransport (new MultiRequestMockHttpTransport (ImmutableList .of (
1200
- new MockLowLevelHttpResponse ()
1201
- .setContent ("{}" )
1202
- .setStatusCode (500 ))))
1203
- .setProjectId ("test-project-id" )
1204
- .build ());
1205
- FirebaseAuth auth = FirebaseAuth .getInstance ();
1179
+ MockLowLevelHttpResponse response = new MockLowLevelHttpResponse ()
1180
+ .setContent ("{}" )
1181
+ .setStatusCode (500 );
1182
+ FirebaseAuth auth = getRetryDisabledAuth (response );
1206
1183
FirebaseUserManager userManager = auth .getUserManager ();
1207
1184
try {
1208
1185
userManager .
getEmailActionLink (
EmailLinkType .
PASSWORD_RESET ,
"[email protected] " ,
null );
@@ -1231,6 +1208,26 @@ private static TestResponseInterceptor initializeAppForUserManagement(String ...
1231
1208
return interceptor ;
1232
1209
}
1233
1210
1211
+ private static FirebaseAuth getRetryDisabledAuth (MockLowLevelHttpResponse response ) {
1212
+ final MockHttpTransport transport = new MockHttpTransport .Builder ()
1213
+ .setLowLevelHttpResponse (response )
1214
+ .build ();
1215
+ final FirebaseApp app = FirebaseApp .initializeApp (new FirebaseOptions .Builder ()
1216
+ .setCredentials (credentials )
1217
+ .setProjectId ("test-project-id" )
1218
+ .setHttpTransport (transport )
1219
+ .build ());
1220
+ return FirebaseAuth .builder ()
1221
+ .setFirebaseApp (app )
1222
+ .setUserManager (new Supplier <FirebaseUserManager >() {
1223
+ @ Override
1224
+ public FirebaseUserManager get () {
1225
+ return new FirebaseUserManager (app , transport .createRequestFactory ());
1226
+ }
1227
+ })
1228
+ .build ();
1229
+ }
1230
+
1234
1231
private static void checkUserRecord (UserRecord userRecord ) {
1235
1232
assertEquals ("testuser" , userRecord .getUid ());
1236
1233
assertEquals (
"[email protected] " ,
userRecord .
getEmail ());
0 commit comments