Skip to content

Commit 0d45e05

Browse files
author
Cory Thompson
committed
#4 Inconsistent casing of gcmAPIKey in GenerateRequestDetails
1 parent 90d80df commit 0d45e05

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ var privateKey = @"mryM-krWj_6IsIMGsd8wNFXGBxnx...............";
4040

4141
var subscription = new PushSubscription(pushEndpoint, p256dh, auth);
4242
var vapidDetails = new VapidDetails(subject, publicKey, privateKey);
43-
//var gcmApiKey = @"[your key here]";
43+
//var gcmAPIKey = @"[your key here]";
4444
4545
var webPushClient = new WebPushClient();
4646
try
4747
{
4848
webPushClient.SendNotification(subscription, "payload", vapidDetails);
49-
//webPushClient.SendNotification(subscription, "payload", gcmApiKey);
49+
//webPushClient.SendNotification(subscription, "payload", gcmAPIKey);
5050
}
5151
catch (WebPushException exception)
5252
{
@@ -56,7 +56,7 @@ catch (WebPushException exception)
5656

5757
# API Reference
5858

59-
## SendNotification(pushSubscription, payload, vapidDetails|gcmApiKey|options)
59+
## SendNotification(pushSubscription, payload, vapidDetails|gcmAPIKey|options)
6060

6161
```csharp
6262
var subscription = new PushSubscription(pushEndpoint, p256dh, auth);

src/WebPushClient.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
122122
extraHeaders = headers;
123123
}
124124

125-
if (options.ContainsKey("gcmApiKey"))
125+
if (options.ContainsKey("gcmAPIKey"))
126126
{
127-
string gcmApiKey = options["gcmApiKey"] as string;
128-
if (gcmApiKey == null)
127+
string gcmAPIKey = options["gcmAPIKey"] as string;
128+
if (gcmAPIKey == null)
129129
{
130-
throw new ArgumentException("options.gcmApiKey must be of type string");
130+
throw new ArgumentException("options.gcmAPIKey must be of type string");
131131
}
132132

133-
currentGCMAPiKey = gcmApiKey;
133+
currentGCMAPiKey = gcmAPIKey;
134134
}
135135

136136
if (options.ContainsKey("vapidDetails"))
@@ -259,11 +259,11 @@ public void SendNotification(PushSubscription subscription, string payload, Vapi
259259
/// </summary>
260260
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
261261
/// <param name="payload">The payload you wish to send to the user</param>
262-
/// <param name="gcmApiKey">The GCM API key</param>
263-
public void SendNotification(PushSubscription subscription, string payload, string gcmApiKey)
262+
/// <param name="gcmAPIKey">The GCM API key</param>
263+
public void SendNotification(PushSubscription subscription, string payload, string gcmAPIKey)
264264
{
265265
Dictionary<string, object> options = new Dictionary<string, object>();
266-
options["gcmApiKey"] = gcmApiKey;
266+
options["gcmAPIKey"] = gcmAPIKey;
267267
SendNotification(subscription, payload, options);
268268
}
269269
}

test/WebPushClientTest.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,37 @@ public void TestSetGCMAPIKey()
2828
{
2929
WebPushClient client = new WebPushClient();
3030

31-
string gcmApiKey = @"teststring";
32-
client.SetGCMAPIKey(gcmApiKey);
31+
string gcmAPIKey = @"teststring";
32+
client.SetGCMAPIKey(gcmAPIKey);
3333
PushSubscription subscription = new PushSubscription(TEST_GCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY);
3434
HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload");
3535
string authorizationHeader = message.Headers.GetValues("Authorization").First();
3636

37-
Assert.AreEqual("key=" + gcmApiKey, authorizationHeader);
37+
Assert.AreEqual("key=" + gcmAPIKey, authorizationHeader);
38+
}
39+
40+
[Test]
41+
public void TestGCMAPIKeyInOptions()
42+
{
43+
WebPushClient client = new WebPushClient();
44+
45+
string gcmAPIKey = @"teststring";
46+
PushSubscription subscription = new PushSubscription(TEST_GCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY);
47+
48+
Dictionary<string, object> options = new Dictionary<string, object>();
49+
options["gcmAPIKey"] = gcmAPIKey;
50+
HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload", options);
51+
string authorizationHeader = message.Headers.GetValues("Authorization").First();
52+
53+
Assert.AreEqual("key=" + gcmAPIKey, authorizationHeader);
54+
55+
// Test previous incorrect casing of gcmAPIKey
56+
Dictionary<string, object> options2 = new Dictionary<string, object>();
57+
options2["gcmApiKey"] = gcmAPIKey;
58+
Assert.Throws<ArgumentException>(delegate
59+
{
60+
client.GenerateRequestDetails(subscription, "test payload", options2);
61+
});
3862
}
3963

4064
[Test]
@@ -69,8 +93,8 @@ public void TestSetGCMAPiKeyNonGCMPushService()
6993
// Ensure that the API key doesn't get added on a service that doesn't accept it.
7094
WebPushClient client = new WebPushClient();
7195

72-
string gcmApiKey = @"teststring";
73-
client.SetGCMAPIKey(gcmApiKey);
96+
string gcmAPIKey = @"teststring";
97+
client.SetGCMAPIKey(gcmAPIKey);
7498
PushSubscription subscription = new PushSubscription(TEST_FCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY);
7599
HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload");
76100

0 commit comments

Comments
 (0)