Skip to content

Commit 7aaf52f

Browse files
authored
Merge pull request #12 from firebase/hkj-fcm
FCM Implementation (Part 1)
2 parents 797feb1 + d7f22ec commit 7aaf52f

20 files changed

+3729
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 System.Text.RegularExpressions;
17+
using System.Threading.Tasks;
18+
using FirebaseAdmin.Messaging;
19+
using Xunit;
20+
21+
namespace FirebaseAdmin.IntegrationTests
22+
{
23+
public class FirebaseMessagingTest
24+
{
25+
public FirebaseMessagingTest()
26+
{
27+
IntegrationTestUtils.EnsureDefaultApp();
28+
}
29+
30+
[Fact]
31+
public async Task Send()
32+
{
33+
var message = new Message()
34+
{
35+
Topic = "foo-bar",
36+
Notification = new Notification()
37+
{
38+
Title = "Title",
39+
Body = "Body",
40+
},
41+
Android = new AndroidConfig()
42+
{
43+
Priority = Priority.Normal,
44+
TimeToLive = TimeSpan.FromHours(1),
45+
RestrictedPackageName = "com.google.firebase.testing",
46+
},
47+
};
48+
var id = await FirebaseMessaging.DefaultInstance.SendAsync(message, dryRun: true);
49+
Assert.True(!string.IsNullOrEmpty(id));
50+
Assert.Matches(new Regex("^projects/.*/messages/.*$"), id);
51+
}
52+
}
53+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 System.Net;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using FirebaseAdmin.Tests;
20+
using Google.Apis.Auth.OAuth2;
21+
using Google.Apis.Http;
22+
using Newtonsoft.Json;
23+
using Xunit;
24+
25+
namespace FirebaseAdmin.Messaging.Tests
26+
{
27+
public class FirebaseMessagingClientTest
28+
{
29+
private static readonly GoogleCredential MockCredential =
30+
GoogleCredential.FromAccessToken("test-token");
31+
32+
[Fact]
33+
public void NoProjectId()
34+
{
35+
var clientFactory = new HttpClientFactory();
36+
Assert.Throws<FirebaseException>(
37+
() => new FirebaseMessagingClient(clientFactory, MockCredential, null));
38+
Assert.Throws<FirebaseException>(
39+
() => new FirebaseMessagingClient(clientFactory, MockCredential, string.Empty));
40+
}
41+
42+
[Fact]
43+
public void NoCredential()
44+
{
45+
var clientFactory = new HttpClientFactory();
46+
Assert.Throws<ArgumentNullException>(
47+
() => new FirebaseMessagingClient(clientFactory, null, "test-project"));
48+
}
49+
50+
[Fact]
51+
public void NoClientFactory()
52+
{
53+
var clientFactory = new HttpClientFactory();
54+
Assert.Throws<ArgumentNullException>(
55+
() => new FirebaseMessagingClient(null, MockCredential, "test-project"));
56+
}
57+
58+
[Fact]
59+
public async Task SendAsync()
60+
{
61+
var handler = new MockMessageHandler()
62+
{
63+
Response = new FirebaseMessagingClient.SendResponse()
64+
{
65+
Name = "test-response",
66+
},
67+
};
68+
var factory = new MockHttpClientFactory(handler);
69+
var client = new FirebaseMessagingClient(factory, MockCredential, "test-project");
70+
var message = new Message()
71+
{
72+
Topic = "test-topic",
73+
};
74+
var response = await client.SendAsync(message);
75+
Assert.Equal("test-response", response);
76+
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(
77+
handler.Request);
78+
Assert.Equal("test-topic", req.Message.Topic);
79+
Assert.False(req.ValidateOnly);
80+
Assert.Equal(1, handler.Calls);
81+
82+
// Send in dryRun mode.
83+
response = await client.SendAsync(message, dryRun: true);
84+
Assert.Equal("test-response", response);
85+
req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(
86+
handler.Request);
87+
Assert.Equal("test-topic", req.Message.Topic);
88+
Assert.True(req.ValidateOnly);
89+
Assert.Equal(2, handler.Calls);
90+
}
91+
92+
[Fact]
93+
public async Task HttpErrorAsync()
94+
{
95+
var handler = new MockMessageHandler()
96+
{
97+
StatusCode = HttpStatusCode.InternalServerError,
98+
Response = "not json",
99+
};
100+
var factory = new MockHttpClientFactory(handler);
101+
var client = new FirebaseMessagingClient(factory, MockCredential, "test-project");
102+
var message = new Message()
103+
{
104+
Topic = "test-topic",
105+
};
106+
var ex = await Assert.ThrowsAsync<FirebaseException>(
107+
async () => await client.SendAsync(message));
108+
Assert.Contains("not json", ex.Message);
109+
var req = JsonConvert.DeserializeObject<FirebaseMessagingClient.SendRequest>(
110+
handler.Request);
111+
Assert.Equal("test-topic", req.Message.Topic);
112+
Assert.False(req.ValidateOnly);
113+
Assert.Equal(1, handler.Calls);
114+
}
115+
}
116+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 System.Threading;
17+
using System.Threading.Tasks;
18+
using FirebaseAdmin.Tests;
19+
using Google.Apis.Auth.OAuth2;
20+
using Xunit;
21+
22+
namespace FirebaseAdmin.Messaging.Tests
23+
{
24+
public class FirebaseMessagingTest : IDisposable
25+
{
26+
private static readonly GoogleCredential MockCredential =
27+
GoogleCredential.FromFile("./resources/service_account.json");
28+
29+
[Fact]
30+
public void GetMessagingWithoutApp()
31+
{
32+
Assert.Null(FirebaseMessaging.DefaultInstance);
33+
}
34+
35+
[Fact]
36+
public void GetDefaultMessaging()
37+
{
38+
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
39+
FirebaseMessaging messaging = FirebaseMessaging.DefaultInstance;
40+
Assert.NotNull(messaging);
41+
Assert.Same(messaging, FirebaseMessaging.DefaultInstance);
42+
app.Delete();
43+
Assert.Null(FirebaseMessaging.DefaultInstance);
44+
}
45+
46+
[Fact]
47+
public void GetMessaging()
48+
{
49+
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential }, "MyApp");
50+
FirebaseMessaging messaging = FirebaseMessaging.GetMessaging(app);
51+
Assert.NotNull(messaging);
52+
Assert.Same(messaging, FirebaseMessaging.GetMessaging(app));
53+
app.Delete();
54+
Assert.Throws<InvalidOperationException>(() => FirebaseMessaging.GetMessaging(app));
55+
}
56+
57+
[Fact]
58+
public async Task UseAfterDelete()
59+
{
60+
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
61+
FirebaseMessaging messaging = FirebaseMessaging.DefaultInstance;
62+
app.Delete();
63+
await Assert.ThrowsAsync<ObjectDisposedException>(
64+
async () => await messaging.SendAsync(new Message() { Topic = "test-topic" }));
65+
}
66+
67+
[Fact]
68+
public async Task SendMessageCancel()
69+
{
70+
var cred = GoogleCredential.FromFile("./resources/service_account.json");
71+
FirebaseApp.Create(new AppOptions() { Credential = cred });
72+
var canceller = new CancellationTokenSource();
73+
canceller.Cancel();
74+
await Assert.ThrowsAsync<OperationCanceledException>(
75+
async () => await FirebaseMessaging.DefaultInstance.SendAsync(
76+
new Message() { Topic = "test-topic" }, canceller.Token));
77+
}
78+
79+
public void Dispose()
80+
{
81+
FirebaseApp.DeleteAll();
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)