public void TestSetGCMAPIKey() { WebPushClient client = new WebPushClient(); string gcmAPIKey = @"teststring"; client.SetGCMAPIKey(gcmAPIKey); PushSubscription subscription = new PushSubscription(TEST_GCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload"); string authorizationHeader = message.Headers.GetValues("Authorization").First(); Assert.AreEqual("key=" + gcmAPIKey, authorizationHeader); }
public void TestSetGcmApiKey() { var client = new WebPushClient(); var gcmAPIKey = @"teststring"; client.SetGcmApiKey(gcmAPIKey); var subscription = new PushSubscription(TestGcmEndpoint, TestPublicKey, TestPrivateKey); var message = client.GenerateRequestDetails(subscription, @"test payload"); var authorizationHeader = message.Headers.GetValues(@"Authorization").First(); Assert.Equal(@"key=" + gcmAPIKey, authorizationHeader); }
public void TestSetGCMAPIKeyNull() { WebPushClient client = new WebPushClient(); client.SetGCMAPIKey(@"somestring"); client.SetGCMAPIKey(null); PushSubscription subscription = new PushSubscription(TEST_GCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload"); IEnumerable <string> values; Assert.False(message.Headers.TryGetValues("Authorization", out values)); }
public void TestSetVapidDetails() { WebPushClient client = new WebPushClient(); client.SetVapidDetails("mailto:[email protected]", TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); PushSubscription subscription = new PushSubscription(TEST_FCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload"); string authorizationHeader = message.Headers.GetValues("Authorization").First(); string cryptoHeader = message.Headers.GetValues("Crypto-Key").First(); Assert.True(authorizationHeader.StartsWith("WebPush ")); Assert.True(cryptoHeader.Contains("p256ecdsa")); }
public void TestSetGcmApiKeyNull() { var client = new WebPushClient(); client.SetGcmApiKey(@"somestring"); client.SetGcmApiKey(null); var subscription = new PushSubscription(TestGcmEndpoint, TestPublicKey, TestPrivateKey); var message = client.GenerateRequestDetails(subscription, @"test payload"); IEnumerable <string> values; Assert.False(message.Headers.TryGetValues("Authorization", out values)); }
public void TestSetVapidDetails() { var client = new WebPushClient(); client.SetVapidDetails("mailto:[email protected]", TestPublicKey, TestPrivateKey); var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey); var message = client.GenerateRequestDetails(subscription, @"test payload"); var authorizationHeader = message.Headers.GetValues(@"Authorization").First(); var cryptoHeader = message.Headers.GetValues(@"Crypto-Key").First(); Assert.True(authorizationHeader.StartsWith(@"WebPush ")); Assert.True(cryptoHeader.Contains(@"p256ecdsa")); }
public void TestSetGCMAPiKeyNonGCMPushService() { // Ensure that the API key doesn't get added on a service that doesn't accept it. WebPushClient client = new WebPushClient(); string gcmAPIKey = @"teststring"; client.SetGCMAPIKey(gcmAPIKey); PushSubscription subscription = new PushSubscription(TEST_FCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload"); IEnumerable <string> values; Assert.False(message.Headers.TryGetValues("Authorization", out values)); }
public void TestGCMAPIKeyInOptions() { WebPushClient client = new WebPushClient(); string gcmAPIKey = @"teststring"; PushSubscription subscription = new PushSubscription(TEST_GCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); Dictionary <string, object> options = new Dictionary <string, object>(); options["gcmAPIKey"] = gcmAPIKey; HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload", options); string authorizationHeader = message.Headers.GetValues("Authorization").First(); Assert.AreEqual("key=" + gcmAPIKey, authorizationHeader); // Test previous incorrect casing of gcmAPIKey Dictionary <string, object> options2 = new Dictionary <string, object>(); options2["gcmApiKey"] = gcmAPIKey; Assert.Throws <ArgumentException>(delegate { client.GenerateRequestDetails(subscription, "test payload", options2); }); }
public void TestSetGcmApiKeyNonGcmPushService() { // Ensure that the API key doesn't get added on a service that doesn't accept it. var client = new WebPushClient(); var gcmAPIKey = @"teststring"; client.SetGcmApiKey(gcmAPIKey); var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey); var message = client.GenerateRequestDetails(subscription, @"test payload"); IEnumerable <string> values; Assert.False(message.Headers.TryGetValues(@"Authorization", out values)); }
public void TestGcmApiKeyInOptions() { var client = new WebPushClient(); var gcmAPIKey = @"teststring"; var subscription = new PushSubscription(TestGcmEndpoint, TestPublicKey, TestPrivateKey); var options = new Dictionary <string, object>(); options[@"gcmAPIKey"] = gcmAPIKey; var message = client.GenerateRequestDetails(subscription, @"test payload", options); var authorizationHeader = message.Headers.GetValues(@"Authorization").First(); Assert.Equal("key=" + gcmAPIKey, authorizationHeader); // Test previous incorrect casing of gcmAPIKey var options2 = new Dictionary <string, object>(); options2[@"gcmApiKey"] = gcmAPIKey; Assert.Throws <ArgumentException>(delegate { client.GenerateRequestDetails(subscription, "test payload", options2); }); }
public async Task <HttpResponseMessage> SendNotificationAsync(PushSubscription subscription, string payload, WebPushOptions options = null) { options = options ?? WebPushOptions.Defaults; var client = new WebPushClient(); var httpClient = httpClientFactory.CreateClient(); client.SetVapidDetails(vapidAuthenticationOptions.Subject, vapidAuthenticationOptions.PublicKey, vapidAuthenticationOptions.PrivateKey); var request = client.GenerateRequestDetails(new WebPush.PushSubscription() { Auth = subscription.Auth, Endpoint = subscription.Endpoint.ToString(), P256DH = subscription.P256dh }, payload, new Dictionary <string, object>() { { "TTL", (int)options.TimeToLive.TotalSeconds } }); return(await httpClient.SendAsync(request)); }