From 5c25f7d2ceda86c7b69582c5180e574cafd56a60 Mon Sep 17 00:00:00 2001 From: Joel McBeth Date: Fri, 6 Nov 2020 18:42:16 -0500 Subject: [PATCH] Allow http status code of 202 for response to SendNotification. #71 --- WebPush.Test/WebPushClientTest.cs | 62 +++++++++++++++++++------------ WebPush/WebPushClient.cs | 3 +- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/WebPush.Test/WebPushClientTest.cs b/WebPush.Test/WebPushClientTest.cs index b168d6c..4bc3a0f 100644 --- a/WebPush.Test/WebPushClientTest.cs +++ b/WebPush.Test/WebPushClientTest.cs @@ -1,12 +1,9 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using RichardSzalay.MockHttp; using System; using System.Collections.Generic; using System.Linq; using System.Net; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Moq; -using RichardSzalay.MockHttp; namespace WebPush.Test { @@ -23,11 +20,21 @@ public class WebPushClientTest private const string TestFcmEndpoint = @"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6"; + public const string TestSubject = "mailto:example@example.com"; + + private MockHttpMessageHandler httpMessageHandlerMock; + private WebPushClient client; + + [TestInitialize] + public void InitializeTest() + { + httpMessageHandlerMock = new MockHttpMessageHandler(); + client = new WebPushClient(httpMessageHandlerMock.ToHttpClient()); + } + [TestMethod] public void TestGcmApiKeyInOptions() { - var client = new WebPushClient(); - var gcmAPIKey = @"teststring"; var subscription = new PushSubscription(TestGcmEndpoint, TestPublicKey, TestPrivateKey); @@ -50,8 +57,6 @@ public void TestGcmApiKeyInOptions() [TestMethod] public void TestSetGcmApiKey() { - var client = new WebPushClient(); - var gcmAPIKey = @"teststring"; client.SetGcmApiKey(gcmAPIKey); var subscription = new PushSubscription(TestGcmEndpoint, TestPublicKey, TestPrivateKey); @@ -64,8 +69,6 @@ public void TestSetGcmApiKey() [TestMethod] public void TestSetGCMAPIKeyEmptyString() { - var client = new WebPushClient(); - Assert.ThrowsException(delegate { client.SetGcmApiKey(""); }); } @@ -73,7 +76,6 @@ public void TestSetGCMAPIKeyEmptyString() public void TestSetGcmApiKeyNonGcmPushService() { // Ensure that the API key doesn't get added on a service that doesn't accept it. - var client = new WebPushClient(); var gcmAPIKey = @"teststring"; client.SetGcmApiKey(gcmAPIKey); @@ -87,8 +89,6 @@ public void TestSetGcmApiKeyNonGcmPushService() [TestMethod] public void TestSetGcmApiKeyNull() { - var client = new WebPushClient(); - client.SetGcmApiKey(@"somestring"); client.SetGcmApiKey(null); @@ -102,9 +102,7 @@ public void TestSetGcmApiKeyNull() [TestMethod] public void TestSetVapidDetails() { - var client = new WebPushClient(); - - client.SetVapidDetails("mailto:example@example.com", TestPublicKey, TestPrivateKey); + client.SetVapidDetails(TestSubject, TestPublicKey, TestPrivateKey); var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey); var message = client.GenerateRequestDetails(subscription, @"test payload"); @@ -116,15 +114,33 @@ public void TestSetVapidDetails() } [TestMethod] - public void TestPassingHttpClient() + [DataRow(HttpStatusCode.Created)] + [DataRow(HttpStatusCode.Accepted)] + public void TestHandlingSuccessHttpCodes(HttpStatusCode status) { - var mockHttp = new MockHttpMessageHandler(); - mockHttp.When(TestFcmEndpoint).Respond(HttpStatusCode.Created); + TestSendNotification(status); + } - var client = new WebPushClient(mockHttp.ToHttpClient()); - client.SetVapidDetails("mailto:example@example.com", TestPublicKey, TestPrivateKey); + [TestMethod] + [DataRow(HttpStatusCode.BadRequest, "Bad Request")] + [DataRow(HttpStatusCode.RequestEntityTooLarge, "Payload too large")] + [DataRow((HttpStatusCode)429, "Too many request.")] + [DataRow(HttpStatusCode.NotFound, "Subscription no longer valid")] + [DataRow(HttpStatusCode.Gone, "Subscription no longer valid")] + [DataRow(HttpStatusCode.InternalServerError, "Received unexpected response code: 500")] + public void TestHandlingFailureHttpCodes(HttpStatusCode status, string expectedMessage) + { + var actual = Assert.ThrowsException(() => TestSendNotification(status)); - var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey); + Assert.AreEqual(expectedMessage, actual.Message); + } + + private void TestSendNotification(HttpStatusCode status) + { + var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey); ; + httpMessageHandlerMock.When(TestFcmEndpoint).Respond(status); + + client.SetVapidDetails(TestSubject, TestPublicKey, TestPrivateKey); client.SendNotification(subscription, "123"); } diff --git a/WebPush/WebPushClient.cs b/WebPush/WebPushClient.cs index 58828e1..547d1e6 100755 --- a/WebPush/WebPushClient.cs +++ b/WebPush/WebPushClient.cs @@ -348,7 +348,8 @@ public async Task SendNotificationAsync(PushSubscription subscription, string pa private static void HandleResponse(HttpResponseMessage response, PushSubscription subscription) { // Successful - if (response.StatusCode == HttpStatusCode.Created) + if (response.StatusCode == HttpStatusCode.Created || + response.StatusCode == HttpStatusCode.Accepted) { return; }