Skip to content

Commit 949a109

Browse files
author
Leo
committed
Before removal
1 parent 497f8c4 commit 949a109

File tree

4 files changed

+174
-10
lines changed

4 files changed

+174
-10
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Google.Apis.Auth.OAuth2;
5+
using Google.Apis.Http;
6+
using Xunit;
7+
8+
namespace FirebaseAdmin.Tests.Messaging
9+
{
10+
public class InstanceIdClientTest
11+
{
12+
private static readonly GoogleCredential MockCredential =
13+
GoogleCredential.FromAccessToken("test-token");
14+
15+
[Fact]
16+
public void InstanceIdClientThrowsOnNoProjectId()
17+
{
18+
var clientFactory = new HttpClientFactory();
19+
Assert.Throws<ArgumentException>(
20+
() => new InstanceIdClient(clientFactory, MockCredential, null));
21+
Assert.Throws<ArgumentException>(
22+
() => new InstanceIdClient(clientFactory, MockCredential, string.Empty));
23+
}
24+
25+
[Fact]
26+
public void InstanceIdClientThrowsOnNoCredential()
27+
{
28+
var clientFactory = new HttpClientFactory();
29+
Assert.Throws<ArgumentNullException>(
30+
() => new InstanceIdClient(clientFactory, null, "test-project"));
31+
}
32+
33+
[Fact]
34+
public void InstanceIdClientThrowsOnNoClientFactory()
35+
{
36+
var clientFactory = new HttpClientFactory();
37+
Assert.Throws<ArgumentNullException>(
38+
() => new InstanceIdClient(null, MockCredential, "test-project"));
39+
}
40+
41+
[Fact]
42+
public async Task InstanceIdClientSubscribesToTopic()
43+
{
44+
var handler = new MockMessageHandler()
45+
{
46+
Response = @"{""results"":[{}]}",
47+
};
48+
var factory = new MockHttpClientFactory(handler);
49+
50+
var client = new InstanceIdClient(factory, MockCredential, "test-project");
51+
52+
var result = await client.SubscribeToTopic("test-topic", new List<string> { "abc123" });
53+
54+
Assert.Equal(1, result.GetSuccessCount());
55+
}
56+
57+
[Fact]
58+
public async Task InstanceIdClientUnsubscribesFromTopic()
59+
{
60+
var handler = new MockMessageHandler()
61+
{
62+
Response = @"{""results"":[{}]}",
63+
};
64+
var factory = new MockHttpClientFactory(handler);
65+
66+
var client = new InstanceIdClient(factory, MockCredential, "test-project");
67+
68+
var result = await client.UnsubscribeFromTopic("test-topic", new List<string> { "abc123" });
69+
70+
Assert.Equal(1, result.GetSuccessCount());
71+
}
72+
}
73+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json.Linq;
4+
using Xunit;
5+
6+
namespace FirebaseAdmin.Tests.Messaging
7+
{
8+
public class TopicManagementResponseTest
9+
{
10+
[Fact]
11+
public void SuccessfulTopicManagementResponse()
12+
{
13+
var json = @"[{}, {}]";
14+
var jObjects = JArray.Parse(json).ToObject<List<JObject>>();
15+
var response = new TopicManagementResponse(jObjects);
16+
17+
Assert.Equal(0, response.GetFailureCount());
18+
Assert.Equal(2, response.GetSuccessCount());
19+
}
20+
21+
[Fact]
22+
public void UnsuccessfulTopicManagementResponse()
23+
{
24+
var json = @"[{}, {""error"":""NOT_FOUND""}]";
25+
var jObjects = JArray.Parse(json).ToObject<List<JObject>>();
26+
var response = new TopicManagementResponse(jObjects);
27+
28+
Assert.Equal(1, response.GetFailureCount());
29+
Assert.Equal(1, response.GetSuccessCount());
30+
}
31+
32+
[Fact]
33+
public void TopicManagementResponseCannotBeNull()
34+
{
35+
Assert.Throws<ArgumentException>(() =>
36+
{
37+
List<JObject> jObjects = null;
38+
var response = new TopicManagementResponse(jObjects);
39+
});
40+
}
41+
42+
[Fact]
43+
public void TopicManagementResponseCannotBeEmptyArray()
44+
{
45+
Assert.Throws<ArgumentException>(() =>
46+
{
47+
List<JObject> jObjects = new List<JObject>();
48+
var response = new TopicManagementResponse(jObjects);
49+
});
50+
}
51+
52+
[Fact]
53+
public void TopicManagementResponseHandlesUnknownErrors()
54+
{
55+
var json = @"[{""error"":""NOT_A_REAL_ERROR_CODE""}]";
56+
var jObjects = JArray.Parse(json).ToObject<List<JObject>>();
57+
var response = new TopicManagementResponse(jObjects);
58+
59+
Assert.Equal(1, response.GetFailureCount());
60+
Assert.Equal(0, response.GetSuccessCount());
61+
}
62+
}
63+
}

FirebaseAdmin/FirebaseAdmin/Messaging/InstanceIdClient.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
using System;
1+
// Copyright 2019, 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;
3-
using System.Collections.Immutable;
417
using System.IO;
518
using System.Net.Http;
619
using System.Threading;
@@ -34,7 +47,7 @@ public sealed class InstanceIdClient
3447
/// Initializes a new instance of the <see cref="InstanceIdClient"/> class.
3548
/// </summary>
3649
/// <param name="clientFactory">A default implentation of the HTTP client factory.</param>
37-
/// <param name="credential">A GoogleCredential.</param>
50+
/// <param name="credential">An instance of the <see cref="GoogleCredential"/> GoogleCredential class.</param>
3851
/// <param name="projectId">The Project Id for FCM Messaging.</param>
3952
public InstanceIdClient(HttpClientFactory clientFactory, GoogleCredential credential, string projectId)
4053
{
@@ -56,7 +69,7 @@ public InstanceIdClient(HttpClientFactory clientFactory, GoogleCredential creden
5669
/// <summary>
5770
/// Index of the registration token to which this error is related to.
5871
/// </summary>
59-
/// <param name="topic">The topic name to subscribe to.</param>
72+
/// <param name="topic">The topic name to subscribe to. /topics/ will be prepended to the topic name provided if absent.</param>
6073
/// <param name="registrationTokens">A list of registration tokens to subscribe.</param>
6174
/// <returns>The response produced by FCM topic management operations.</returns>
6275
public async Task<TopicManagementResponse> SubscribeToTopic(string topic, List<string> registrationTokens)
@@ -78,7 +91,7 @@ public async Task<TopicManagementResponse> SubscribeToTopic(string topic, List<s
7891
/// <summary>
7992
/// Index of the registration token to which this error is related to.
8093
/// </summary>
81-
/// <param name="topic">The topic name to unsubscribe from.</param>
94+
/// <param name="topic">The topic name to unsubscribe from. /topics/ will be prepended to the topic name provided if absent.</param>
8295
/// <param name="registrationTokens">A list of registration tokens to unsubscribe.</param>
8396
/// <returns>The response produced by FCM topic management operations.</returns>
8497
public async Task<TopicManagementResponse> UnsubscribeFromTopic(string topic, List<string> registrationTokens)
@@ -99,7 +112,7 @@ public async Task<TopicManagementResponse> UnsubscribeFromTopic(string topic, Li
99112

100113
private async Task<TopicManagementResponse> SendInstanceIdRequest(string topic, List<string> registrationTokens, string path)
101114
{
102-
string url = string.Format("%s/%s", this.iidHost, path);
115+
string url = $"{this.iidHost}/{path}";
103116
var body = new InstanceIdServiceRequest
104117
{
105118
Topic = this.GetPrefixedTopic(topic),
@@ -159,6 +172,7 @@ private class InstanceIdServiceRequest
159172
public List<string> RegistrationTokens { get; set; }
160173
}
161174

175+
/*
162176
private class InstanceIdServiceErrorResponse
163177
{
164178
[JsonProperty("error")]
@@ -170,4 +184,5 @@ private class InstanceIdServiceResponse
170184
[JsonProperty("results")]
171185
public List<JObject> Results { get; set; }
172186
}
187+
*/
173188
}

FirebaseAdmin/FirebaseAdmin/Messaging/TopicManagementResponse.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
1+
// Copyright 2019, 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 Newtonsoft.Json.Linq;
418

@@ -73,9 +87,8 @@ public class Error
7387
// TODO: Should we handle other error codes here (e.g. PERMISSION_DENIED)?
7488
private readonly IReadOnlyDictionary<string, string> errorCodes;
7589
private readonly string unknownError = "unknown-error";
76-
77-
private int index;
78-
private string reason;
90+
private readonly int index;
91+
private readonly string reason;
7992

8093
/// <summary>
8194
/// Initializes a new instance of the <see cref="Error"/> class.

0 commit comments

Comments
 (0)