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); }
private bool ValidateParameters(MultipartFormDataParser parser) { var actualParameters = parser.Parameters.GroupBy(p => p.Name); var expectedParameters = ExpectedParams.GroupBy(p => p.Name); // Make sure the number of actual parameters matches the number of expected parameters if (actualParameters.Count() != expectedParameters.Count()) { return(false); } // Validate that each expected value has a corresponding actual value return(actualParameters.Zip(expectedParameters, Tuple.Create).All(t => { // Make sure the name of the actual parameter matches the name of the expected parameter if (t.Item1.Key != t.Item2.Key) { return false; } var actualValues = t.Item1.Select(i => i.Data); var expectedValues = t.Item2.Select(i => i.Data); // Make sure the number of actual values matches the number of expected values if (actualValues.Count() != expectedValues.Count()) { return false; } // Validate that each expected value has a corresponding actual value return actualValues.Zip(expectedValues, Tuple.Create).All(v => v.Item1 == v.Item2); })); }
/// <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); }
/// <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); }