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

Adding support for Mozilla Autopush Server return codes (Resolves #36) #37

Merged
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
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,19 +24,33 @@
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)
{
context.Response.StatusCode = 429;
context.Response.Headers.Add("Retry-After", "5");

Check warning on line 53 in test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs

View workflow job for this annotation

GitHub Actions / integration

Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key. (https://aka.ms/aspnet/analyzers)

Check warning on line 53 in test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs

View workflow job for this annotation

GitHub Actions / integration

Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key. (https://aka.ms/aspnet/analyzers)

shouldRetryAfter = false;
}
Expand All @@ -53,7 +67,7 @@
endpoints.MapPost("/push-retry-after-always", context =>
{
context.Response.StatusCode = 429;
context.Response.Headers.Add("Retry-After", "5");

Check warning on line 70 in test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs

View workflow job for this annotation

GitHub Actions / integration

Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key. (https://aka.ms/aspnet/analyzers)

Check warning on line 70 in test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs

View workflow job for this annotation

GitHub Actions / integration

Use IHeaderDictionary.Append or the indexer to append or set headers. IDictionary.Add will throw an ArgumentException when attempting to add a duplicate key. (https://aka.ms/aspnet/analyzers)

return Task.CompletedTask;
});
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
Loading