コード例 #1
0
        private void CheckInvalidTryParse(string input)
        {
            RangeHeaderValue result = null;

            Assert.False(RangeHeaderValue.TryParse(input, out result));
            Assert.Null(result);
        }
コード例 #2
0
        public void Equals_UseSameAndDifferentRanges_EqualOrNotEqualNoExceptions()
        {
            var range1 = new RangeHeaderValue(1, 2);
            var range2 = new RangeHeaderValue(1, 2);

            range2.Unit = "BYTES";
            var range3 = new RangeHeaderValue(1, null);
            var range4 = new RangeHeaderValue(null, 2);
            var range5 = new RangeHeaderValue();

            range5.Ranges.Add(new RangeItemHeaderValue(1, 2));
            range5.Ranges.Add(new RangeItemHeaderValue(3, 4));
            var range6 = new RangeHeaderValue();

            range6.Ranges.Add(new RangeItemHeaderValue(3, 4)); // reverse order of range5
            range6.Ranges.Add(new RangeItemHeaderValue(1, 2));
            var range7 = new RangeHeaderValue(1, 2);

            range7.Unit = "other";

            Assert.False(range1.Equals(null), "bytes=1-2 vs. <null>");
            Assert.True(range1.Equals(range2), "bytes=1-2 vs. BYTES=1-2");
            Assert.False(range1.Equals(range3), "bytes=1-2 vs. bytes=1-");
            Assert.False(range1.Equals(range4), "bytes=1-2 vs. bytes=-2");
            Assert.False(range1.Equals(range5), "bytes=1-2 vs. bytes=1-2,3-4");
            Assert.True(range5.Equals(range6), "bytes=1-2,3-4 vs. bytes=3-4,1-2");
            Assert.False(range1.Equals(range7), "bytes=1-2 vs. other=1-2");
        }
コード例 #3
0
        private void CheckValidTryParse(string input, RangeHeaderValue expectedResult)
        {
            RangeHeaderValue result = null;

            Assert.True(RangeHeaderValue.TryParse(input, out result));
            Assert.Equal(expectedResult, result);
        }
コード例 #4
0
        public void TryParse_SetOfValidValueStrings_ParsedCorrectly()
        {
            CheckValidTryParse(" bytes=1-2 ", new RangeHeaderValue(1, 2));

            var expected = new RangeHeaderValue();

            expected.Unit = "custom";
            expected.Ranges.Add(new RangeItemHeaderValue(null, 5));
            expected.Ranges.Add(new RangeItemHeaderValue(1, 4));
            CheckValidTryParse("custom = -  5 , 1 - 4 ,,", expected);
        }
コード例 #5
0
        public void ToString_UseDifferentRanges_AllSerializedCorrectly()
        {
            var range = new RangeHeaderValue();

            range.Unit = "myunit";
            range.Ranges.Add(new RangeItemHeaderValue(1, 3));
            Assert.Equal("myunit=1-3", range.ToString());

            range.Ranges.Add(new RangeItemHeaderValue(5, null));
            range.Ranges.Add(new RangeItemHeaderValue(null, 17));
            Assert.Equal("myunit=1-3, 5-, -17", range.ToString());
        }
コード例 #6
0
        public void Unit_GetAndSetValidAndInvalidValues_MatchExpectation()
        {
            var range = new RangeHeaderValue();

            range.Unit = "myunit";
            Assert.Equal("myunit", range.Unit);

            Assert.Throws <ArgumentException>(() => range.Unit = null);
            Assert.Throws <ArgumentException>(() => range.Unit = "");
            Assert.Throws <FormatException>(() => range.Unit   = " x");
            Assert.Throws <FormatException>(() => range.Unit   = "x ");
            Assert.Throws <FormatException>(() => range.Unit   = "x y");
        }
コード例 #7
0
        private static void CheckValidTryParse(string input, long?expectedFrom, long?expectedTo)
        {
            RangeHeaderValue result;

            Assert.True(RangeHeaderValue.TryParse("byte=" + input, out result), input);

            var ranges = result.Ranges.ToArray();

            Assert.Single(ranges);

            var range = ranges.First();

            Assert.Equal(expectedFrom, range.From);
            Assert.Equal(expectedTo, range.To);
        }
コード例 #8
0
        private static void CheckValidTryParse(string input, params Tuple <long?, long?>[] expectedRanges)
        {
            RangeHeaderValue result;

            Assert.True(RangeHeaderValue.TryParse("byte=" + input, out result), input);

            var ranges = result.Ranges.ToArray();

            Assert.Equal(expectedRanges.Length, ranges.Length);

            for (int i = 0; i < expectedRanges.Length; i++)
            {
                Assert.Equal(expectedRanges[i].Item1, ranges[i].From);
                Assert.Equal(expectedRanges[i].Item2, ranges[i].To);
            }
        }
コード例 #9
0
        private static int GetRangeLength(StringSegment input, int startIndex, out RangeHeaderValue parsedValue)
        {
            Contract.Requires(startIndex >= 0);

            parsedValue = null;

            if (StringSegment.IsNullOrEmpty(input) || (startIndex >= input.Length))
            {
                return(0);
            }

            // Parse the unit string: <unit> in '<unit>=<from1>-<to1>, <from2>-<to2>'
            var unitLength = ProtoRuleParser.GetTokenLength(input, startIndex);

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

            RangeHeaderValue result = new RangeHeaderValue();

            result._unit = input.Subsegment(startIndex, unitLength);
            var current = startIndex + unitLength;

            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            if ((current == input.Length) || (input[current] != '='))
            {
                return(0);
            }

            current++; // skip '=' separator
            current = current + ProtoRuleParser.GetWhitespaceLength(input, current);

            var rangesLength = RangeItemHeaderValue.GetRangeItemListLength(input, current, result.Ranges);

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

            current = current + rangesLength;
            Contract.Assert(current == input.Length, "GetRangeItemListLength() should consume the whole string or fail.");

            parsedValue = result;
            return(current - startIndex);
        }
コード例 #10
0
        public void GetHashCode_UseSameAndDifferentRanges_SameOrDifferentHashCodes()
        {
            var range1 = new RangeHeaderValue(1, 2);
            var range2 = new RangeHeaderValue(1, 2);

            range2.Unit = "BYTES";
            var range3 = new RangeHeaderValue(1, null);
            var range4 = new RangeHeaderValue(null, 2);
            var range5 = new RangeHeaderValue();

            range5.Ranges.Add(new RangeItemHeaderValue(1, 2));
            range5.Ranges.Add(new RangeItemHeaderValue(3, 4));
            var range6 = new RangeHeaderValue();

            range6.Ranges.Add(new RangeItemHeaderValue(3, 4)); // reverse order of range5
            range6.Ranges.Add(new RangeItemHeaderValue(1, 2));

            Assert.Equal(range1.GetHashCode(), range2.GetHashCode());
            Assert.NotEqual(range1.GetHashCode(), range3.GetHashCode());
            Assert.NotEqual(range1.GetHashCode(), range4.GetHashCode());
            Assert.NotEqual(range1.GetHashCode(), range5.GetHashCode());
            Assert.Equal(range5.GetHashCode(), range6.GetHashCode());
        }
コード例 #11
0
        [InlineData("-9999999999999999999")]     // 19-digit numbers outside the Int64 range.
        public void TryParse_DifferentInvalidScenarios_AllReturnFalse(string input)
        {
            RangeHeaderValue result;

            Assert.False(RangeHeaderValue.TryParse("byte=" + input, out result));
        }
コード例 #12
0
 private void CheckInvalidParse(string input)
 {
     Assert.Throws <FormatException>(() => RangeHeaderValue.Parse(input));
 }
コード例 #13
0
        private void CheckValidParse(string input, RangeHeaderValue expectedResult)
        {
            var result = RangeHeaderValue.Parse(input);

            Assert.Equal(expectedResult, result);
        }
コード例 #14
0
        public static bool TryParse(StringSegment input, out RangeHeaderValue parsedValue)
        {
            var index = 0;

            return(Parser.TryParseValue(input, ref index, out parsedValue));
        }