Skip to content

Commit ec7d7d4

Browse files
committed
Add multicast/send-all support
1 parent 9719659 commit ec7d7d4

File tree

14 files changed

+621
-76
lines changed

14 files changed

+621
-76
lines changed

FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseMessagingTest.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,76 @@ public async Task Send()
4949
Assert.True(!string.IsNullOrEmpty(id));
5050
Assert.Matches(new Regex("^projects/.*/messages/.*$"), id);
5151
}
52+
53+
[Fact]
54+
public async Task SendAll()
55+
{
56+
var message1 = new Message()
57+
{
58+
Topic = "foo-bar",
59+
Notification = new Notification()
60+
{
61+
Title = "Title",
62+
Body = "Body",
63+
},
64+
Android = new AndroidConfig()
65+
{
66+
Priority = Priority.Normal,
67+
TimeToLive = TimeSpan.FromHours(1),
68+
RestrictedPackageName = "com.google.firebase.testing",
69+
},
70+
};
71+
var message2 = new Message()
72+
{
73+
Topic = "fiz-buz",
74+
Notification = new Notification()
75+
{
76+
Title = "Title",
77+
Body = "Body",
78+
},
79+
Android = new AndroidConfig()
80+
{
81+
Priority = Priority.Normal,
82+
TimeToLive = TimeSpan.FromHours(1),
83+
RestrictedPackageName = "com.google.firebase.testing",
84+
},
85+
};
86+
var response = await FirebaseMessaging.DefaultInstance.SendAllAsync(new[] { message1, message2 }, dryRun: true);
87+
Assert.NotNull(response);
88+
Assert.Equal(2, response.SuccessCount);
89+
Assert.True(!string.IsNullOrEmpty(response.Responses[0].MessageId));
90+
Assert.Matches(new Regex("^projects/.*/messages/.*$"), response.Responses[0].MessageId);
91+
Assert.True(!string.IsNullOrEmpty(response.Responses[1].MessageId));
92+
Assert.Matches(new Regex("^projects/.*/messages/.*$"), response.Responses[1].MessageId);
93+
}
94+
95+
[Fact]
96+
public async Task SendMulticast()
97+
{
98+
var multicastMessage = new MulticastMessage
99+
{
100+
Notification = new Notification()
101+
{
102+
Title = "Title",
103+
Body = "Body",
104+
},
105+
Android = new AndroidConfig()
106+
{
107+
Priority = Priority.Normal,
108+
TimeToLive = TimeSpan.FromHours(1),
109+
RestrictedPackageName = "com.google.firebase.testing",
110+
},
111+
Tokens = new[]
112+
{
113+
"token1",
114+
"token2",
115+
},
116+
};
117+
var response = await FirebaseMessaging.DefaultInstance.SendMulticastAsync(multicastMessage, dryRun: true);
118+
Assert.NotNull(response);
119+
Assert.Equal(2, response.FailureCount);
120+
Assert.NotNull(response.Responses[0].Exception);
121+
Assert.NotNull(response.Responses[1].Exception);
122+
}
52123
}
53124
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using FirebaseAdmin.Messaging;
17+
using Xunit;
18+
19+
namespace FirebaseAdmin.Tests.Messaging
20+
{
21+
public class BatchItemResponseTest
22+
{
23+
[Fact]
24+
public void SuccessfulResponse()
25+
{
26+
var response = BatchItemResponse.FromMessageId("message-id");
27+
28+
Assert.Equal("message-id", response.MessageId);
29+
Assert.True(response.IsSuccessful);
30+
Assert.Null(response.Exception);
31+
}
32+
33+
[Fact]
34+
public void FailureResponse()
35+
{
36+
FirebaseMessagingException exception = new FirebaseMessagingException(
37+
400,
38+
"error-message",
39+
null);
40+
var response = BatchItemResponse.FromException(exception);
41+
42+
Assert.Null(response.MessageId);
43+
Assert.False(response.IsSuccessful);
44+
Assert.Same(exception, response.Exception);
45+
}
46+
47+
[Fact]
48+
public void MessageIdCannotBeNull()
49+
{
50+
Assert.Throws<ArgumentNullException>(() => BatchItemResponse.FromMessageId(null));
51+
}
52+
53+
[Fact]
54+
public void MessageIdCannotBeEmpty()
55+
{
56+
Assert.Throws<ArgumentException>(() => BatchItemResponse.FromMessageId(string.Empty));
57+
}
58+
59+
[Fact]
60+
public void ExceptionCannotBeNull()
61+
{
62+
Assert.Throws<ArgumentNullException>(() => BatchItemResponse.FromException(null));
63+
}
64+
}
65+
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/BatchResponseTest.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
216
using System.Collections.Generic;
317
using System.Linq;
418
using FirebaseAdmin.Messaging;
@@ -11,7 +25,7 @@ public class BatchResponseTest
1125
[Fact]
1226
public void EmptyResponses()
1327
{
14-
var responses = new List<SendResponse>();
28+
var responses = new List<BatchItemResponse>();
1529

1630
var batchResponse = new BatchResponse(responses);
1731

@@ -23,13 +37,13 @@ public void EmptyResponses()
2337
[Fact]
2438
public void SomeResponse()
2539
{
26-
var responses = new SendResponse[]
40+
var responses = new BatchItemResponse[]
2741
{
28-
SendResponse.FromMessageId("message1"),
29-
SendResponse.FromMessageId("message2"),
30-
SendResponse.FromException(
42+
BatchItemResponse.FromMessageId("message1"),
43+
BatchItemResponse.FromMessageId("message2"),
44+
BatchItemResponse.FromException(
3145
new FirebaseMessagingException(
32-
"error-code",
46+
400,
3347
"error-message",
3448
null)),
3549
};

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/FirebaseMessagingClientTest.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
using System;
1616
using System.Net;
17-
using System.Threading;
1817
using System.Threading.Tasks;
1918
using FirebaseAdmin.Tests;
2019
using Google.Apis.Auth.OAuth2;
@@ -89,6 +88,65 @@ public async Task SendAsync()
8988
Assert.Equal(2, handler.Calls);
9089
}
9190

91+
[Fact]
92+
public async Task SendAllAsync()
93+
{
94+
var rawResponse = @"
95+
--batch_test-boundary
96+
Content-Type: application/http
97+
Content-ID: response-
98+
99+
HTTP/1.1 200 OK
100+
Content-Type: application/json; charset=UTF-8
101+
Vary: Origin
102+
Vary: X-Origin
103+
Vary: Referer
104+
105+
{
106+
""name"": ""projects/fir-adminintegrationtests/messages/8580920590356323124""
107+
}
108+
109+
--batch_test-boundary
110+
Content-Type: application/http
111+
Content-ID: response-
112+
113+
HTTP/1.1 200 OK
114+
Content-Type: application/json; charset=UTF-8
115+
Vary: Origin
116+
Vary: X-Origin
117+
Vary: Referer
118+
119+
{
120+
""name"": ""projects/fir-adminintegrationtests/messages/5903525881088369386""
121+
}
122+
123+
--batch_test-boundary
124+
";
125+
var handler = new MockMessageHandler()
126+
{
127+
Response = rawResponse,
128+
ApplyContentHeaders = (headers) =>
129+
{
130+
headers.Remove("Content-Type");
131+
headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=batch_test-boundary");
132+
},
133+
};
134+
var factory = new MockHttpClientFactory(handler);
135+
var client = new FirebaseMessagingClient(factory, MockCredential, "test-project");
136+
var message1 = new Message()
137+
{
138+
Token = "test-token1",
139+
};
140+
var message2 = new Message()
141+
{
142+
Token = "test-token2",
143+
};
144+
var response = await client.SendAllAsync(new[] { message1, message2 });
145+
Assert.Equal(2, response.SuccessCount);
146+
Assert.Equal("projects/fir-adminintegrationtests/messages/8580920590356323124", response.Responses[0].MessageId);
147+
Assert.Equal("projects/fir-adminintegrationtests/messages/5903525881088369386", response.Responses[1].MessageId);
148+
}
149+
92150
[Fact]
93151
public async Task HttpErrorAsync()
94152
{

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MulticastMessageTest.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
using System;
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
216
using System.Collections.Generic;
317
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
618
using FirebaseAdmin.Messaging;
719
using Google.Apis.Json;
820
using Newtonsoft.Json.Linq;
@@ -23,6 +35,21 @@ public void EmptyMulticastMessage()
2335
this.AssertJsonEquals(new JObject() { { "tokens", new JArray("test-token") } }, message);
2436
}
2537

38+
[Fact]
39+
public void GetMessageList()
40+
{
41+
var message = new MulticastMessage
42+
{
43+
Tokens = new[] { "test-token1", "test-token2" },
44+
};
45+
46+
var messages = message.GetMessageList();
47+
48+
Assert.Equal(2, messages.Count);
49+
Assert.Equal("test-token1", messages[0].Token);
50+
Assert.Equal("test-token2", messages[1].Token);
51+
}
52+
2653
[Fact]
2754
public void Data()
2855
{
@@ -143,6 +170,12 @@ public void MessageWithNoTokens()
143170
Assert.Throws<ArgumentException>(() => new MulticastMessage { Tokens = new string[] { } }.CopyAndValidate());
144171
}
145172

173+
[Fact]
174+
public void MessageWithTooManyTokens()
175+
{
176+
Assert.Throws<ArgumentException>(() => new MulticastMessage { Tokens = Enumerable.Range(0, 101).Select(x => x.ToString()).ToList() }.CopyAndValidate());
177+
}
178+
146179
[Fact]
147180
public void AndroidConfig()
148181
{

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/SendResponseTest.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)