private void CheckValidTryParse(string input, RangeConditionHeaderValue expectedResult)
        {
            RangeConditionHeaderValue result = null;

            Assert.True(RangeConditionHeaderValue.TryParse(input, out result));
            Assert.Equal(expectedResult, result);
        }
        public void TryParse_SetOfInvalidValueStrings_ReturnsFalse(string input)
        {
            RangeConditionHeaderValue result = null;

            Assert.False(RangeConditionHeaderValue.TryParse(input, out result));
            Assert.Null(result);
        }
        public void Ctor_DateOverload_MatchExpectation()
        {
            var rangeCondition = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));

            Assert.Null(rangeCondition.EntityTag);
            Assert.Equal(new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero), rangeCondition.LastModified);
        }
        public void ToString_UseDifferentRangeConditions_AllSerializedCorrectly()
        {
            var rangeCondition = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));

            Assert.Equal("\"x\"", rangeCondition.ToString());

            rangeCondition = new RangeConditionHeaderValue(new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            Assert.Equal("Thu, 15 Jul 2010 12:33:57 GMT", rangeCondition.ToString());
        }
        public void Ctor_EntityTagStringOverload_MatchExpectation()
        {
            var rangeCondition = new RangeConditionHeaderValue("\"y\"");

            Assert.Equal(new EntityTagHeaderValue("\"y\""), rangeCondition.EntityTag);
            Assert.Null(rangeCondition.LastModified);

            Assert.Throws <ArgumentException>(() => new RangeConditionHeaderValue((string)null));
        }
        public void Ctor_EntityTagOverload_MatchExpectation()
        {
            var rangeCondition = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));

            Assert.Equal(new EntityTagHeaderValue("\"x\""), rangeCondition.EntityTag);
            Assert.Null(rangeCondition.LastModified);

            EntityTagHeaderValue input = null;

            Assert.Throws <ArgumentNullException>(() => new RangeConditionHeaderValue(input));
        }
        public void GetHashCode_UseSameAndDifferentRangeConditions_SameOrDifferentHashCodes()
        {
            var rangeCondition1 = new RangeConditionHeaderValue("\"x\"");
            var rangeCondition2 = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));
            var rangeCondition3 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            var rangeCondition4 = new RangeConditionHeaderValue(
                new DateTimeOffset(2008, 8, 16, 13, 44, 10, TimeSpan.Zero));
            var rangeCondition5 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            var rangeCondition6 = new RangeConditionHeaderValue(
                new EntityTagHeaderValue("\"x\"", true));

            Assert.Equal(rangeCondition1.GetHashCode(), rangeCondition2.GetHashCode());
            Assert.NotEqual(rangeCondition1.GetHashCode(), rangeCondition3.GetHashCode());
            Assert.NotEqual(rangeCondition3.GetHashCode(), rangeCondition4.GetHashCode());
            Assert.Equal(rangeCondition3.GetHashCode(), rangeCondition5.GetHashCode());
            Assert.NotEqual(rangeCondition1.GetHashCode(), rangeCondition6.GetHashCode());
        }
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            var rangeCondition1 = new RangeConditionHeaderValue("\"x\"");
            var rangeCondition2 = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));
            var rangeCondition3 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            var rangeCondition4 = new RangeConditionHeaderValue(
                new DateTimeOffset(2008, 8, 16, 13, 44, 10, TimeSpan.Zero));
            var rangeCondition5 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            var rangeCondition6 = new RangeConditionHeaderValue(
                new EntityTagHeaderValue("\"x\"", true));

            Assert.False(rangeCondition1.Equals(null), "\"x\" vs. <null>");
            Assert.True(rangeCondition1.Equals(rangeCondition2), "\"x\" vs. \"x\"");
            Assert.False(rangeCondition1.Equals(rangeCondition3), "\"x\" vs. date");
            Assert.False(rangeCondition3.Equals(rangeCondition1), "date vs. \"x\"");
            Assert.False(rangeCondition3.Equals(rangeCondition4), "date vs. different date");
            Assert.True(rangeCondition3.Equals(rangeCondition5), "date vs. date");
            Assert.False(rangeCondition1.Equals(rangeCondition6), "\"x\" vs. W/\"x\"");
        }
        public static bool TryParse(StringSegment input, out RangeConditionHeaderValue parsedValue)
        {
            var index = 0;

            return(Parser.TryParseValue(input, ref index, out parsedValue));
        }
        private static int GetRangeConditionLength(StringSegment input, int startIndex, out RangeConditionHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            // Make sure we have at least 2 characters
            if (StringSegment.IsNullOrEmpty(input) || (startIndex + 1 >= input.Length))
            {
                return(0);
            }

            var current = startIndex;

            // Caller must remove leading whitespaces.
            DateTimeOffset       date      = DateTimeOffset.MinValue;
            EntityTagHeaderValue entityTag = null;

            // Entity tags are quoted strings optionally preceded by "W/". By looking at the first two character we
            // can determine whether the string is en entity tag or a date.
            var firstChar  = input[current];
            var secondChar = input[current + 1];

            if ((firstChar == '\"') || (((firstChar == 'w') || (firstChar == 'W')) && (secondChar == '/')))
            {
                // trailing whitespaces are removed by GetEntityTagLength()
                var entityTagLength = EntityTagHeaderValue.GetEntityTagLength(input, current, out entityTag);

                if (entityTagLength == 0)
                {
                    return(0);
                }

                current = current + entityTagLength;

                // RangeConditionHeaderValue only allows 1 value. There must be no delimiter/other chars after an
                // entity tag.
                if (current != input.Length)
                {
                    return(0);
                }
            }
            else
            {
                if (!ProtoRuleParser.TryStringToDate(input.Subsegment(current), out date))
                {
                    return(0);
                }

                // If we got a valid date, then the parser consumed the whole string (incl. trailing whitespaces).
                current = input.Length;
            }

            parsedValue = new RangeConditionHeaderValue();
            if (entityTag == null)
            {
                parsedValue._lastModified = date;
            }
            else
            {
                parsedValue._entityTag = entityTag;
            }

            return(current - startIndex);
        }
        private void CheckValidParse(string input, RangeConditionHeaderValue expectedResult)
        {
            var result = RangeConditionHeaderValue.Parse(input);

            Assert.Equal(expectedResult, result);
        }
 public void Parse_SetOfInvalidValueStrings_Throws(string input)
 {
     Assert.Throws <FormatException>(() => RangeConditionHeaderValue.Parse(input));
 }