private static string CreateTextEventData()
        {
            EventTestData data = new EventTestData
            {
                TextField    = ExpectedTextField,
                IntegerField = ExpectedIntegerField,
            };

            return(JsonConvert.SerializeObject(data));
        }
        private static void AssertPusherEventData(string expectedData, string actualData)
        {
            Assert.IsNotNull(expectedData);
            Assert.AreEqual(expectedData, actualData);
            EventTestData actual = JsonConvert.DeserializeObject <EventTestData>(actualData);

            Assert.AreEqual(ExpectedIntegerField, actual.IntegerField, nameof(actual.IntegerField));
            Assert.IsNull(actual.NothingField, nameof(actual.NothingField));
            Assert.AreEqual(ExpectedTextField, actual.TextField, nameof(actual.TextField));
        }
        public async Task PusherEventEmitterPrivateEncryptedChannelTestAsync()
        {
            // Arrange
            byte[] encryptionKey = GenerateEncryptionMasterKey();
            var    pusherServer  = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions
            {
                Cluster             = Config.Cluster,
                EncryptionMasterKey = encryptionKey,
            });

            ChannelTypes   channelType          = ChannelTypes.PrivateEncrypted;
            Pusher         localPusher          = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: encryptionKey);
            string         testEventName        = "private-encrypted-event-test";
            AutoResetEvent globalEventReceived  = new AutoResetEvent(false);
            AutoResetEvent channelEventReceived = new AutoResetEvent(false);
            PusherEvent    globalEvent          = null;
            PusherEvent    channelEvent         = null;
            PusherEvent    pusherEvent          = CreatePusherEvent(channelType, testEventName);

            await localPusher.ConnectAsync().ConfigureAwait(false);

            Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false);

            void GeneralListener(string eventName, PusherEvent eventData)
            {
                if (eventName == testEventName)
                {
                    globalEvent = eventData;
                    globalEventReceived.Set();
                }
            }

            void Listener(PusherEvent eventData)
            {
                channelEvent = eventData;
                channelEventReceived.Set();
            }

            EventTestData data = new EventTestData
            {
                TextField    = ExpectedTextField,
                IntegerField = ExpectedIntegerField,
            };

            // Act
            localPusher.BindAll(GeneralListener);
            localChannel.Bind(testEventName, Listener);
            await pusherServer.TriggerAsync(pusherEvent.ChannelName, testEventName, data).ConfigureAwait(false);

            // Assert
            Assert.IsTrue(globalEventReceived.WaitOne(TimeSpan.FromSeconds(5)));
            Assert.IsTrue(channelEventReceived.WaitOne(TimeSpan.FromSeconds(5)));
            AssertPusherEventsAreEqual(channelType, pusherEvent, globalEvent);
            AssertPusherEventsAreEqual(channelType, pusherEvent, channelEvent);
        }
        private static void ValidateDynamicEvent(string channelName, string eventName, dynamic dynamicEvent)
        {
            Assert.AreEqual(channelName, dynamicEvent.channel, "channel");
            Assert.AreEqual(eventName, dynamicEvent.@event, "event");
            if (Channel.GetChannelType(channelName) == ChannelTypes.Presence)
            {
                Assert.IsNotNull(dynamicEvent.user_id, "user_id");
            }

            EventTestData actual = JsonConvert.DeserializeObject <EventTestData>(dynamicEvent.data.ToString());

            Assert.AreEqual(ExpectedIntegerField, actual.IntegerField, nameof(actual.IntegerField));
            Assert.IsNull(actual.NothingField, nameof(actual.NothingField));
            Assert.AreEqual(ExpectedTextField, actual.TextField, nameof(actual.TextField));
        }
        private static PusherEvent CreatePusherEvent(ChannelTypes channelType, string eventName)
        {
            EventTestData data = new EventTestData
            {
                TextField    = ExpectedTextField,
                IntegerField = ExpectedIntegerField,
            };
            Dictionary <string, object> properties = new Dictionary <string, object>
            {
                { "channel", ChannelNameFactory.CreateUniqueChannelName(channelType: channelType) },
                { "event", eventName },
                { "data", data },
            };

            PusherEvent result = new PusherEvent(properties, JsonConvert.SerializeObject(properties));

            return(result);
        }
        public async Task PusherEventEmitterPrivateEncryptedChannelDecryptionFailureTestAsync()
        {
            // Arrange
            var pusherServer = new PusherServer.Pusher(Config.AppId, Config.AppKey, Config.AppSecret, new PusherServer.PusherOptions
            {
                Cluster             = Config.Cluster,
                EncryptionMasterKey = GenerateEncryptionMasterKey(),
            });

            ChannelDecryptionException decryptionException = null;
            ChannelTypes channelType = ChannelTypes.PrivateEncrypted;

            // Use a different encryption master key - decryption should fail
            Pusher         localPusher          = PusherFactory.GetPusher(channelType: ChannelTypes.Presence, saveTo: _clients, encryptionKey: GenerateEncryptionMasterKey());
            string         testEventName        = "private-encrypted-event-test";
            AutoResetEvent globalEventReceived  = new AutoResetEvent(false);
            AutoResetEvent channelEventReceived = new AutoResetEvent(false);
            AutoResetEvent errorReceived        = new AutoResetEvent(false);
            PusherEvent    pusherEvent          = CreatePusherEvent(channelType, testEventName);

            await localPusher.ConnectAsync().ConfigureAwait(false);

            Channel localChannel = await localPusher.SubscribeAsync(pusherEvent.ChannelName).ConfigureAwait(false);

            void GeneralListener(string eventName, PusherEvent eventData)
            {
                if (eventName == testEventName)
                {
                    globalEventReceived.Set();
                }
            }

            void Listener(PusherEvent eventData)
            {
                channelEventReceived.Set();
            }

            EventTestData data = new EventTestData
            {
                TextField    = ExpectedTextField,
                IntegerField = ExpectedIntegerField,
            };

            void ErrorHandler(object sender, PusherException error)
            {
                decryptionException = error as ChannelDecryptionException;
                if (decryptionException != null)
                {
                    errorReceived.Set();
                }
            }

            // Act
            localPusher.Error += ErrorHandler;
            localPusher.BindAll(GeneralListener);
            localChannel.Bind(testEventName, Listener);
            await pusherServer.TriggerAsync(pusherEvent.ChannelName, testEventName, data).ConfigureAwait(false);

            // Assert
            Assert.IsTrue(errorReceived.WaitOne(TimeSpan.FromSeconds(5)), "Expected to handle an error.");
            Assert.IsNotNull(decryptionException.SocketID, nameof(decryptionException.SocketID));
            Assert.AreEqual(testEventName, decryptionException.EventName, nameof(decryptionException.EventName));
            Assert.AreEqual(localChannel.Name, decryptionException.ChannelName, nameof(decryptionException.ChannelName));
            Assert.IsFalse(globalEventReceived.WaitOne(TimeSpan.FromSeconds(0.1)), "Did not expect to get a global event raised.");
            Assert.IsFalse(channelEventReceived.WaitOne(TimeSpan.FromSeconds(0.1)), "Did not expect to get a channel event raised.");
        }