Skip to content

Commit 53078df

Browse files
committed
Adding support for Mozilla Autopush Server return codes
1 parent fef97af commit 53078df

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/Lib.Net.Http.WebPush/PushServiceClient.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private bool ShouldRetryAfter(HttpResponseMessage pushMessageDeliveryRequestResp
389389

390390
private static async Task HandlePushMessageDeliveryRequestResponse(HttpResponseMessage pushMessageDeliveryRequestResponse, PushSubscription subscription)
391391
{
392-
if (pushMessageDeliveryRequestResponse.StatusCode == HttpStatusCode.Created)
392+
if (PushMessageDeliverySuccessful(pushMessageDeliveryRequestResponse.StatusCode))
393393
{
394394
return;
395395
}
@@ -402,6 +402,19 @@ private static async Task HandlePushMessageDeliveryRequestResponse(HttpResponseM
402402

403403
throw new PushServiceClientException(reason, pushMessageDeliveryRequestResponse.StatusCode, pushMessageDeliveryRequestResponse.Headers, content, subscription);
404404
}
405+
406+
private static bool PushMessageDeliverySuccessful(HttpStatusCode statusCode)
407+
{
408+
switch (statusCode)
409+
{
410+
case HttpStatusCode.Created: // RFC 8030
411+
case HttpStatusCode.OK: // Mozilla Autopush Server (Delivered to node client is connected to)
412+
case HttpStatusCode.Accepted: // Mozilla Autopush Server (Stored for delivery to client at a later time)
413+
return true;
414+
default:
415+
return false;
416+
}
417+
}
405418
#endregion
406419
}
407420
}

test/Test.Lib.Net.Http.WebPush/Functional/Infrastructure/FakePushServiceStartup.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,27 @@ public void Configure(IApplicationBuilder app)
2424
app.UseRouting()
2525
.UseEndpoints(endpoints =>
2626
{
27-
endpoints.MapPost("/push-created", context =>
27+
endpoints.MapPost("/push-rfc-8030-created", context =>
2828
{
2929
context.Response.StatusCode = StatusCodes.Status201Created;
3030

3131
return Task.CompletedTask;
3232
});
3333

34+
endpoints.MapPost("/mozilla-autopush-delivered", context =>
35+
{
36+
context.Response.StatusCode = StatusCodes.Status200OK;
37+
38+
return Task.CompletedTask;
39+
});
40+
41+
endpoints.MapPost("/mozilla-autopush-stored", context =>
42+
{
43+
context.Response.StatusCode = StatusCodes.Status202Accepted;
44+
45+
return Task.CompletedTask;
46+
});
47+
3448
endpoints.MapPost("/push-retry-after-once", context =>
3549
{
3650
if (shouldRetryAfter)

test/Test.Lib.Net.Http.WebPush/Functional/PushMessageDeliveryTests.cs

+16-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ namespace Test.Lib.Net.Http.WebPush.Functional
1111
public class PushMessageDeliveryTests : IClassFixture<FakePushServiceApplicationFactory>
1212
{
1313
#region Fields
14-
private const string CREATED_ENDPOINT = "http://localhost/push-created";
15-
private const string RETRY_AFTER_ONCE_ENDPOINT = "http://localhost/push-retry-after-once";
14+
private const string RFC_8030_CREATED_ENDPOINT = "http://localhost/push-rfc-8030-created";
15+
private const string MOZILLA_AUTOPUSH_DELIVERED_ENDPOINT = "http://localhost/mozilla-autopush-delivered";
16+
private const string MOZILLA_AUTOPUSH_STORED_ENDPOINT = "http://localhost/mozilla-autopush-stored";
17+
private const string RETRY_AFTER_ONCE_ENDPOINT = "http://localhost/push-retry-after-once";
1618
private const string RETRY_AFTER_ALWAYS_ENDPOINT = "http://localhost/push-retry-after-always";
1719
private const string CLIENT_ERROR_ENDPOINT = "http://localhost/push-client-error";
1820

@@ -56,10 +58,13 @@ private PushServiceClient PreparePushServiceClient()
5658
#endregion
5759

5860
#region Tests
59-
[Fact]
60-
public async Task PushService_NoError_DeliversPushMessage()
61+
[Theory]
62+
[InlineData(RFC_8030_CREATED_ENDPOINT)]
63+
[InlineData(MOZILLA_AUTOPUSH_DELIVERED_ENDPOINT)]
64+
[InlineData(MOZILLA_AUTOPUSH_STORED_ENDPOINT)]
65+
public async Task PushService_NoError_DeliversPushMessage(string endpoint)
6166
{
62-
_pushSubscription.Endpoint = CREATED_ENDPOINT;
67+
_pushSubscription.Endpoint = endpoint;
6368

6469
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
6570

@@ -217,8 +222,8 @@ public async Task PushService_PushEncryptionKeysNamesLowercase_DeliversPushMessa
217222
{ "auth", PUSH_SUBSCRIPTION_AUTH_KEY },
218223
{ "p256dh", PUSH_SUBSCRIPTION_P256DH_KEY }
219224
},
220-
Endpoint = CREATED_ENDPOINT
221-
};
225+
Endpoint = RFC_8030_CREATED_ENDPOINT
226+
};
222227

223228
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
224229

@@ -242,8 +247,8 @@ public async Task PushService_PushEncryptionKeysNamesUppercase_DeliversPushMessa
242247
{ "AUTH", PUSH_SUBSCRIPTION_AUTH_KEY },
243248
{ "P256DH", PUSH_SUBSCRIPTION_P256DH_KEY }
244249
},
245-
Endpoint = CREATED_ENDPOINT
246-
};
250+
Endpoint = RFC_8030_CREATED_ENDPOINT
251+
};
247252

248253
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
249254

@@ -267,8 +272,8 @@ public async Task PushService_PushEncryptionKeysNamesMixedCase_DeliversPushMessa
267272
{ "AuTh", PUSH_SUBSCRIPTION_AUTH_KEY },
268273
{ "P256dH", PUSH_SUBSCRIPTION_P256DH_KEY }
269274
},
270-
Endpoint = CREATED_ENDPOINT
271-
};
275+
Endpoint = RFC_8030_CREATED_ENDPOINT
276+
};
272277

273278
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
274279

test/Test.Lib.Net.Http.WebPush/Test.Lib.Net.Http.WebPush.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<IsPackable>false</IsPackable>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
8-
<PackageReference Include="xunit" Version="2.4.1" />
9-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
8+
<PackageReference Include="xunit" Version="2.9.2" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1010
<PrivateAssets>all</PrivateAssets>
1111
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1212
</PackageReference>

0 commit comments

Comments
 (0)