Ejemplo n.º 1
0
        public void TryParseFormValues_MultiSegmentFastPathWorks()
        {
            var readOnlySequence = ReadOnlySequenceFactory.CreateSegments(Encoding.UTF8.GetBytes("foo=bar&"), Encoding.UTF8.GetBytes("baz=boo"));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null);

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
            Assert.True(readOnlySequence.IsEmpty);

            Assert.Equal(2, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("bar", dict["foo"]);
            Assert.Equal("boo", dict["baz"]);
        }
Ejemplo n.º 2
0
        public async Task ResetPipeWorks()
        {
            // Same test that is in the benchmark
            var pipe  = new Pipe();
            var bytes = Encoding.UTF8.GetBytes("foo=bar&baz=boo");

            for (var i = 0; i < 1000; i++)
            {
                pipe.Writer.Write(bytes);
                pipe.Writer.Complete();
                var formReader = new FormPipeReader(pipe.Reader);
                await formReader.ReadFormAsync();

                pipe.Reader.Complete();
                pipe.Reset();
            }
        }
Ejemplo n.º 3
0
        public void TryParseFormValues_MultiSegmentWithArrayPoolAcrossSegmentsWorks(Encoding encoding)
        {
            var readOnlySequence = ReadOnlySequenceFactory.SegmentPerByteFactory.CreateWithContent(encoding.GetBytes("foo=bar&baz=bo" + new string('a', 128)));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null, encoding);

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
            Assert.True(readOnlySequence.IsEmpty);

            Assert.Equal(2, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("bar", dict["foo"]);
            Assert.Equal("bo" + new string('a', 128), dict["baz"]);
        }
Ejemplo n.º 4
0
        public void TryParseFormValues_SplitAcrossSegmentsThatNeedDecodingWorks(Encoding encoding)
        {
            var readOnlySequence = ReadOnlySequenceFactory.SegmentPerByteFactory.CreateWithContent(encoding.GetBytes("\"%-.<>\\^_`{|}~=\"%-.<>\\^_`{|}~&\"%-.<>\\^_`{|}=wow"));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null, encoding);

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
            Assert.True(readOnlySequence.IsEmpty);

            Assert.Equal(2, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("\"%-.<>\\^_`{|}~", dict["\"%-.<>\\^_`{|}~"]);
            Assert.Equal("wow", dict["\"%-.<>\\^_`{|}"]);
        }
Ejemplo n.º 5
0
        public void TryParseFormValues_DecodedPlusesWorks(Encoding encoding)
        {
            var readOnlySequence = ReadOnlySequenceFactory.SingleSegmentFactory.CreateWithContent(encoding.GetBytes("++%2B=+++%2B&++++=++++&+="));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null, encoding);

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);

            Assert.Equal(3, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("    ", dict["    "]);
            Assert.Equal("   +", dict["  +"]);
            Assert.Equal("", dict[" "]);
        }
Ejemplo n.º 6
0
        public void TryParseFormValues_MultiSegmentSplitAcrossSegmentsWorks(Encoding encoding)
        {
            var readOnlySequence = ReadOnlySequenceFactory.SingleSegmentFactory.CreateWithContent(encoding.GetBytes("foo=bar&baz=boo&t="));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null, encoding);

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);

            Assert.Equal(3, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("bar", dict["foo"]);
            Assert.Equal("boo", dict["baz"]);
            Assert.Equal("", dict["t"]);
        }
Ejemplo n.º 7
0
        public void ParseFormWithIncompleteValueWhenIsFinalBlockSucceeds(ReadOnlySequence <byte> readOnlySequence)
        {
            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null)
            {
                ValueLengthLimit = 3
            };

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: false);

            IDictionary <string, StringValues> values = accumulator.GetResults();

            Assert.Single(values);
            Assert.Contains("fo", values);
            Assert.Equal("bar", values["fo"]);
        }
Ejemplo n.º 8
0
        public void TryParseFormValues_MultiSegmentSplitAcrossSegmentsWithPlusesWorks(Encoding encoding)
        {
            var readOnlySequence = ReadOnlySequenceFactory.SegmentPerByteFactory.CreateWithContent(encoding.GetBytes("+++=+++&++++=++++&+="));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null, encoding);

            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
            Assert.True(readOnlySequence.IsEmpty);

            Assert.Equal(3, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("    ", dict["    "]);
            Assert.Equal("   ", dict["   "]);
            Assert.Equal("", dict[" "]);
        }
Ejemplo n.º 9
0
        public void TryParseFormValues_SplitAcrossSegmentsWorks_LimitsCanBeLarge(Encoding encoding)
        {
            var readOnlySequence = ReadOnlySequenceFactory.SegmentPerByteFactory.CreateWithContent(encoding.GetBytes("foo=bar&baz=boo&t="));

            KeyValueAccumulator accumulator = default;

            var formReader = new FormPipeReader(null, encoding);

            formReader.KeyLengthLimit   = int.MaxValue;
            formReader.ValueLengthLimit = int.MaxValue;
            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: false);
            formReader.ParseFormValues(ref readOnlySequence, ref accumulator, isFinalBlock: true);
            Assert.True(readOnlySequence.IsEmpty);

            Assert.Equal(3, accumulator.KeyCount);
            var dict = accumulator.GetResults();

            Assert.Equal("bar", dict["foo"]);
            Assert.Equal("boo", dict["baz"]);
            Assert.Equal("", dict["t"]);
        }
Ejemplo n.º 10
0
 internal virtual Task <Dictionary <string, StringValues> > ReadFormAsync(FormPipeReader reader)
 {
     return(reader.ReadFormAsync());
 }