|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | using System; |
| 16 | +using System.Linq; |
16 | 17 | using System.Net; |
17 | 18 | using System.Threading.Tasks; |
18 | 19 | using FirebaseAdmin.Tests; |
@@ -59,7 +60,7 @@ public async Task SendAsync() |
59 | 60 | { |
60 | 61 | var handler = new MockMessageHandler() |
61 | 62 | { |
62 | | - Response = new FirebaseMessagingClient.SendResponse() |
| 63 | + Response = new FirebaseMessagingClient.SingleMessageResponse() |
63 | 64 | { |
64 | 65 | Name = "test-response", |
65 | 66 | }, |
@@ -147,6 +148,103 @@ public async Task SendAllAsync() |
147 | 148 | Assert.Equal("projects/fir-adminintegrationtests/messages/5903525881088369386", response.Responses[1].MessageId); |
148 | 149 | } |
149 | 150 |
|
| 151 | + [Fact] |
| 152 | + public async Task SendAllAsyncWithError() |
| 153 | + { |
| 154 | + var rawResponse = @" |
| 155 | +--batch_test-boundary |
| 156 | +Content-Type: application/http |
| 157 | +Content-ID: response- |
| 158 | +
|
| 159 | +HTTP/1.1 200 OK |
| 160 | +Content-Type: application/json; charset=UTF-8 |
| 161 | +Vary: Origin |
| 162 | +Vary: X-Origin |
| 163 | +Vary: Referer |
| 164 | +
|
| 165 | +{ |
| 166 | + ""name"": ""projects/fir-adminintegrationtests/messages/8580920590356323124"" |
| 167 | +} |
| 168 | +
|
| 169 | +--batch_test-boundary |
| 170 | +Content-Type: application/http |
| 171 | +Content-ID: response- |
| 172 | +
|
| 173 | +HTTP/1.1 400 Bad Request |
| 174 | +Content-Type: application/json; charset=UTF-8 |
| 175 | +Vary: Origin |
| 176 | +Vary: X-Origin |
| 177 | +Vary: Referer |
| 178 | +
|
| 179 | +{ |
| 180 | + ""error"": { |
| 181 | + ""code"": 400, |
| 182 | + ""message"": ""The registration token is not a valid FCM registration token"", |
| 183 | + ""errors"": [ |
| 184 | + { |
| 185 | + ""message"": ""The registration token is not a valid FCM registration token"", |
| 186 | + ""domain"": ""global"", |
| 187 | + ""reason"": ""badRequest"" |
| 188 | + } |
| 189 | + ], |
| 190 | + ""status"": ""INVALID_ARGUMENT"" |
| 191 | + } |
| 192 | +} |
| 193 | +
|
| 194 | +--batch_test-boundary |
| 195 | +"; |
| 196 | + var handler = new MockMessageHandler() |
| 197 | + { |
| 198 | + Response = rawResponse, |
| 199 | + ApplyContentHeaders = (headers) => |
| 200 | + { |
| 201 | + headers.Remove("Content-Type"); |
| 202 | + headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=batch_test-boundary"); |
| 203 | + }, |
| 204 | + }; |
| 205 | + var factory = new MockHttpClientFactory(handler); |
| 206 | + var client = new FirebaseMessagingClient(factory, MockCredential, "test-project"); |
| 207 | + var message1 = new Message() |
| 208 | + { |
| 209 | + Token = "test-token1", |
| 210 | + }; |
| 211 | + var message2 = new Message() |
| 212 | + { |
| 213 | + Token = "test-token2", |
| 214 | + }; |
| 215 | + var response = await client.SendAllAsync(new[] { message1, message2 }); |
| 216 | + Assert.Equal(1, response.SuccessCount); |
| 217 | + Assert.Equal(1, response.FailureCount); |
| 218 | + Assert.Equal("projects/fir-adminintegrationtests/messages/8580920590356323124", response.Responses[0].MessageId); |
| 219 | + Assert.NotNull(response.Responses[1].Exception); |
| 220 | + } |
| 221 | + |
| 222 | + [Fact] |
| 223 | + public async Task SendAllAsyncNullList() |
| 224 | + { |
| 225 | + var factory = new MockHttpClientFactory(new MockMessageHandler()); |
| 226 | + var client = new FirebaseMessagingClient(factory, MockCredential, "test-project"); |
| 227 | + |
| 228 | + await Assert.ThrowsAsync<ArgumentNullException>(() => client.SendAllAsync(null)); |
| 229 | + } |
| 230 | + |
| 231 | + [Fact] |
| 232 | + public async Task SendAllAsyncWithNoMessages() |
| 233 | + { |
| 234 | + var factory = new MockHttpClientFactory(new MockMessageHandler()); |
| 235 | + var client = new FirebaseMessagingClient(factory, MockCredential, "test-project"); |
| 236 | + await Assert.ThrowsAsync<ArgumentException>(() => client.SendAllAsync(Enumerable.Empty<Message>())); |
| 237 | + } |
| 238 | + |
| 239 | + [Fact] |
| 240 | + public async Task SendAllAsyncWithTooManyMessages() |
| 241 | + { |
| 242 | + var factory = new MockHttpClientFactory(new MockMessageHandler()); |
| 243 | + var client = new FirebaseMessagingClient(factory, MockCredential, "test-project"); |
| 244 | + var messages = Enumerable.Range(0, 101).Select(_ => new Message { Topic = "test-topic" }); |
| 245 | + await Assert.ThrowsAsync<ArgumentException>(() => client.SendAllAsync(messages)); |
| 246 | + } |
| 247 | + |
150 | 248 | [Fact] |
151 | 249 | public async Task HttpErrorAsync() |
152 | 250 | { |
|
0 commit comments