@@ -59,7 +59,20 @@ public TestData(
5959 /// </returns>
6060 public bool Validate ( MultipartFormDataParser parser )
6161 {
62- // Deal with all the parameters who are only expected to have one value.
62+ var result = ValidateSingleValueParameters ( parser ) ;
63+ result &= ValidateMultipleValuesParameters ( parser ) ;
64+ result &= ValidateFiles ( parser ) ;
65+
66+ return result ;
67+ }
68+
69+ #endregion
70+
71+ #region Private methods
72+
73+ private bool ValidateSingleValueParameters ( MultipartFormDataParser parser )
74+ {
75+ // Deal with the parameters that are expected to have only one value.
6376 var expectedParametersWithSingleValue = ExpectedParams
6477 . GroupBy ( p => p . Name )
6578 . Where ( g => g . Count ( ) == 1 )
@@ -86,10 +99,15 @@ public bool Validate(MultipartFormDataParser parser)
8699 }
87100 }
88101
89- // Deal with the parameters who are expected to have more then one value
102+ return true ;
103+ }
104+
105+ private bool ValidateMultipleValuesParameters ( MultipartFormDataParser parser )
106+ {
107+ // Deal with the parameters that are expected to have more than one value
90108 var expectedParametersWithMultiValues = ExpectedParams
91- . GroupBy ( p => p . Name )
92- . Where ( a => a . Count ( ) > 1 ) ;
109+ . GroupBy ( p => p . Name )
110+ . Where ( a => a . Count ( ) > 1 ) ;
93111
94112 foreach ( var expectedParameters in expectedParametersWithMultiValues )
95113 {
@@ -107,60 +125,48 @@ public bool Validate(MultipartFormDataParser parser)
107125 }
108126 }
109127
110- // Validate files
111- foreach ( var filePart in ExpectedFileData )
128+ return true ;
129+ }
130+
131+ private bool ValidateFiles ( MultipartFormDataParser parser )
132+ {
133+ // Validate files.
134+
135+ // PLEASE NOTE: we can't rely on the name and/or the file name because they are not guaranteed to be unique.
136+ // Therefore we assume that the first expected file should match the first actual file,
137+ // the second expected file should match the second actual files, etc.
138+
139+ if ( ExpectedFileData . Count != parser . Files . Count ) return false ;
140+
141+ for ( int i = 0 ; i < ExpectedFileData . Count ; i ++ )
112142 {
113- var foundPairMatch = false ;
114- foreach ( var file in parser . Files )
143+ var expectedFile = ExpectedFileData [ i ] ;
144+ var actualFile = parser . Files [ i ] ;
145+
146+ if ( expectedFile . Name != actualFile . Name ) return false ;
147+ if ( expectedFile . FileName != actualFile . FileName ) return false ;
148+ if ( expectedFile . ContentType != actualFile . ContentType ) return false ;
149+ if ( expectedFile . ContentDisposition != actualFile . ContentDisposition ) return false ;
150+
151+ // Read the data from the files and see if it's the same
152+ if ( expectedFile . Data . CanSeek && expectedFile . Data . Position != 0 ) expectedFile . Data . Position = 0 ;
153+ if ( actualFile . Data . CanSeek && actualFile . Data . Position != 0 ) actualFile . Data . Position = 0 ;
154+
155+ string expectedFileData ;
156+ // The last boolean parameter MUST be set to true: it ensures the stream is left open
157+ using ( var reader = new StreamReader ( expectedFile . Data , Encoding . UTF8 , false , 1024 , true ) )
158+ {
159+ expectedFileData = reader . ReadToEnd ( ) ;
160+ }
161+
162+ string actualFileData ;
163+ // The last boolean parameter MUST be set to true: it ensures the stream is left open
164+ using ( var reader = new StreamReader ( actualFile . Data , Encoding . UTF8 , false , 1024 , true ) )
115165 {
116- if ( filePart . Name == file . Name )
117- {
118- foundPairMatch = true ;
119-
120- FilePart expectedFile = filePart ;
121- FilePart actualFile = file ;
122-
123- if ( expectedFile . Name != actualFile . Name || expectedFile . FileName != actualFile . FileName )
124- {
125- return false ;
126- }
127-
128- if ( expectedFile . ContentType != actualFile . ContentType ||
129- expectedFile . ContentDisposition != actualFile . ContentDisposition )
130- {
131- return false ;
132- }
133-
134- // Read the data from the files and see if it's the same
135- if ( expectedFile . Data . CanSeek )
136- {
137- expectedFile . Data . Position = 0 ;
138- }
139-
140- string expectedFileData ;
141- // The last boolean parameter MUST be set to true: it ensures the stream is left open
142- using ( var reader = new StreamReader ( expectedFile . Data , Encoding . UTF8 , false , 1024 , true ) )
143- {
144- expectedFileData = reader . ReadToEnd ( ) ;
145- }
146-
147- string actualFileData ;
148- // The last boolean parameter MUST be set to true: it ensures the stream is left open
149- using ( var reader = new StreamReader ( actualFile . Data , Encoding . UTF8 , false , 1024 , true ) )
150- {
151- actualFileData = reader . ReadToEnd ( ) ;
152- }
153-
154- if ( expectedFileData != actualFileData )
155- {
156- return false ;
157- }
158-
159- break ;
160- }
166+ actualFileData = reader . ReadToEnd ( ) ;
161167 }
162168
163- if ( ! foundPairMatch )
169+ if ( expectedFileData != actualFileData )
164170 {
165171 return false ;
166172 }
0 commit comments