Esempio n. 1
0
        public void Adding_Voip_To_NonVoip_Type_Throws_InvalidOperationException()
        {
            var backgroundPush = ApplePush.CreateContentAvailable();
            var alert          = ApplePush.CreateContentAvailable();

            Assert.Throws <InvalidOperationException>(() => backgroundPush.AddVoipToken("voip"));
        }
Esempio n. 2
0
        public void Sending_NonVoip_Type_With_Voip_Cert_Fails()
        {
            var apns = ApnsClient.CreateUsingCert("voip.p12");
            var push = ApplePush.CreateAlert(new ApplePushAlert("title", "body")).AddToken("token");

            Assert.ThrowsAsync <InvalidOperationException>(async() => await apns.Send(push));
        }
Esempio n. 3
0
        ApplePush CreateStubPush()
        {
            var push = new ApplePush(ApplePushType.Alert)
                       .AddToken("token");

            return(push);
        }
Esempio n. 4
0
        public void Adding_ContentAvailable_To_Push_With_Badge_or_Sound_Fails()
        {
            var pushWithContentAvailable = ApplePush.CreateContentAvailable();

            Assert.Throws <InvalidOperationException>(() => pushWithContentAvailable.AddBadge(0));
            Assert.Throws <InvalidOperationException>(() => pushWithContentAvailable.AddSound("sound"));
        }
Esempio n. 5
0
        public async Task Ensure_Client_Caching_Works_With_Jwt()
        {
            var service = BoostrapApnsService();
            var jwtOpt1 = new ApnsJwtOptions()
            {
                KeyId       = "1234567890",
                TeamId      = "1234567890",
                BundleId    = "bundleid1",
                CertContent = _certs.P8CertData
            };
            var jwtOpt2 = new ApnsJwtOptions()
            {
                KeyId       = "1234567890",
                TeamId      = "1234567890",
                BundleId    = "bundleid2",
                CertContent = _certs.P8CertData
            };
            var firstPush  = ApplePush.CreateContentAvailable().AddToken("token");
            var secondPush = ApplePush.CreateAlert(new ApplePushAlert(null, "body")).AddToken("token");
            var thirdPush  = ApplePush.CreateAlert(new ApplePushAlert(null, "body")).AddToken("token");
            var fourthPush = ApplePush.CreateContentAvailable().AddToken("token");

            await service.SendPush(firstPush, jwtOpt1);

            await service.SendPush(secondPush, jwtOpt1);

            await service.SendPush(thirdPush, jwtOpt2);

            await service.SendPush(fourthPush, jwtOpt2);

            Assert.Equal(2, ((IDictionary)service.GetType().GetField("_cachedJwtClients", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(service)).Count);
        }
Esempio n. 6
0
        public void Setting_Custom_Priority()
        {
            var push = ApplePush.CreateContentAvailable();

            Assert.Equal(5, push.Priority);
            push.SetPriority(10);
            Assert.Equal(10, push.Priority);
        }
Esempio n. 7
0
        public void Ensure_Type_Correspond_To_Payload()
        {
            var pushWithContentAvailable = ApplePush.CreateContentAvailable();
            var pushWithAlert            = ApplePush.CreateAlert(new ApplePushAlert("title", "body"));

            Assert.Equal(ApplePushType.Background, pushWithContentAvailable.Type);
            Assert.Equal(ApplePushType.Alert, pushWithAlert.Type);
        }
Esempio n. 8
0
        public void Ensure_Priority_Corresponds_To_Payload()
        {
            var pushWithContentAvailable = ApplePush.CreateContentAvailable();
            var pushWithAlert            = ApplePush.CreateAlert(new ApplePushAlert("title", "body"));

            Assert.Equal(5, pushWithContentAvailable.Priority);
            Assert.Equal(10, pushWithAlert.Priority);
        }
Esempio n. 9
0
        public void AddImmediateExpiration()
        {
            var push = new ApplePush(ApplePushType.Alert);

            push.AddImmediateExpiration();

            Assert.Equal(DateTimeOffset.MinValue, push.Expiration);
        }
Esempio n. 10
0
        public void AddExpiration()
        {
            var now  = DateTimeOffset.UtcNow;
            var push = new ApplePush(ApplePushType.Alert);

            push.AddExpiration(now);

            Assert.Equal(now, push.Expiration);
        }
Esempio n. 11
0
 public async Task Sending_Push_Not_Throws()
 {
     var client = BoostrapApnsClient();
     var push   = ApplePush.CreateAlert("body")
                  .AddToken("token")
                  .AddBadge(1)
                  .AddSound()
                  .AddLocation("location");
     await client.Send(push);
 }
Esempio n. 12
0
        public void Sending_NonVoip_Type_With_Voip_Cert_Fails()
        {
#if !NETCOREAPP3_1
            return;
#endif
            var apns = ApnsClient.CreateUsingCert(_certs.P12Cert);
            var push = ApplePush.CreateAlert(new ApplePushAlert("title", "body")).AddToken("token");

            Assert.ThrowsAsync <InvalidOperationException>(async() => await apns.SendAsync(push));
        }
Esempio n. 13
0
        public void Adding_Token_and_VoipToken_Together_Fails()
        {
            var pushWithToken      = ApplePush.CreateContentAvailable().AddToken("token");
            var pushWithVoipToken  = ApplePush.CreateContentAvailable(true).AddVoipToken("voip");
            var alertPushWithToken = ApplePush.CreateAlert(new ApplePushAlert("title", "body")).AddToken("token");

            Assert.Throws <InvalidOperationException>(() => pushWithToken.AddVoipToken("voip"));
            Assert.Throws <InvalidOperationException>(() => pushWithVoipToken.AddToken("token"));
            Assert.Throws <InvalidOperationException>(() => alertPushWithToken.AddVoipToken("voip"));
        }
Esempio n. 14
0
        public void Creating_Push_With_Alert_Title_Body_Subtitle()
        {
            var push = new ApplePush(ApplePushType.Alert)
                       .AddAlert("title", "subtitle", "body");

            var          payload       = push.GeneratePayload();
            string       payloadJson   = JsonConvert.SerializeObject(payload);
            const string referenceJson = "{\"aps\":{\"alert\":{\"title\":\"title\",\"subtitle\":\"subtitle\",\"body\":\"body\"}}}";

            Assert.Equal(referenceJson, payloadJson);
        }
Esempio n. 15
0
        public void Creating_Push_With_Localized_Alert_TitleKey_TitleKeyArgs_Key_Args_ActionKey()
        {
            var push = new ApplePush(ApplePushType.Alert)
                       .AddLocalizedAlert("LOCALIZED_TITLE", new[] { "LocalizedTitleArgument", "5" }, "LOCALIZED_KEY", new[] { "LocalizedArg", "6" }, "LOCALIZED_ACTION");

            var          payload       = push.GeneratePayload();
            var          payloadJson   = JsonConvert.SerializeObject(payload);
            const string referenceJson = "{\"aps\":{\"alert\":{\"title-loc-key\":\"LOCALIZED_TITLE\",\"title-loc-args\":[\"LocalizedTitleArgument\",\"5\"],\"loc-key\":\"LOCALIZED_KEY\",\"loc-args\":[\"LocalizedArg\",\"6\"],\"action-loc-key\":\"LOCALIZED_ACTION\"}}}";

            Assert.Equal(referenceJson, payloadJson);
        }
Esempio n. 16
0
        public void Creating_Push_With_Development_Server_Should_Set_Property()
        {
            var prodPush = new ApplePush(ApplePushType.Alert)
                           .AddAlert("body");

            var devPush = new ApplePush(ApplePushType.Alert)
                          .AddAlert("body")
                          .SendToDevelopmentServer();

            Assert.False(prodPush.IsSendToDevelopmentServer);
            Assert.True(devPush.IsSendToDevelopmentServer);
        }
Esempio n. 17
0
        //[Fact] cert uses real handler. Can't test until refactored.
        public async Task Ensure_Client_Caching_Works_With_Cert()
        {
            var service    = BoostrapApnsService();
            var firstPush  = ApplePush.CreateContentAvailable().AddToken("token");
            var secondPush = ApplePush.CreateAlert(new ApplePushAlert(null, "body")).AddToken("token");

            await service.SendPush(firstPush, _certs.P12Cert);

            await service.SendPush(secondPush, _certs.P12Cert);

            Assert.Single((IDictionary)service.GetType().GetField("_cachedJwtClients", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(service));
        }
Esempio n. 18
0
        public void AddContentAvailable()
        {
            var push = new ApplePush(ApplePushType.Background);

            push.AddContentAvailable();

            var          payload       = push.GeneratePayload();
            string       payloadJson   = JsonConvert.SerializeObject(payload);
            const string referenceJson = "{\"aps\":{\"content-available\":\"1\"}}";

            Assert.Equal(referenceJson, payloadJson);
        }
Esempio n. 19
0
        public void Creating_Push_With_Alert_Only_Body_And_Localized_Alert_Should_Return_Only_Alert()
        {
            var push = new ApplePush(ApplePushType.Alert)
                       .AddAlert("body")
                       .AddLocalizedAlert("LOCALIZED_KEY", new[] { "LocalizedArg" });

            var          payload       = push.GeneratePayload();
            string       payloadJson   = JsonConvert.SerializeObject(payload);
            const string referenceJson = "{\"aps\":{\"alert\":\"body\"}}";

            Assert.Equal(referenceJson, payloadJson);
        }
Esempio n. 20
0
        public void Creating_Push_With_Localized_Alert_Key_And_Args()
        {
            var push = new ApplePush(ApplePushType.Alert)
                       .AddLocalizedAlert("LOCALIZED_KEY", new[] { "LocalizedArg" });

            var payload     = push.GeneratePayload();
            var payloadJson = JsonConvert.SerializeObject(payload, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            const string referenceJson = "{\"aps\":{\"alert\":{\"loc-key\":\"LOCALIZED_KEY\",\"loc-args\":[\"LocalizedArg\"]}}}";

            Assert.Equal(referenceJson, payloadJson);
        }
Esempio n. 21
0
        public void AddCustomProperty_Correctly_Adds_String_Value()
        {
            var push = ApplePush
                       .CreateAlert("testAlert")
                       .AddCustomProperty("customPropertyKey", "customPropertyValue");

            var    payload     = push.GeneratePayload();
            string payloadJson = JsonConvert.SerializeObject(payload);

            const string referencePayloadJson = "{\"aps\":{\"alert\":\"testAlert\"},\"customPropertyKey\":\"customPropertyValue\"}";

            Assert.Equal(referencePayloadJson, payloadJson);
        }
Esempio n. 22
0
        public void AddCustomProperty_Correctly_Adds_Complex_Value()
        {
            var push = ApplePush
                       .CreateAlert("testAlert")
                       .AddCustomProperty("customPropertyKey", new { value1 = "123", value2 = 456 });

            var    payload     = push.GeneratePayload();
            string payloadJson = JsonConvert.SerializeObject(payload);

            const string referencePayloadJson = "{\"aps\":{\"alert\":\"testAlert\"},\"customPropertyKey\":{\"value1\":\"123\",\"value2\":456}}";

            Assert.Equal(referencePayloadJson, payloadJson);
        }
Esempio n. 23
0
        public void Creating_Push_With_ContentAvailable_MutableContent_Alert()
        {
            var push = new ApplePush(ApplePushType.Alert)
                       .AddContentAvailable()
                       .AddMutableContent()
                       .AddAlert("title", "body");

            var          payload       = push.GeneratePayload();
            string       payloadJson   = JsonConvert.SerializeObject(payload);
            const string referenceJson = "{\"aps\":{\"content-available\":\"1\",\"mutable-content\":\"1\",\"alert\":{\"title\":\"title\",\"body\":\"body\"}}}";

            Assert.Equal(referenceJson, payloadJson);
        }
Esempio n. 24
0
        public Task <ApnsResponse> SendPush(ApplePush push, ApnsJwtOptions jwtOptions)
        {
            var client = GetOrCreateCached(jwtOptions);

            try
            {
                return(client.Send(push));
            }
            catch
            {
                _cachedJwtClients.TryRemove(jwtOptions.BundleId, out _);
                throw;
            }
        }
Esempio n. 25
0
        public Task <ApnsResponse> SendPush(ApplePush push, X509Certificate2 cert)
        {
            var client = GetOrCreateCached(cert);

            try
            {
                return(client.Send(push));
            }
            catch
            {
                _cachedCertClients.TryRemove(cert.Thumbprint, out _);
                throw;
            }
        }
Esempio n. 26
0
        public Task <ApnsResponse> SendPush(ApplePush push, X509Certificate2 cert, bool useSandbox = false)
        {
            string clientCacheId = (useSandbox ? "s_" : "") + cert.Thumbprint;
            var    client        = _cachedCertClients.GetOrAdd(clientCacheId, _ => _apnsClientFactory.CreateUsingCert(cert, useSandbox));

            try
            {
                return(client.Send(push));
            }
            catch
            {
                _cachedCertClients.TryRemove(clientCacheId, out _);
                throw;
            }
        }
Esempio n. 27
0
        public Task <ApnsResponse> SendPush(ApplePush push, ApnsJwtOptions jwtOptions, bool useSandbox = false)
        {
            string clientCacheId = (useSandbox ? "s_" : "") + jwtOptions.BundleId;
            var    client        = _cachedJwtClients.GetOrAdd(clientCacheId, _ => _apnsClientFactory.CreateUsingJwt(jwtOptions, useSandbox));

            try
            {
                return(client.Send(push));
            }
            catch
            {
                _cachedJwtClients.TryRemove(clientCacheId, out _);
                throw;
            }
        }
Esempio n. 28
0
        public void Adding_Token_To_Voip_Type_Throws_InvalidOperationException()
        {
            var backgroundPush = ApplePush.CreateContentAvailable(true);

            Assert.Throws <InvalidOperationException>(() => backgroundPush.AddToken("token"));
        }
Esempio n. 29
0
        public void CreateContentAvailableAsVoip_Has_Voip_Type()
        {
            var voipPush = ApplePush.CreateContentAvailable(true);

            Assert.Equal(ApplePushType.Voip, voipPush.Type);
        }
Esempio n. 30
0
        public void CreateContentAvailable_Has_Background_Type()
        {
            var voipPush = ApplePush.CreateContentAvailable();

            Assert.Equal(ApplePushType.Background, voipPush.Type);
        }