|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using FirebaseAdmin.Messaging; |
| 6 | +using Google.Apis.Auth.OAuth2; |
| 7 | +using Google.Apis.Http; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace FirebaseAdmin.Tests.Messaging |
| 11 | +{ |
| 12 | + public class InstanceIdClientTest |
| 13 | + { |
| 14 | + private static readonly GoogleCredential MockCredential = |
| 15 | + GoogleCredential.FromAccessToken("test-token"); |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void NoCredential() |
| 19 | + { |
| 20 | + var clientFactory = new HttpClientFactory(); |
| 21 | + Assert.Throws<ArgumentNullException>( |
| 22 | + () => new InstanceIdClient(clientFactory, null)); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void NoClientFactory() |
| 27 | + { |
| 28 | + var clientFactory = new HttpClientFactory(); |
| 29 | + Assert.Throws<ArgumentNullException>( |
| 30 | + () => new InstanceIdClient(null, MockCredential)); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task SubscribeToTopicAsync() |
| 35 | + { |
| 36 | + var handler = new MockMessageHandler() |
| 37 | + { |
| 38 | + Response = @"{""results"":[{}]}", |
| 39 | + }; |
| 40 | + var factory = new MockHttpClientFactory(handler); |
| 41 | + |
| 42 | + var client = new InstanceIdClient(factory, MockCredential); |
| 43 | + |
| 44 | + var result = await client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" }); |
| 45 | + |
| 46 | + Assert.Equal(1, result.SuccessCount); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task UnsubscribeFromTopicAsync() |
| 51 | + { |
| 52 | + var handler = new MockMessageHandler() |
| 53 | + { |
| 54 | + Response = @"{""results"":[{}]}", |
| 55 | + }; |
| 56 | + var factory = new MockHttpClientFactory(handler); |
| 57 | + |
| 58 | + var client = new InstanceIdClient(factory, MockCredential); |
| 59 | + |
| 60 | + var result = await client.UnsubscribeFromTopicAsync("test-topic", new List<string> { "abc123" }); |
| 61 | + |
| 62 | + Assert.Equal(1, result.SuccessCount); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task BadRequest() |
| 67 | + { |
| 68 | + var handler = new MockMessageHandler() |
| 69 | + { |
| 70 | + StatusCode = HttpStatusCode.BadRequest, |
| 71 | + Response = "BadRequest", |
| 72 | + }; |
| 73 | + var factory = new MockHttpClientFactory(handler); |
| 74 | + |
| 75 | + var client = new InstanceIdClient(factory, MockCredential); |
| 76 | + |
| 77 | + var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( |
| 78 | + () => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); |
| 79 | + |
| 80 | + Assert.Equal(ErrorCode.InvalidArgument, exception.ErrorCode); |
| 81 | + Assert.Equal("Unexpected HTTP response with status: 400 (BadRequest)\nBadRequest", exception.Message); |
| 82 | + Assert.Null(exception.MessagingErrorCode); |
| 83 | + Assert.NotNull(exception.HttpResponse); |
| 84 | + Assert.Null(exception.InnerException); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public async Task Unauthorized() |
| 89 | + { |
| 90 | + var handler = new MockMessageHandler() |
| 91 | + { |
| 92 | + StatusCode = HttpStatusCode.Unauthorized, |
| 93 | + Response = "Unauthorized", |
| 94 | + }; |
| 95 | + var factory = new MockHttpClientFactory(handler); |
| 96 | + |
| 97 | + var client = new InstanceIdClient(factory, MockCredential); |
| 98 | + |
| 99 | + var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( |
| 100 | + () => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); |
| 101 | + |
| 102 | + Assert.Equal(ErrorCode.Unauthenticated, exception.ErrorCode); |
| 103 | + Assert.Equal("Unexpected HTTP response with status: 401 (Unauthorized)\nUnauthorized", exception.Message); |
| 104 | + Assert.Null(exception.MessagingErrorCode); |
| 105 | + Assert.NotNull(exception.HttpResponse); |
| 106 | + Assert.Null(exception.InnerException); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public async Task Forbidden() |
| 111 | + { |
| 112 | + var handler = new MockMessageHandler() |
| 113 | + { |
| 114 | + StatusCode = HttpStatusCode.Forbidden, |
| 115 | + Response = "Forbidden", |
| 116 | + }; |
| 117 | + var factory = new MockHttpClientFactory(handler); |
| 118 | + |
| 119 | + var client = new InstanceIdClient(factory, MockCredential); |
| 120 | + |
| 121 | + var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( |
| 122 | + () => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); |
| 123 | + |
| 124 | + Assert.Equal(ErrorCode.PermissionDenied, exception.ErrorCode); |
| 125 | + Assert.Equal("Unexpected HTTP response with status: 403 (Forbidden)\nForbidden", exception.Message); |
| 126 | + Assert.Null(exception.MessagingErrorCode); |
| 127 | + Assert.NotNull(exception.HttpResponse); |
| 128 | + Assert.Null(exception.InnerException); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public async Task NotFound() |
| 133 | + { |
| 134 | + var handler = new MockMessageHandler() |
| 135 | + { |
| 136 | + StatusCode = HttpStatusCode.NotFound, |
| 137 | + Response = "NotFound", |
| 138 | + }; |
| 139 | + var factory = new MockHttpClientFactory(handler); |
| 140 | + |
| 141 | + var client = new InstanceIdClient(factory, MockCredential); |
| 142 | + |
| 143 | + var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( |
| 144 | + () => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); |
| 145 | + |
| 146 | + Assert.Equal(ErrorCode.NotFound, exception.ErrorCode); |
| 147 | + Assert.Equal("Unexpected HTTP response with status: 404 (NotFound)\nNotFound", exception.Message); |
| 148 | + Assert.Null(exception.MessagingErrorCode); |
| 149 | + Assert.NotNull(exception.HttpResponse); |
| 150 | + Assert.Null(exception.InnerException); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public async Task ServiceUnavailable() |
| 155 | + { |
| 156 | + var handler = new MockMessageHandler() |
| 157 | + { |
| 158 | + StatusCode = HttpStatusCode.ServiceUnavailable, |
| 159 | + Response = "ServiceUnavailable", |
| 160 | + }; |
| 161 | + var factory = new MockHttpClientFactory(handler); |
| 162 | + |
| 163 | + var client = new InstanceIdClient(factory, MockCredential); |
| 164 | + |
| 165 | + var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( |
| 166 | + () => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); |
| 167 | + |
| 168 | + Assert.Equal(ErrorCode.Unavailable, exception.ErrorCode); |
| 169 | + Assert.Equal("Unexpected HTTP response with status: 503 (ServiceUnavailable)\nServiceUnavailable", exception.Message); |
| 170 | + Assert.Null(exception.MessagingErrorCode); |
| 171 | + Assert.NotNull(exception.HttpResponse); |
| 172 | + Assert.Null(exception.InnerException); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments