private void CheckInvalidTryParse(string input)
        {
            RangeConditionHeaderValue result = null;

            Assert.False(RangeConditionHeaderValue.TryParse(input, out result));
            Assert.Null(result);
        }
Exemple #2
0
        private static void CheckInvalidGetRangeConditionLength(string input, int startIndex)
        {
            object result = null;

            Assert.Equal(0, RangeConditionHeaderValue.GetRangeConditionLength(input, startIndex, out result));
            Assert.Null(result);
        }
        public void TryParse_Invalid()
        {
            RangeConditionHeaderValue res;

            Assert.IsFalse(RangeConditionHeaderValue.TryParse("", out res), "#1");
            Assert.IsNull(res, "#2");
        }
        private void CheckValidTryParse(string input, RangeConditionHeaderValue expectedResult)
        {
            RangeConditionHeaderValue result = null;

            Assert.True(RangeConditionHeaderValue.TryParse(input, out result));
            Assert.Equal(expectedResult, result);
        }
Exemple #5
0
        private void CheckInvalidParse(string input)
        {
            Assert.Throws <FormatException>(() => { RangeConditionHeaderValue.Parse(input); });

            Assert.False(RangeConditionHeaderValue.TryParse(input, out RangeConditionHeaderValue result));
            Assert.Null(result);
        }
        public void Parse()
        {
            var res = RangeConditionHeaderValue.Parse("\"c\"");

            Assert.AreEqual("\"c\"", res.EntityTag.Tag, "#1");
            Assert.IsFalse(res.EntityTag.IsWeak, "#2");
            Assert.IsNull(res.Date, "#3");
        }
Exemple #7
0
        public void Ctor_DateOverload_MatchExpectation()
        {
            RangeConditionHeaderValue 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.Date);
        }
Exemple #8
0
        public void TryParse()
        {
            RangeConditionHeaderValue res;

            Assert.IsTrue(RangeConditionHeaderValue.TryParse("\"\"", out res), "#1");
            Assert.AreEqual("\"\"", res.EntityTag.Tag, "#2");
            Assert.IsNull(res.Date, "#3");
        }
Exemple #9
0
        public void Ctor_EntityTagStringOverload_MatchExpectation()
        {
            RangeConditionHeaderValue rangeCondition = new RangeConditionHeaderValue("\"y\"");

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

            AssertExtensions.Throws <ArgumentException>("tag", () => { new RangeConditionHeaderValue((string)null); });
        }
    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));
    }
Exemple #11
0
        public void ToString_UseDifferentrangeConditions_AllSerializedCorrectly()
        {
            RangeConditionHeaderValue 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());
        }
Exemple #12
0
        private static void CallGetRangeConditionLength(string input, int startIndex, int expectedLength,
                                                        out RangeConditionHeaderValue result)
        {
            object temp = null;

            Assert.Equal(expectedLength, RangeConditionHeaderValue.GetRangeConditionLength(input, startIndex,
                                                                                           out temp));
            result = temp as RangeConditionHeaderValue;
        }
    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));
    }
Exemple #14
0
        public void Ctor_EntityTagOverload_MatchExpectation()
        {
            RangeConditionHeaderValue rangeCondition = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));

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

            EntityTagHeaderValue input = null;

            Assert.Throws <ArgumentNullException>(() => { new RangeConditionHeaderValue(input); });
        }
Exemple #15
0
        private void CheckValidParsedValue(string input, int startIndex, RangeConditionHeaderValue expectedResult,
                                           int expectedIndex)
        {
            HttpHeaderParser parser = GenericHeaderParser.RangeConditionParser;
            object           result = null;

            Assert.True(parser.TryParseValue(input, null, ref startIndex, out result),
                        string.Format("TryParse returned false. Input: '{0}'", input));
            Assert.Equal(expectedIndex, startIndex);
            Assert.Equal(expectedResult, result);
        }
Exemple #16
0
        public void Equals()
        {
            var value = new RangeConditionHeaderValue("\"abc\"");

            Assert.AreEqual(value, new RangeConditionHeaderValue("\"abc\""), "#1");
            Assert.AreNotEqual(value, new RangeConditionHeaderValue("\"AbC\""), "#2");

            value = new RangeConditionHeaderValue(DateTimeOffset.MinValue);
            Assert.AreEqual(value, new RangeConditionHeaderValue(DateTimeOffset.MinValue), "#3");
            Assert.AreNotEqual(value, new RangeConditionHeaderValue(DateTimeOffset.MaxValue), "#4");
            Assert.AreNotEqual(value, new RangeConditionHeaderValue("\"AbC\""), "#5");
        }
Exemple #17
0
        public void Properties()
        {
            var value = new RangeConditionHeaderValue("\"b\"");

            Assert.AreEqual(new EntityTagHeaderValue("\"b\""), value.EntityTag, "#1");
            Assert.IsNull(value.Date, "#2");

            var dto = new DateTimeOffset(20000, new TimeSpan());

            value = new RangeConditionHeaderValue(dto);
            Assert.AreEqual(null, value.EntityTag, "#3");
            Assert.AreEqual(dto, value.Date, "#4");
        }
Exemple #18
0
        public void Clone_Call_CloneFieldsMatchSourceFields()
        {
            RangeConditionHeaderValue source = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));
            RangeConditionHeaderValue clone  = (RangeConditionHeaderValue)((ICloneable)source).Clone();

            Assert.Equal(source.EntityTag, clone.EntityTag);
            Assert.Null(clone.Date);

            source = new RangeConditionHeaderValue(new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            clone  = (RangeConditionHeaderValue)((ICloneable)source).Clone();

            Assert.Null(clone.EntityTag);
            Assert.Equal(source.Date, clone.Date);
        }
Exemple #19
0
        public void GetHashCode_UseSameAndDifferentrangeConditions_SameOrDifferentHashCodes()
        {
            RangeConditionHeaderValue rangeCondition1 = new RangeConditionHeaderValue("\"x\"");
            RangeConditionHeaderValue rangeCondition2 = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));
            RangeConditionHeaderValue rangeCondition3 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            RangeConditionHeaderValue rangeCondition4 = new RangeConditionHeaderValue(
                new DateTimeOffset(2008, 8, 16, 13, 44, 10, TimeSpan.Zero));
            RangeConditionHeaderValue rangeCondition5 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            RangeConditionHeaderValue 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());
        }
Exemple #20
0
        public void Parse_Invalid()
        {
            try {
                RangeConditionHeaderValue.Parse(null);
                Assert.Fail("#1");
            } catch (FormatException) {
            }

            try {
                RangeConditionHeaderValue.Parse("  ");
                Assert.Fail("#2");
            } catch (FormatException) {
            }

            try {
                RangeConditionHeaderValue.Parse("a b");
                Assert.Fail("#3");
            } catch (FormatException) {
            }
        }
Exemple #21
0
        public void Parse()
        {
            var res = RangeConditionHeaderValue.Parse("\"c\"");

            Assert.AreEqual("\"c\"", res.EntityTag.Tag, "#1");
            Assert.IsFalse(res.EntityTag.IsWeak, "#2");
            Assert.IsNull(res.Date, "#3");
            Assert.AreEqual("\"c\"", res.ToString(), "#4");

            res = RangeConditionHeaderValue.Parse("W/\"\"");
            Assert.AreEqual("\"\"", res.EntityTag.Tag, "#11");
            Assert.IsTrue(res.EntityTag.IsWeak, "#12");
            Assert.IsNull(res.Date, "#13");
            Assert.AreEqual("W/\"\"", res.ToString(), "#14");


            res = RangeConditionHeaderValue.Parse("Sun Nov 6 08:49:37 1994");
            Assert.IsNull(res.EntityTag, "#21");
            Assert.AreEqual(new DateTimeOffset(1994, 11, 6, 8, 49, 37, 0, TimeSpan.Zero), res.Date, "#22");
            Assert.AreEqual("Sun, 06 Nov 1994 08:49:37 GMT", res.ToString(), "#23");
        }
Exemple #22
0
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            RangeConditionHeaderValue rangeCondition1 = new RangeConditionHeaderValue("\"x\"");
            RangeConditionHeaderValue rangeCondition2 = new RangeConditionHeaderValue(new EntityTagHeaderValue("\"x\""));
            RangeConditionHeaderValue rangeCondition3 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            RangeConditionHeaderValue rangeCondition4 = new RangeConditionHeaderValue(
                new DateTimeOffset(2008, 8, 16, 13, 44, 10, TimeSpan.Zero));
            RangeConditionHeaderValue rangeCondition5 = new RangeConditionHeaderValue(
                new DateTimeOffset(2010, 7, 15, 12, 33, 57, TimeSpan.Zero));
            RangeConditionHeaderValue 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\"");
        }
Exemple #23
0
        public void GetRangeConditionLength_DifferentValidScenarios_AllReturnNonZero()
        {
            RangeConditionHeaderValue result = null;

            CallGetRangeConditionLength(" W/ \"tag\" ", 1, 9, out result);
            Assert.Equal(new EntityTagHeaderValue("\"tag\"", true), result.EntityTag);
            Assert.Null(result.Date);

            CallGetRangeConditionLength(" w/\"tag\"", 1, 7, out result);
            Assert.Equal(new EntityTagHeaderValue("\"tag\"", true), result.EntityTag);
            Assert.Null(result.Date);

            CallGetRangeConditionLength("\"tag\"", 0, 5, out result);
            Assert.Equal(new EntityTagHeaderValue("\"tag\""), result.EntityTag);
            Assert.Null(result.Date);

            CallGetRangeConditionLength("Wed, 09 Nov 1994 08:49:37 GMT", 0, 29, out result);
            Assert.Null(result.EntityTag);
            Assert.Equal(new DateTimeOffset(1994, 11, 9, 8, 49, 37, TimeSpan.Zero), result.Date);

            CallGetRangeConditionLength("Sun, 06 Nov 1994 08:49:37 GMT", 0, 29, out result);
            Assert.Null(result.EntityTag);
            Assert.Equal(new DateTimeOffset(1994, 11, 6, 8, 49, 37, TimeSpan.Zero), result.Date);
        }
 public void Parse_SetOfInvalidValueStrings_Throws(string input)
 {
     Assert.Throws <FormatException>(() => RangeConditionHeaderValue.Parse(input));
 }
        /// <summary>
        /// Insert a request HTTP header
        /// </summary>
        /// <param name="header">A <see cref="System.Net.Http.Headers.HttpRequestHeaders"/> object on wich the request header will be recorded.</param>
        /// <param name="name">The header attribute name.</param>
        /// <param name="value">The header attribute value.</param>
        public void AddRequestHeader(HttpRequestHeaders header, string name, string value)
        {
            if (name.Equals("Authorization", StringComparison.OrdinalIgnoreCase))
            {
                header.Authorization = new AuthenticationHeaderValue(value);
            }

            else if (name.Equals("Cache-Control", StringComparison.OrdinalIgnoreCase))
            {
                header.CacheControl = CacheControlHeaderValue.Parse(value);
            }

            else if (name.Equals("Date", StringComparison.OrdinalIgnoreCase))
            {
                header.Date = DateTimeOffset.Parse(value);
            }

            else if (name.Equals("If-Modified-Since", StringComparison.OrdinalIgnoreCase))
            {
                header.IfModifiedSince = DateTimeOffset.Parse(value);
            }

            else if (name.Equals("If-Range", StringComparison.OrdinalIgnoreCase))
            {
                header.IfRange = RangeConditionHeaderValue.Parse(value);
            }

            else if (name.Equals("If-Unmodified-Since", StringComparison.OrdinalIgnoreCase))
            {
                header.IfUnmodifiedSince = DateTimeOffset.Parse(value);
            }

            else if (name.Equals("Max-Forwards", StringComparison.OrdinalIgnoreCase))
            {
                header.MaxForwards = int.Parse(value);
            }

            else if (name.Equals("Proxy-Authorization", StringComparison.OrdinalIgnoreCase))
            {
                header.ProxyAuthorization = AuthenticationHeaderValue.Parse(value);
            }

            else if (name.Equals("Range", StringComparison.OrdinalIgnoreCase))
            {
                header.Range = RangeHeaderValue.Parse(value);
            }

            else if (name.Equals("Referrer", StringComparison.OrdinalIgnoreCase))
            {
                header.Referrer = new Uri(value);
            }

            else
            {
                try{
                    header.Add(name, value);
                }catch (ArgumentException) {
                    if (header.GetType().GetProperty(name.Replace("-", "")) != null)
                    {
                        header.GetType().GetProperty(name.Replace("-", "")).SetValue(header, value);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
 public void TryParse_SetOfInvalidValueStrings_ReturnsFalse(string input)
 {
     Assert.False(RangeConditionHeaderValue.TryParse(input, out var result));
     Assert.Null(result);
 }
    private void CheckValidParse(string input, RangeConditionHeaderValue expectedResult)
    {
        var result = RangeConditionHeaderValue.Parse(input);

        Assert.Equal(expectedResult, result);
    }
Exemple #28
0
 public static bool TryParse(string input, out RangeConditionHeaderValue parsedValue);
 private void CheckInvalidParse(string input)
 {
     Assert.Throws <FormatException>(() => { RangeConditionHeaderValue.Parse(input); });
 }
Exemple #30
0
    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 (!HttpRuleParser.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);
    }
		public static bool TryParse (string input, out RangeConditionHeaderValue parsedValue)
		{
			throw new NotImplementedException ();
		}