/// <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() == 0)
.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;
}