|
17 | 17 | package com.google.firebase.messaging; |
18 | 18 |
|
19 | 19 | import static org.junit.Assert.assertEquals; |
20 | | -import static org.junit.Assert.assertFalse; |
21 | 20 | import static org.junit.Assert.assertNotNull; |
22 | 21 | import static org.junit.Assert.assertNull; |
23 | 22 | import static org.junit.Assert.assertSame; |
|
29 | 28 | import com.google.api.client.http.HttpHeaders; |
30 | 29 | import com.google.api.client.http.HttpMethods; |
31 | 30 | import com.google.api.client.http.HttpRequest; |
32 | | -import com.google.api.client.http.HttpRequestInitializer; |
33 | 31 | import com.google.api.client.http.HttpResponseException; |
34 | 32 | import com.google.api.client.http.HttpResponseInterceptor; |
35 | 33 | import com.google.api.client.http.HttpTransport; |
@@ -73,16 +71,9 @@ public class FirebaseMessagingClientImplTest { |
73 | 71 |
|
74 | 72 | private static final String MOCK_RESPONSE = "{\"name\": \"mock-name\"}"; |
75 | 73 |
|
76 | | - private static final String MOCK_BATCH_SUCCESS_RESPONSE = TestUtils.loadResource( |
77 | | - "fcm_batch_success.txt"); |
78 | | - |
79 | | - private static final String MOCK_BATCH_FAILURE_RESPONSE = TestUtils.loadResource( |
80 | | - "fcm_batch_failure.txt"); |
81 | | - |
82 | 74 | private static final Message EMPTY_MESSAGE = Message.builder() |
83 | 75 | .setTopic("test-topic") |
84 | 76 | .build(); |
85 | | - private static final List<Message> MESSAGE_LIST = ImmutableList.of(EMPTY_MESSAGE, EMPTY_MESSAGE); |
86 | 77 |
|
87 | 78 | private static final boolean DRY_RUN_ENABLED = true; |
88 | 79 | private static final boolean DRY_RUN_DISABLED = false; |
@@ -320,187 +311,6 @@ public void testSendErrorWithDetailsAndNoCode() { |
320 | 311 | } |
321 | 312 | } |
322 | 313 |
|
323 | | - @Test |
324 | | - public void testSendAll() throws Exception { |
325 | | - final TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
326 | | - FirebaseMessagingClient client = initMessagingClientForBatchRequests( |
327 | | - MOCK_BATCH_SUCCESS_RESPONSE, interceptor); |
328 | | - |
329 | | - BatchResponse responses = client.sendAll(MESSAGE_LIST, false); |
330 | | - |
331 | | - assertBatchResponse(responses, interceptor, 2, 0); |
332 | | - } |
333 | | - |
334 | | - @Test |
335 | | - public void testSendAllDryRun() throws Exception { |
336 | | - final TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
337 | | - FirebaseMessagingClient client = initMessagingClientForBatchRequests( |
338 | | - MOCK_BATCH_SUCCESS_RESPONSE, interceptor); |
339 | | - |
340 | | - BatchResponse responses = client.sendAll(MESSAGE_LIST, true); |
341 | | - |
342 | | - assertBatchResponse(responses, interceptor, 2, 0); |
343 | | - } |
344 | | - |
345 | | - @Test |
346 | | - public void testRequestInitializerAppliedToBatchRequests() throws Exception { |
347 | | - TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
348 | | - MockHttpTransport transport = new MockHttpTransport.Builder() |
349 | | - .setLowLevelHttpResponse(getBatchResponse(MOCK_BATCH_SUCCESS_RESPONSE)) |
350 | | - .build(); |
351 | | - HttpRequestInitializer initializer = new HttpRequestInitializer() { |
352 | | - @Override |
353 | | - public void initialize(HttpRequest httpRequest) { |
354 | | - httpRequest.getHeaders().set("x-custom-header", "test-value"); |
355 | | - } |
356 | | - }; |
357 | | - FirebaseMessagingClientImpl client = FirebaseMessagingClientImpl.builder() |
358 | | - .setProjectId("test-project") |
359 | | - .setJsonFactory(ApiClientUtils.getDefaultJsonFactory()) |
360 | | - .setRequestFactory(transport.createRequestFactory(initializer)) |
361 | | - .setChildRequestFactory(Utils.getDefaultTransport().createRequestFactory()) |
362 | | - .setResponseInterceptor(interceptor) |
363 | | - .build(); |
364 | | - |
365 | | - try { |
366 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
367 | | - } finally { |
368 | | - HttpRequest request = interceptor.getLastRequest(); |
369 | | - assertEquals("test-value", request.getHeaders().get("x-custom-header")); |
370 | | - } |
371 | | - } |
372 | | - |
373 | | - @Test |
374 | | - public void testSendAllFailure() throws Exception { |
375 | | - final TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
376 | | - FirebaseMessagingClient client = initMessagingClientForBatchRequests( |
377 | | - MOCK_BATCH_FAILURE_RESPONSE, interceptor); |
378 | | - List<Message> messages = ImmutableList.of(EMPTY_MESSAGE, EMPTY_MESSAGE, EMPTY_MESSAGE); |
379 | | - |
380 | | - BatchResponse responses = client.sendAll(messages, DRY_RUN_DISABLED); |
381 | | - |
382 | | - assertBatchResponse(responses, interceptor, 1, 2); |
383 | | - } |
384 | | - |
385 | | - @Test |
386 | | - public void testSendAllHttpError() { |
387 | | - for (int code : HTTP_ERRORS) { |
388 | | - response.setStatusCode(code).setContent("{}"); |
389 | | - |
390 | | - try { |
391 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
392 | | - fail("No error thrown for HTTP error"); |
393 | | - } catch (FirebaseMessagingException error) { |
394 | | - checkExceptionFromHttpResponse(error, HTTP_2_ERROR.get(code), null, |
395 | | - "Unexpected HTTP response with status: " + code + "\n{}"); |
396 | | - } |
397 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
398 | | - } |
399 | | - } |
400 | | - |
401 | | - @Test |
402 | | - public void testSendAllTransportError() { |
403 | | - FirebaseMessagingClient client = initClientWithFaultyTransport(); |
404 | | - |
405 | | - try { |
406 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
407 | | - fail("No error thrown for HTTP error"); |
408 | | - } catch (FirebaseMessagingException error) { |
409 | | - assertEquals(ErrorCode.UNKNOWN, error.getErrorCode()); |
410 | | - assertEquals( |
411 | | - "Unknown error while making a remote service call: transport error", error.getMessage()); |
412 | | - assertTrue(error.getCause() instanceof IOException); |
413 | | - assertNull(error.getHttpResponse()); |
414 | | - assertNull(error.getMessagingErrorCode()); |
415 | | - } |
416 | | - } |
417 | | - |
418 | | - @Test |
419 | | - public void testSendAllErrorWithEmptyResponse() { |
420 | | - for (int code : HTTP_ERRORS) { |
421 | | - response.setStatusCode(code).setZeroContent(); |
422 | | - |
423 | | - try { |
424 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
425 | | - fail("No error thrown for HTTP error"); |
426 | | - } catch (FirebaseMessagingException error) { |
427 | | - checkExceptionFromHttpResponse(error, HTTP_2_ERROR.get(code), null, |
428 | | - "Unexpected HTTP response with status: " + code + "\nnull"); |
429 | | - } |
430 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
431 | | - } |
432 | | - } |
433 | | - |
434 | | - @Test |
435 | | - public void testSendAllErrorWithDetails() { |
436 | | - for (int code : HTTP_ERRORS) { |
437 | | - response.setStatusCode(code).setContent( |
438 | | - "{\"error\": {\"status\": \"INVALID_ARGUMENT\", \"message\": \"test error\"}}"); |
439 | | - |
440 | | - try { |
441 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
442 | | - fail("No error thrown for HTTP error"); |
443 | | - } catch (FirebaseMessagingException error) { |
444 | | - checkExceptionFromHttpResponse(error, ErrorCode.INVALID_ARGUMENT, null); |
445 | | - } |
446 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
447 | | - } |
448 | | - } |
449 | | - |
450 | | - @Test |
451 | | - public void testSendAllErrorWithCanonicalCode() { |
452 | | - for (int code : HTTP_ERRORS) { |
453 | | - response.setStatusCode(code).setContent( |
454 | | - "{\"error\": {\"status\": \"NOT_FOUND\", \"message\": \"test error\"}}"); |
455 | | - |
456 | | - try { |
457 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
458 | | - fail("No error thrown for HTTP error"); |
459 | | - } catch (FirebaseMessagingException error) { |
460 | | - checkExceptionFromHttpResponse(error, ErrorCode.NOT_FOUND, null); |
461 | | - } |
462 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
463 | | - } |
464 | | - } |
465 | | - |
466 | | - @Test |
467 | | - public void testSendAllErrorWithFcmError() { |
468 | | - for (int code : HTTP_ERRORS) { |
469 | | - response.setStatusCode(code).setContent( |
470 | | - "{\"error\": {\"status\": \"INVALID_ARGUMENT\", \"message\": \"test error\", " |
471 | | - + "\"details\":[{\"@type\": \"type.googleapis.com/google.firebase.fcm" |
472 | | - + ".v1.FcmError\", \"errorCode\": \"UNREGISTERED\"}]}}"); |
473 | | - |
474 | | - try { |
475 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
476 | | - fail("No error thrown for HTTP error"); |
477 | | - } catch (FirebaseMessagingException error) { |
478 | | - checkExceptionFromHttpResponse(error, ErrorCode.INVALID_ARGUMENT, |
479 | | - MessagingErrorCode.UNREGISTERED); |
480 | | - } |
481 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
482 | | - } |
483 | | - } |
484 | | - |
485 | | - @Test |
486 | | - public void testSendAllErrorWithoutMessage() { |
487 | | - final String responseBody = "{\"error\": {\"status\": \"INVALID_ARGUMENT\", " |
488 | | - + "\"details\":[{\"@type\": \"type.googleapis.com/google.firebase.fcm" |
489 | | - + ".v1.FcmError\", \"errorCode\": \"UNREGISTERED\"}]}}"; |
490 | | - for (int code : HTTP_ERRORS) { |
491 | | - response.setStatusCode(code).setContent(responseBody); |
492 | | - |
493 | | - try { |
494 | | - client.sendAll(MESSAGE_LIST, DRY_RUN_DISABLED); |
495 | | - fail("No error thrown for HTTP error"); |
496 | | - } catch (FirebaseMessagingException error) { |
497 | | - checkExceptionFromHttpResponse(error, ErrorCode.INVALID_ARGUMENT, |
498 | | - MessagingErrorCode.UNREGISTERED, |
499 | | - "Unexpected HTTP response with status: " + code + "\n" + responseBody); |
500 | | - } |
501 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
502 | | - } |
503 | | - } |
504 | 314 |
|
505 | 315 | @Test(expected = IllegalArgumentException.class) |
506 | 316 | public void testBuilderNullProjectId() { |
@@ -563,18 +373,6 @@ private FirebaseMessagingClientImpl initMessagingClient( |
563 | 373 | .build(); |
564 | 374 | } |
565 | 375 |
|
566 | | - private FirebaseMessagingClientImpl initMessagingClientForBatchRequests( |
567 | | - String responsePayload, TestResponseInterceptor interceptor) { |
568 | | - MockLowLevelHttpResponse httpResponse = getBatchResponse(responsePayload); |
569 | | - return initMessagingClient(httpResponse, interceptor); |
570 | | - } |
571 | | - |
572 | | - private MockLowLevelHttpResponse getBatchResponse(String responsePayload) { |
573 | | - return new MockLowLevelHttpResponse() |
574 | | - .setContentType("multipart/mixed; boundary=test_boundary") |
575 | | - .setContent(responsePayload); |
576 | | - } |
577 | | - |
578 | 376 | private FirebaseMessagingClientImpl initClientWithFaultyTransport() { |
579 | 377 | HttpTransport transport = TestUtils.createFaultyHttpTransport(); |
580 | 378 | return FirebaseMessagingClientImpl.builder() |
@@ -603,64 +401,6 @@ private void checkRequest( |
603 | 401 | assertEquals(expected, parsed); |
604 | 402 | } |
605 | 403 |
|
606 | | - private void assertBatchResponse( |
607 | | - BatchResponse batchResponse, TestResponseInterceptor interceptor, |
608 | | - int successCount, int failureCount) throws IOException { |
609 | | - |
610 | | - assertEquals(successCount, batchResponse.getSuccessCount()); |
611 | | - assertEquals(failureCount, batchResponse.getFailureCount()); |
612 | | - |
613 | | - List<SendResponse> responses = batchResponse.getResponses(); |
614 | | - assertEquals(successCount + failureCount, responses.size()); |
615 | | - for (int i = 0; i < successCount; i++) { |
616 | | - SendResponse sendResponse = responses.get(i); |
617 | | - assertTrue(sendResponse.isSuccessful()); |
618 | | - assertEquals("projects/test-project/messages/" + (i + 1), sendResponse.getMessageId()); |
619 | | - assertNull(sendResponse.getException()); |
620 | | - } |
621 | | - |
622 | | - for (int i = successCount; i < failureCount; i++) { |
623 | | - SendResponse sendResponse = responses.get(i); |
624 | | - assertFalse(sendResponse.isSuccessful()); |
625 | | - assertNull(sendResponse.getMessageId()); |
626 | | - |
627 | | - FirebaseMessagingException exception = sendResponse.getException(); |
628 | | - assertNotNull(exception); |
629 | | - assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode()); |
630 | | - assertNull(exception.getCause()); |
631 | | - assertNull(exception.getHttpResponse()); |
632 | | - assertEquals(MessagingErrorCode.INVALID_ARGUMENT, exception.getMessagingErrorCode()); |
633 | | - } |
634 | | - |
635 | | - checkBatchRequestHeader(interceptor.getLastRequest()); |
636 | | - checkBatchRequest(interceptor.getLastRequest(), successCount + failureCount); |
637 | | - } |
638 | | - |
639 | | - private void checkBatchRequestHeader(HttpRequest request) { |
640 | | - assertEquals("POST", request.getRequestMethod()); |
641 | | - assertEquals("https://fcm.googleapis.com/batch", request.getUrl().toString()); |
642 | | - } |
643 | | - |
644 | | - private void checkBatchRequest(HttpRequest request, int expectedParts) throws IOException { |
645 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
646 | | - request.getContent().writeTo(out); |
647 | | - String[] lines = out.toString().split("\n"); |
648 | | - assertEquals(expectedParts, countLinesWithPrefix(lines, "POST " + TEST_FCM_URL)); |
649 | | - assertEquals(expectedParts, countLinesWithPrefix(lines, "x-goog-api-format-version: 2")); |
650 | | - assertEquals(expectedParts, countLinesWithPrefix( |
651 | | - lines, "x-firebase-client: fire-admin-java/" + SdkUtils.getVersion())); |
652 | | - } |
653 | | - |
654 | | - private int countLinesWithPrefix(String[] lines, String prefix) { |
655 | | - int matchCount = 0; |
656 | | - for (String line : lines) { |
657 | | - if (line.trim().startsWith(prefix)) { |
658 | | - matchCount++; |
659 | | - } |
660 | | - } |
661 | | - return matchCount; |
662 | | - } |
663 | | - |
664 | 404 | private FirebaseMessagingClientImpl.Builder fullyPopulatedBuilder() { |
665 | 405 | return FirebaseMessagingClientImpl.builder() |
666 | 406 | .setProjectId("test-project") |
|
0 commit comments