public virtual string Serialize(HoneypotData token)
        {
            if (token == null) {
                throw new ArgumentNullException("token");
            }

            using (var stream = new MemoryStream())
            using (var writer = new BinaryWriter(stream)) {
                writer.Write(token.Key.ToString());
                writer.Write(token.InputNameValue);
                writer.Write(token.CreationDate.Ticks);

                return this.Encoder(stream.ToArray());
            }
        }
        public void CanRoundTripData()
        {
            // Arrange
            var serializer = new HoneypotDataSerializer
            { Decoder = value => Convert.FromBase64String(value), Encoder = bytes => Convert.ToBase64String(bytes) };

            var input = new HoneypotData("input-name");

            // Act
            HoneypotData output = serializer.Deserialize(serializer.Serialize(input));

            // Assert
            Assert.NotNull(output);
            Assert.Equal(input.InputNameValue, output.InputNameValue);
            Assert.Equal(input.Key, output.Key);
            Assert.Equal(input.CreationDate, output.CreationDate);
        }
        public void IsBot_ShouldBeFalse_IfHpInputNameIsInFormAndEmpty()
        {
            // Arrang
            Honeypot.InputNames.Clear();
            Honeypot.CustomRules.Clear();
            var worker = new HoneypotWorker();
            var token = new HoneypotData(HoneypotData.DefaultFieldName);
            var serializer = new HoneypotDataSerializer();
            var form = new NameValueCollection {
                { "FirstName", "Jon" }, {
                    HoneypotData.FormKeyFieldName,
                    serializer.Serialize(token)
                },
                {token.InputNameValue, ""}
            };

            // Act
            bool isBot = worker.IsBot(MvcHelper.GetHttpContext(form));

            // Assert
            Assert.False(isBot);
        }
 public void Ctor_ShouldSetInputNameValue_WhenPassedValidString()
 {
     var honeypotData = new HoneypotData("Valid String");
     Assert.Equal(honeypotData.InputNameValue, "Valid String");
 }
        public void IsBot_ShouldReturnTrue_WhenValidateRulesReturnsFalse()
        {
            // Arrang
            Honeypot.InputNames.Clear();
            var worker = new HoneypotWorker();
            Honeypot.CustomRules.Clear();

            var token = new HoneypotData(HoneypotData.DefaultFieldName);
            var serializer = new HoneypotDataSerializer();
            var form = new NameValueCollection {
                { "FirstName", "Jon" }, {
                    HoneypotData.FormKeyFieldName,
                    serializer.Serialize(token)
                },
                {token.InputNameValue, ""}
            };
            Func<NameValueCollection, bool> failingRule = (f) => true;
            Honeypot.CustomRules.Add(failingRule);

            // Act
            bool isBot = worker.IsBot(MvcHelper.GetHttpContext(form));

            // Assert
            Assert.True(isBot);
        }