Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow http status code of 202 for response to SendNotification. #71 #72

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions WebPush.Test/WebPushClientTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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:[email protected]";

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);

Expand All @@ -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);
Expand All @@ -64,16 +69,13 @@ public void TestSetGcmApiKey()
[TestMethod]
public void TestSetGCMAPIKeyEmptyString()
{
var client = new WebPushClient();

Assert.ThrowsException<ArgumentException>(delegate { client.SetGcmApiKey(""); });
}

[TestMethod]
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);
Expand All @@ -87,8 +89,6 @@ public void TestSetGcmApiKeyNonGcmPushService()
[TestMethod]
public void TestSetGcmApiKeyNull()
{
var client = new WebPushClient();

client.SetGcmApiKey(@"somestring");
client.SetGcmApiKey(null);

Expand All @@ -102,9 +102,7 @@ public void TestSetGcmApiKeyNull()
[TestMethod]
public void TestSetVapidDetails()
{
var client = new WebPushClient();

client.SetVapidDetails("mailto:[email protected]", TestPublicKey, TestPrivateKey);
client.SetVapidDetails(TestSubject, TestPublicKey, TestPrivateKey);

var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey);
var message = client.GenerateRequestDetails(subscription, @"test payload");
Expand All @@ -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:[email protected]", 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<WebPushException>(() => 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");
}
Expand Down
3 changes: 2 additions & 1 deletion WebPush/WebPushClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down