Skip to content

Commit 265a3cc

Browse files
committed
#60 Add GCM key when calling FCM endpoint if VapidDetails are not provided.
1 parent f444aa8 commit 265a3cc

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

WebPush.Test/WebPushClientTest.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Net;
7+
using System.Net.Http;
78

89
namespace WebPush.Test
910
{
@@ -20,6 +21,9 @@ public class WebPushClientTest
2021
private const string TestFcmEndpoint =
2122
@"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6";
2223

24+
private const string TestFirefoxEndpoint =
25+
@"https://updates.push.services.mozilla.com/wpush/v2/gBABAABgOe_sGrdrsT35ljtA4O9xCX";
26+
2327
public const string TestSubject = "mailto:[email protected]";
2428

2529
private MockHttpMessageHandler httpMessageHandlerMock;
@@ -76,10 +80,9 @@ public void TestSetGCMAPIKeyEmptyString()
7680
public void TestSetGcmApiKeyNonGcmPushService()
7781
{
7882
// Ensure that the API key doesn't get added on a service that doesn't accept it.
79-
8083
var gcmAPIKey = @"teststring";
8184
client.SetGcmApiKey(gcmAPIKey);
82-
var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey);
85+
var subscription = new PushSubscription(TestFirefoxEndpoint, TestPublicKey, TestPrivateKey);
8386
var message = client.GenerateRequestDetails(subscription, @"test payload");
8487

8588
IEnumerable<string> values;
@@ -103,8 +106,8 @@ public void TestSetGcmApiKeyNull()
103106
public void TestSetVapidDetails()
104107
{
105108
client.SetVapidDetails(TestSubject, TestPublicKey, TestPrivateKey);
106-
107-
var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey);
109+
110+
var subscription = new PushSubscription(TestFirefoxEndpoint, TestPublicKey, TestPrivateKey);
108111
var message = client.GenerateRequestDetails(subscription, @"test payload");
109112
var authorizationHeader = message.Headers.GetValues(@"Authorization").First();
110113
var cryptoHeader = message.Headers.GetValues(@"Crypto-Key").First();
@@ -113,6 +116,17 @@ public void TestSetVapidDetails()
113116
Assert.IsTrue(cryptoHeader.Contains(@"p256ecdsa"));
114117
}
115118

119+
[TestMethod]
120+
public void TestFcmAddsAuthorizationHeader()
121+
{
122+
client.SetGcmApiKey(@"somestring");
123+
var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey);
124+
var message = client.GenerateRequestDetails(subscription, @"test payload");
125+
var authorizationHeader = message.Headers.GetValues(@"Authorization").First();
126+
127+
Assert.IsTrue(authorizationHeader.StartsWith(@"key="));
128+
}
129+
116130
[TestMethod]
117131
[DataRow(HttpStatusCode.Created)]
118132
[DataRow(HttpStatusCode.Accepted)]

WebPush/WebPushClient.cs

+5
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
218218
}
219219

220220
var isGcm = subscription.Endpoint.StartsWith(@"https://android.googleapis.com/gcm/send");
221+
var isFcm = subscription.Endpoint.StartsWith(@"https://fcm.googleapis.com/fcm/send/");
222+
221223
if (isGcm)
222224
{
223225
if (!string.IsNullOrEmpty(currentGcmApiKey))
@@ -241,6 +243,9 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
241243
{
242244
cryptoKeyHeader += @";" + vapidHeaders["Crypto-Key"];
243245
}
246+
} else if (isFcm && !string.IsNullOrEmpty(currentGcmApiKey))
247+
{
248+
request.Headers.TryAddWithoutValidation("Authorization", "key=" + currentGcmApiKey);
244249
}
245250

246251
request.Headers.Add("Crypto-Key", cryptoKeyHeader);

0 commit comments

Comments
 (0)