Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.google.android.gcm.server;

import static com.google.android.gcm.server.Constants.FCM_SEND_ENDPOINT;
import static com.google.android.gcm.server.Constants.JSON_CANONICAL_IDS;
import static com.google.android.gcm.server.Constants.JSON_ERROR;
import static com.google.android.gcm.server.Constants.JSON_FAILURE;
Expand Down Expand Up @@ -70,7 +69,6 @@
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Optional;

/**
* Helper class to send messages to the GCM service using an API Key.
Expand Down Expand Up @@ -105,16 +103,22 @@ public class Sender {
* @param key API key obtained through the Google API Console.
*/
public Sender(String key) {
this.key = nonNull(key);
this(key, Constants.FCM_SEND_ENDPOINT);
}

/**
* Full options constructor.
*
* @param key FCM Server Key obtained through the Firebase Web Console.
* @param endpoint Endpoint to use when sending the message.
*/
public Sender(String key, String endpoint) {
this(key);
this.endpoint = endpoint;
this.key = nonNull(key);
this.endpoint = nonNull(endpoint);
}

public String getEndpoint() {
return Optional.ofNullable(endpoint).orElse(FCM_SEND_ENDPOINT);
return endpoint;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
Expand Down Expand Up @@ -396,8 +397,7 @@ public void testSendNoRetry_internalServerError() throws Exception {
@Test
public void testSendNoRetry_ioException_post() throws Exception {
when(mockedConn.getOutputStream()).thenThrow(new IOException());
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
Result result = sender.sendNoRetry(message, regId);
assertNull(result);
}
Expand All @@ -407,8 +407,7 @@ public void testSendNoRetry_ioException_errorStream() throws Exception {
when(mockedConn.getResponseCode()).thenReturn(42);
when(mockedConn.getOutputStream()).thenReturn(outputStream);
when(mockedConn.getErrorStream()).thenReturn(exceptionalStream);
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
try {
sender.sendNoRetry(message, regId);
} catch (InvalidRequestException e) {
Expand All @@ -421,8 +420,7 @@ public void testSendNoRetry_ioException_inputStream() throws Exception {
when(mockedConn.getResponseCode()).thenReturn(200);
when(mockedConn.getOutputStream()).thenReturn(outputStream);
when(mockedConn.getInputStream()).thenReturn(exceptionalStream);
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
Result result = sender.sendNoRetry(message, regId);
assertNull(result);
}
Expand Down Expand Up @@ -665,8 +663,7 @@ public void testSendNoRetry_json_badRequest_nullError() throws Exception {
@Test
public void testSendNoRetry_json_ioException_post() throws Exception {
when(mockedConn.getOutputStream()).thenThrow(new IOException());
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
MulticastResult multicastResult = sender.sendNoRetry(message,
Arrays.asList("4", "8", "15"));
assertNull(multicastResult);
Expand All @@ -677,8 +674,7 @@ public void testSendNoRetry_json_ioException_errorStream() throws Exception {
when(mockedConn.getResponseCode()).thenReturn(42);
when(mockedConn.getOutputStream()).thenReturn(outputStream);
when(mockedConn.getErrorStream()).thenReturn(exceptionalStream);
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
try {
sender.sendNoRetry(message, Arrays.asList("4", "8", "15"));
} catch (InvalidRequestException e) {
Expand All @@ -691,8 +687,7 @@ public void testSendNoRetry_json_ioException_inputStream() throws Exception {
when(mockedConn.getResponseCode()).thenReturn(200);
when(mockedConn.getOutputStream()).thenReturn(outputStream);
when(mockedConn.getInputStream()).thenReturn(exceptionalStream);
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
MulticastResult multicastResult = sender.sendNoRetry(message,
Arrays.asList("4", "8", "15"));
assertNull(multicastResult);
Expand Down Expand Up @@ -744,7 +739,9 @@ private void assertResult(Result result, String messageId, String error,

private void assertRequestJsonBody(String...expectedRegIds) throws Exception {
ArgumentCaptor<String> capturedBody = ArgumentCaptor.forClass(String.class);
verify(sender).post(eq(Constants.GCM_SEND_ENDPOINT), eq("application/json"),
verify(sender).post(
eq(Constants.FCM_SEND_ENDPOINT),
eq("application/json"),
capturedBody.capture());
// parse body
String body = capturedBody.getValue();
Expand Down Expand Up @@ -865,12 +862,12 @@ public void testPost_noUrl() throws Exception {

@Test(expected = IllegalArgumentException.class)
public void testPost_noBody() throws Exception {
sender.post(Constants.GCM_SEND_ENDPOINT, "whatever", null);
sender.post(Constants.FCM_SEND_ENDPOINT, "whatever", null);
}

@Test(expected = IllegalArgumentException.class)
public void testPost_noType() throws Exception {
sender.post(Constants.GCM_SEND_ENDPOINT, null, "whatever");
sender.post(Constants.FCM_SEND_ENDPOINT, null, "whatever");
}

@Test
Expand All @@ -879,7 +876,7 @@ public void testPost() throws Exception {
String responseBody = "resp";
setResponseExpectations(200, responseBody);
HttpURLConnection response =
sender.post(Constants.GCM_SEND_ENDPOINT, requestBody);
sender.post(Constants.FCM_SEND_ENDPOINT, requestBody);
assertEquals(requestBody, new String(outputStream.toByteArray()));
verify(mockedConn).setRequestMethod("POST");
verify(mockedConn).setFixedLengthStreamingMode(requestBody.getBytes("UTF-8").length);
Expand All @@ -895,7 +892,7 @@ public void testPost_customType() throws Exception {
String responseBody = "resp";
setResponseExpectations(200, responseBody);
HttpURLConnection response =
sender.post(Constants.GCM_SEND_ENDPOINT, "stuff", requestBody);
sender.post(Constants.FCM_SEND_ENDPOINT, "stuff", requestBody);
assertEquals(requestBody, new String(outputStream.toByteArray()));
verify(mockedConn).setRequestMethod("POST");
verify(mockedConn).setFixedLengthStreamingMode(requestBody.getBytes("UTF-8").length);
Expand All @@ -918,8 +915,7 @@ private void setResponseExpectations(int statusCode, String response)
when(mockedConn.getErrorStream()).thenReturn(inputStream);
}
when(mockedConn.getOutputStream()).thenReturn(outputStream);
doReturn(mockedConn).when(sender)
.getConnection(Constants.GCM_SEND_ENDPOINT);
doReturn(mockedConn).when(sender).getConnection(anyString());
}

private void doNotSleep() {
Expand Down