From 9a5524c03d462347d79bd2b67efcb10efbb6be47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C4=99czek?= Date: Thu, 24 Oct 2024 12:31:10 +0200 Subject: [PATCH] Adding support for Mozilla Autopush Server return codes --- src/Lib.Net.Http.WebPush/PushServiceClient.cs | 15 ++++++++++- .../Infrastructure/FakePushServiceStartup.cs | 16 ++++++++++- .../Functional/PushMessageDeliveryTests.cs | 27 +++++++++++-------- .../Test.Lib.Net.Http.WebPush.csproj | 6 ++--- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/Lib.Net.Http.WebPush/PushServiceClient.cs b/src/Lib.Net.Http.WebPush/PushServiceClient.cs index 5804a42..9eac86d 100644 --- a/src/Lib.Net.Http.WebPush/PushServiceClient.cs +++ b/src/Lib.Net.Http.WebPush/PushServiceClient.cs @@ -389,7 +389,7 @@ private bool ShouldRetryAfter(HttpResponseMessage pushMessageDeliveryRequestResp private static async Task HandlePushMessageDeliveryRequestResponse(HttpResponseMessage pushMessageDeliveryRequestResponse, PushSubscription subscription) { - if (pushMessageDeliveryRequestResponse.StatusCode == HttpStatusCode.Created) + if (PushMessageDeliverySuccessful(pushMessageDeliveryRequestResponse.StatusCode)) { return; } @@ -402,6 +402,19 @@ private static async Task HandlePushMessageDeliveryRequestResponse(HttpResponseM throw new PushServiceClientException(reason, pushMessageDeliveryRequestResponse.StatusCode, pushMessageDeliveryRequestResponse.Headers, content, subscription); } + + private static bool PushMessageDeliverySuccessful(HttpStatusCode statusCode) + { + switch (statusCode) + { + case HttpStatusCode.Created: // RFC 8030 + case HttpStatusCode.OK: // Mozilla Autopush Server (Delivered to node client is connected to) + case HttpStatusCode.Accepted: // Mozilla Autopush Server (Stored for delivery to client at a later time) + return true; + default: + return false; + } + } #endregion } } diff --git a/test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs b/test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs index de8fd4b..26c1836 100644 --- a/test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs +++ b/test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs @@ -24,13 +24,27 @@ public void Configure(IApplicationBuilder app) app.UseRouting() .UseEndpoints(endpoints => { - endpoints.MapPost("/push-created", context => + endpoints.MapPost("/push-rfc-8030-created", context => { context.Response.StatusCode = StatusCodes.Status201Created; return Task.CompletedTask; }); + endpoints.MapPost("/mozilla-autopush-delivered", context => + { + context.Response.StatusCode = StatusCodes.Status200OK; + + return Task.CompletedTask; + }); + + endpoints.MapPost("/mozilla-autopush-stored", context => + { + context.Response.StatusCode = StatusCodes.Status202Accepted; + + return Task.CompletedTask; + }); + endpoints.MapPost("/push-retry-after-once", context => { if (shouldRetryAfter) diff --git a/test/Test.Lib.Net.Http.WebPush/Functional/PushMessageDeliveryTests.cs b/test/Test.Lib.Net.Http.WebPush/Functional/PushMessageDeliveryTests.cs index 35f91be..8204432 100644 --- a/test/Test.Lib.Net.Http.WebPush/Functional/PushMessageDeliveryTests.cs +++ b/test/Test.Lib.Net.Http.WebPush/Functional/PushMessageDeliveryTests.cs @@ -11,8 +11,10 @@ namespace Test.Lib.Net.Http.WebPush.Functional public class PushMessageDeliveryTests : IClassFixture { #region Fields - private const string CREATED_ENDPOINT = "http://localhost/push-created"; - private const string RETRY_AFTER_ONCE_ENDPOINT = "http://localhost/push-retry-after-once"; + private const string RFC_8030_CREATED_ENDPOINT = "http://localhost/push-rfc-8030-created"; + private const string MOZILLA_AUTOPUSH_DELIVERED_ENDPOINT = "http://localhost/mozilla-autopush-delivered"; + private const string MOZILLA_AUTOPUSH_STORED_ENDPOINT = "http://localhost/mozilla-autopush-stored"; + private const string RETRY_AFTER_ONCE_ENDPOINT = "http://localhost/push-retry-after-once"; private const string RETRY_AFTER_ALWAYS_ENDPOINT = "http://localhost/push-retry-after-always"; private const string CLIENT_ERROR_ENDPOINT = "http://localhost/push-client-error"; @@ -56,10 +58,13 @@ private PushServiceClient PreparePushServiceClient() #endregion #region Tests - [Fact] - public async Task PushService_NoError_DeliversPushMessage() + [Theory] + [InlineData(RFC_8030_CREATED_ENDPOINT)] + [InlineData(MOZILLA_AUTOPUSH_DELIVERED_ENDPOINT)] + [InlineData(MOZILLA_AUTOPUSH_STORED_ENDPOINT)] + public async Task PushService_NoError_DeliversPushMessage(string endpoint) { - _pushSubscription.Endpoint = CREATED_ENDPOINT; + _pushSubscription.Endpoint = endpoint; PushMessage pushMessage = new PushMessage(WALRUS_CONTENT); @@ -217,8 +222,8 @@ public async Task PushService_PushEncryptionKeysNamesLowercase_DeliversPushMessa { "auth", PUSH_SUBSCRIPTION_AUTH_KEY }, { "p256dh", PUSH_SUBSCRIPTION_P256DH_KEY } }, - Endpoint = CREATED_ENDPOINT - }; + Endpoint = RFC_8030_CREATED_ENDPOINT + }; PushMessage pushMessage = new PushMessage(WALRUS_CONTENT); @@ -242,8 +247,8 @@ public async Task PushService_PushEncryptionKeysNamesUppercase_DeliversPushMessa { "AUTH", PUSH_SUBSCRIPTION_AUTH_KEY }, { "P256DH", PUSH_SUBSCRIPTION_P256DH_KEY } }, - Endpoint = CREATED_ENDPOINT - }; + Endpoint = RFC_8030_CREATED_ENDPOINT + }; PushMessage pushMessage = new PushMessage(WALRUS_CONTENT); @@ -267,8 +272,8 @@ public async Task PushService_PushEncryptionKeysNamesMixedCase_DeliversPushMessa { "AuTh", PUSH_SUBSCRIPTION_AUTH_KEY }, { "P256dH", PUSH_SUBSCRIPTION_P256DH_KEY } }, - Endpoint = CREATED_ENDPOINT - }; + Endpoint = RFC_8030_CREATED_ENDPOINT + }; PushMessage pushMessage = new PushMessage(WALRUS_CONTENT); diff --git a/test/Test.Lib.Net.Http.WebPush/Test.Lib.Net.Http.WebPush.csproj b/test/Test.Lib.Net.Http.WebPush/Test.Lib.Net.Http.WebPush.csproj index 5e743c8..a445f57 100644 --- a/test/Test.Lib.Net.Http.WebPush/Test.Lib.Net.Http.WebPush.csproj +++ b/test/Test.Lib.Net.Http.WebPush/Test.Lib.Net.Http.WebPush.csproj @@ -4,9 +4,9 @@ false - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive