private bool ValidateSingleValueParameters(MultipartFormDataParser parser)
        {
            // Deal with the parameters that are expected to have only one value.
            var expectedParametersWithSingleValue = ExpectedParams
                                                    .GroupBy(p => p.Name)
                                                    .Where(g => g.Count() == 1)
                                                    .Select(g => g.Single());

            foreach (var expectedParameter in expectedParametersWithSingleValue)
            {
                if (!parser.HasParameter(expectedParameter.Name))
                {
                    return(false);
                }

                var actualValue           = parser.GetParameterValue(expectedParameter.Name);
                var actualValueFromValues = parser.GetParameterValues(expectedParameter.Name).Single();

                if (actualValue != actualValueFromValues)
                {
                    return(false);
                }

                if (expectedParameter.Data != actualValue)
                {
                    return(false);
                }
            }

            return(true);
        }
        private bool ValidateMultipleValuesParameters(MultipartFormDataParser parser)
        {
            // Deal with the parameters that are expected to have more than one value
            var expectedParametersWithMultiValues = ExpectedParams
                                                    .GroupBy(p => p.Name)
                                                    .Where(a => a.Count() > 1);

            foreach (var expectedParameters in expectedParametersWithMultiValues)
            {
                var key = expectedParameters.Key;
                if (!parser.HasParameter(key))
                {
                    return(false);
                }

                var actualValues = parser.GetParameterValues(key);

                if (actualValues.Count() != expectedParameters.Count() || actualValues.Zip(expectedParameters, Tuple.Create).Any(t => t.Item1 != t.Item2.Data))
                {
                    return(false);
                }
            }

            return(true);
        }
        public void CorrectlyHandlesMultilineParameter()
        {
            string request = TestUtil.TrimAllLines(
                @"-----------------------------41952539122868
                Content-Disposition: form-data; name=""multilined""

                line 1
                line 2
                line 3
                -----------------------------41952539122868--");

            using (Stream stream = TestUtil.StringToStream(request, Encoding.UTF8))
            {
                var parser = new MultipartFormDataParser(stream, Encoding.UTF8);
                Assert.AreEqual(parser.GetParameterValue("multilined"), "line 1\r\nline 2\r\nline 3");
                Assert.AreEqual(parser.GetParameterValues("multilined").First(), "line 1\r\nline 2\r\nline 3");
            }
        }
            /// <summary>
            ///     Validates the output of the parser against the expected outputs for
            ///     this test
            /// </summary>
            /// <param name="parser">
            ///     The parser to validate.
            /// </param>
            /// <returns>
            ///     The <see cref="bool" /> representing if this test passed.
            /// </returns>
            public bool Validate(MultipartFormDataParser parser)
            {
                // Deal with all the parameters who are only expected to have one value.
                var expectedParametersWithSingleValue = ExpectedParams
                                                        .GroupBy(p => p.Name)
                                                        .Where(g => g.Count() == 1)
                                                        .Select(g => g.Single());

                foreach (var expectedParameter in expectedParametersWithSingleValue)
                {
                    if (!parser.HasParameter(expectedParameter.Name))
                    {
                        return(false);
                    }

                    var actualValue           = parser.GetParameterValue(expectedParameter.Name);
                    var actualValueFromValues = parser.GetParameterValues(expectedParameter.Name).Single();

                    if (actualValue != actualValueFromValues)
                    {
                        Console.WriteLine("GetParameterValue vs. GetParameterValues mismatch! ({0} != {1})", actualValue, actualValueFromValues);
                        return(false);
                    }

                    Console.WriteLine("Expected {0} = {1}. Found {2} = {3}", expectedParameter.Name, expectedParameter.Data, expectedParameter.Name, actualValue);

                    if (expectedParameter.Data != actualValue)
                    {
                        return(false);
                    }
                }

                // Deal with the parameters who are expected to have more then one value
                var expectedParametersWithMultiValues = ExpectedParams
                                                        .GroupBy(p => p.Name)
                                                        .Where(a => a.Count() > 1);

                foreach (var expectedParameters in expectedParametersWithMultiValues)
                {
                    var key = expectedParameters.Key;
                    if (!parser.HasParameter(key))
                    {
                        return(false);
                    }

                    var actualValues = parser.GetParameterValues(key);

                    Console.WriteLine("Expected {0} = {1}. Found {2} = {3}",
                                      key,
                                      string.Join(",", expectedParameters.Select(p => p.Data)),
                                      key,
                                      string.Join(",", actualValues)
                                      );

                    if (actualValues.Count() != expectedParameters.Count() || actualValues.Zip(expectedParameters, Tuple.Create).Any(t => t.Item1 != t.Item2.Data))
                    {
                        return(false);
                    }
                }

                // Validate files
                foreach (var filePart in ExpectedFileData)
                {
                    var foundPairMatch = false;
                    foreach (var file in parser.Files)
                    {
                        if (filePart.Name == file.Name)
                        {
                            foundPairMatch = true;

                            FilePart expectedFile = filePart;
                            FilePart actualFile   = file;

                            if (expectedFile.Name != actualFile.Name || expectedFile.FileName != actualFile.FileName)
                            {
                                return(false);
                            }

                            if (expectedFile.ContentType != actualFile.ContentType ||
                                expectedFile.ContentDisposition != actualFile.ContentDisposition)
                            {
                                return(false);
                            }

                            // Read the data from the files and see if it's the same
                            var    reader           = new StreamReader(expectedFile.Data);
                            string expectedFileData = reader.ReadToEnd();

                            reader = new StreamReader(actualFile.Data);
                            string actualFileData = reader.ReadToEnd();

                            if (expectedFileData != actualFileData)
                            {
                                return(false);
                            }

                            break;
                        }
                    }

                    if (!foundPairMatch)
                    {
                        return(false);
                    }
                }

                return(true);
            }
Ejemplo n.º 5
0
        /// <summary>
        ///     Validates the output of the parser against the expected outputs for
        ///     this test
        /// </summary>
        /// <param name="parser">
        ///     The parser to validate.
        /// </param>
        /// <returns>
        ///     The <see cref="bool" /> representing if this test passed.
        /// </returns>
        public bool Validate(MultipartFormDataParser parser)
        {
            // Deal with all the parameters who are only expected to have one value.
            var expectedParametersWithSingleValue = ExpectedParams
                                                    .GroupBy(p => p.Name)
                                                    .Where(g => g.Count() == 1)
                                                    .Select(g => g.Single());

            foreach (var expectedParameter in expectedParametersWithSingleValue)
            {
                if (!parser.HasParameter(expectedParameter.Name))
                {
                    return(false);
                }

                var actualValue           = parser.GetParameterValue(expectedParameter.Name);
                var actualValueFromValues = parser.GetParameterValues(expectedParameter.Name).Single();

                if (actualValue != actualValueFromValues)
                {
                    return(false);
                }

                if (expectedParameter.Data != actualValue)
                {
                    return(false);
                }
            }

            // Deal with the parameters who are expected to have more then one value
            var expectedParametersWithMultiValues = ExpectedParams
                                                    .GroupBy(p => p.Name)
                                                    .Where(a => a.Count() > 1);

            foreach (var expectedParameters in expectedParametersWithMultiValues)
            {
                var key = expectedParameters.Key;
                if (!parser.HasParameter(key))
                {
                    return(false);
                }

                var actualValues = parser.GetParameterValues(key);

                if (actualValues.Count() != expectedParameters.Count() || actualValues.Zip(expectedParameters, Tuple.Create).Any(t => t.Item1 != t.Item2.Data))
                {
                    return(false);
                }
            }

            // Validate files
            foreach (var filePart in ExpectedFileData)
            {
                var foundPairMatch = false;
                foreach (var file in parser.Files)
                {
                    if (filePart.Name == file.Name)
                    {
                        foundPairMatch = true;

                        FilePart expectedFile = filePart;
                        FilePart actualFile   = file;

                        if (expectedFile.Name != actualFile.Name || expectedFile.FileName != actualFile.FileName)
                        {
                            return(false);
                        }

                        if (expectedFile.ContentType != actualFile.ContentType ||
                            expectedFile.ContentDisposition != actualFile.ContentDisposition)
                        {
                            return(false);
                        }

                        // Read the data from the files and see if it's the same
                        if (expectedFile.Data.CanSeek)
                        {
                            expectedFile.Data.Position = 0;
                        }

                        string expectedFileData;
                        // The last boolean parameter MUST be set to true: it ensures the stream is left open
                        using (var reader = new StreamReader(expectedFile.Data, Encoding.UTF8, false, 1024, true))
                        {
                            expectedFileData = reader.ReadToEnd();
                        }

                        string actualFileData;
                        // The last boolean parameter MUST be set to true: it ensures the stream is left open
                        using (var reader = new StreamReader(actualFile.Data, Encoding.UTF8, false, 1024, true))
                        {
                            actualFileData = reader.ReadToEnd();
                        }

                        if (expectedFileData != actualFileData)
                        {
                            return(false);
                        }

                        break;
                    }
                }

                if (!foundPairMatch)
                {
                    return(false);
                }
            }

            return(true);
        }