Skip to content

Commit

Permalink
Adding support for Mozilla Autopush Server return codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeczek committed Oct 24, 2024
1 parent fef97af commit 53078df
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
15 changes: 14 additions & 1 deletion src/Lib.Net.Http.WebPush/PushServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ namespace Test.Lib.Net.Http.WebPush.Functional
public class PushMessageDeliveryTests : IClassFixture<FakePushServiceApplicationFactory>
{
#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";

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 53078df

Please sign in to comment.