|
| 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 | +} |
0 commit comments