Skip to content

Commit 186b96d

Browse files
author
Leo
committed
changes part 1
1 parent a62c04f commit 186b96d

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/TopicManagementResponseTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void SuccessfulReponse()
1414
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json);
1515
var response = new TopicManagementResponse(instanceIdServiceResponse);
1616

17-
Assert.Equal(0, response.FailureCount);
17+
Assert.Empty(response.Errors);
1818
Assert.Equal(2, response.SuccessCount);
1919
}
2020

@@ -25,7 +25,7 @@ public void UnsuccessfulResponse()
2525
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json);
2626
var response = new TopicManagementResponse(instanceIdServiceResponse);
2727

28-
Assert.Equal(1, response.FailureCount);
28+
Assert.Single(response.Errors);
2929
Assert.Equal(1, response.SuccessCount);
3030
Assert.NotEmpty(response.Errors);
3131
Assert.Equal("registration-token-not-registered", response.Errors[0].Reason);
@@ -37,17 +37,17 @@ public void NullResponse()
3737
{
3838
Assert.Throws<ArgumentException>(() =>
3939
{
40-
var response = new TopicManagementResponse(null);
40+
new TopicManagementResponse(null);
4141
});
4242
}
4343

4444
[Fact]
4545
public void EmptyResponse()
4646
{
47+
var instanceIdServiceResponse = new InstanceIdServiceResponse();
4748
Assert.Throws<ArgumentException>(() =>
4849
{
49-
var instanceIdServiceResponse = new InstanceIdServiceResponse();
50-
var response = new TopicManagementResponse(instanceIdServiceResponse);
50+
new TopicManagementResponse(instanceIdServiceResponse);
5151
});
5252
}
5353

@@ -58,7 +58,7 @@ public void UnknownError()
5858
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json);
5959
var response = new TopicManagementResponse(instanceIdServiceResponse);
6060

61-
Assert.Equal(1, response.FailureCount);
61+
Assert.Empty(response.Errors);
6262
Assert.Equal(0, response.SuccessCount);
6363
Assert.NotEmpty(response.Errors);
6464
Assert.Equal("unknown-error", response.Errors[0].Reason);
@@ -72,7 +72,7 @@ public void UnexpectedResponse()
7272
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json);
7373
var response = new TopicManagementResponse(instanceIdServiceResponse);
7474

75-
Assert.Equal(0, response.FailureCount);
75+
Assert.Empty(response.Errors);
7676
Assert.Equal(1, response.SuccessCount);
7777
}
7878

FirebaseAdmin/FirebaseAdmin/Messaging/InstanceIdClient.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.IO;
18+
using System.Linq;
1819
using System.Net.Http;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -87,6 +88,8 @@ public void Dispose()
8788

8889
private async Task<TopicManagementResponse> SendInstanceIdRequest(string topic, List<string> registrationTokens, string path)
8990
{
91+
this.ValidateRegistrationTokenList(registrationTokens);
92+
9093
string url = $"{IidHost}/{path}";
9194
var body = new InstanceIdServiceRequest
9295
{
@@ -131,6 +134,32 @@ private FirebaseMessagingException CreateExceptionFromResponse(HttpRequestExcept
131134
response: temp.HttpResponse);
132135
}
133136

137+
private void ValidateRegistrationTokenList(List<string> registrationTokens)
138+
{
139+
if (registrationTokens == null)
140+
{
141+
throw new FirebaseMessagingException(ErrorCode.InvalidArgument, "Registration token list must not be null");
142+
}
143+
144+
if (registrationTokens.Count() == 0)
145+
{
146+
throw new FirebaseMessagingException(ErrorCode.InvalidArgument, "Registration token list must not be empty");
147+
}
148+
149+
if (registrationTokens.Count() > 1000)
150+
{
151+
throw new FirebaseMessagingException(ErrorCode.InvalidArgument, "Registration token list must not contain more than 1000 tokens");
152+
}
153+
154+
foreach (var registrationToken in registrationTokens)
155+
{
156+
if (string.IsNullOrEmpty(registrationToken))
157+
{
158+
throw new FirebaseMessagingException(ErrorCode.InvalidArgument, "Registration token must not be null");
159+
}
160+
}
161+
}
162+
134163
private string GetPrefixedTopic(string topic)
135164
{
136165
if (topic.StartsWith("/topics/"))

0 commit comments

Comments
 (0)