@@ -217,30 +217,42 @@ public ViewResultTest ShouldRenderDefaultPartialView()
217217
218218 #region File Results
219219
220- public FileResult ShouldRenderAnyFile ( string contentType = null )
220+ private static void EnsureContentTypeIsSame ( string actual , string expected )
221221 {
222- ValidateActionReturnType < FileResult > ( ) ;
223-
224- var fileResult = ( FileResult ) _actionResult ;
222+ if ( expected == null ) return ;
223+ if ( actual != expected )
224+ {
225+ throw new ActionResultAssertionException ( string . Format (
226+ "Expected file to be of content type '{0}', but instead was given '{1}'." , expected , actual ) ) ;
227+ }
228+ }
225229
226- if ( contentType != null && fileResult . ContentType != contentType )
230+ private static byte [ ] ConvertStreamToArray ( Stream stream )
231+ {
232+ using ( var memoryStream = new MemoryStream ( ) )
227233 {
228- throw new ActionResultAssertionException ( string . Format ( "Expected file to be of content type '{0}', but instead was given '{1}'." , contentType , fileResult . ContentType ) ) ;
234+ stream . CopyTo ( memoryStream ) ;
235+ stream . Position = 0 ;
236+ return memoryStream . ToArray ( ) ;
229237 }
238+ }
239+
240+ public FileResult ShouldRenderAnyFile ( string contentType = null )
241+ {
242+ ValidateActionReturnType < FileResult > ( ) ;
243+ var fileResult = ( FileResult ) _actionResult ;
230244
245+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
246+
231247 return fileResult ;
232248 }
233249
234250 public FileContentResult ShouldRenderFileContents ( byte [ ] contents = null , string contentType = null )
235251 {
236252 ValidateActionReturnType < FileContentResult > ( ) ;
237-
238253 var fileResult = ( FileContentResult ) _actionResult ;
239254
240- if ( contentType != null && fileResult . ContentType != contentType )
241- {
242- throw new ActionResultAssertionException ( string . Format ( "Expected file to be of content type '{0}', but instead was given '{1}'." , contentType , fileResult . ContentType ) ) ;
243- }
255+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
244256
245257 if ( contents != null && ! fileResult . FileContents . SequenceEqual ( contents ) )
246258 {
@@ -256,45 +268,37 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
256268 public FileContentResult ShouldRenderFileContents ( string contents , string contentType = null , Encoding encoding = null )
257269 {
258270 ValidateActionReturnType < FileContentResult > ( ) ;
259-
260271 var fileResult = ( FileContentResult ) _actionResult ;
261272
262- if ( contentType != null && fileResult . ContentType != contentType )
263- {
264- throw new ActionResultAssertionException (
265- string . Format ( "Expected file to be of content type '{0}', but instead was given '{1}'." , contentType ,
266- fileResult . ContentType ) ) ;
267- }
273+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
268274
269275 if ( encoding == null )
270276 encoding = Encoding . UTF8 ;
271277
272278 var reconstitutedText = encoding . GetString ( fileResult . FileContents ) ;
273279 if ( contents != reconstitutedText )
274280 {
275- throw new ActionResultAssertionException ( string . Format ( "Expected file contents to be \" {0}\" , but instead was \" {1}\" ." , contents , reconstitutedText ) ) ;
281+ throw new ActionResultAssertionException ( string . Format (
282+ "Expected file contents to be \" {0}\" , but instead was \" {1}\" ." ,
283+ contents ,
284+ reconstitutedText ) ) ;
276285 }
277286
278287 return fileResult ;
279288 }
280289
281290 public FileStreamResult ShouldRenderFileStream ( byte [ ] content , string contentType = null )
282291 {
283- return ShouldRenderFileStream ( new MemoryStream ( content ) , contentType ) ;
292+ var reconstitutedStream = new MemoryStream ( content ) ;
293+ return ShouldRenderFileStream ( reconstitutedStream , contentType ) ;
284294 }
285295
286296 public FileStreamResult ShouldRenderFileStream ( Stream stream = null , string contentType = null )
287297 {
288298 ValidateActionReturnType < FileStreamResult > ( ) ;
289299 var fileResult = ( FileStreamResult ) _actionResult ;
290300
291- if ( contentType != null && fileResult . ContentType != contentType )
292- {
293- throw new ActionResultAssertionException ( string . Format (
294- "Expected stream to be of content type '{0}', but instead was given '{1}'." ,
295- contentType ,
296- fileResult . ContentType ) ) ;
297- }
301+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
298302
299303 if ( stream != null )
300304 {
@@ -304,7 +308,7 @@ public FileStreamResult ShouldRenderFileStream(Stream stream = null, string cont
304308 if ( ! expected . SequenceEqual ( actual ) )
305309 {
306310 throw new ActionResultAssertionException ( string . Format (
307- "Expected stream contents to be equal to [{0}], but instead was given [{1}]." ,
311+ "Expected file contents to be equal to [{0}], but instead was given [{1}]." ,
308312 string . Join ( ", " , expected ) ,
309313 string . Join ( ", " , actual ) ) ) ;
310314 }
@@ -318,13 +322,7 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
318322 ValidateActionReturnType < FileStreamResult > ( ) ;
319323 var fileResult = ( FileStreamResult ) _actionResult ;
320324
321- if ( contentType != null && fileResult . ContentType != contentType )
322- {
323- throw new ActionResultAssertionException ( string . Format (
324- "Expected stream to be of content type '{0}', but instead was given '{1}'." ,
325- contentType ,
326- fileResult . ContentType ) ) ;
327- }
325+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
328326
329327 if ( encoding == null )
330328 encoding = Encoding . UTF8 ;
@@ -341,30 +339,19 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
341339 return fileResult ;
342340 }
343341
344- private static byte [ ] ConvertStreamToArray ( Stream stream )
345- {
346- using ( var memoryStream = new MemoryStream ( ) )
347- {
348- stream . CopyTo ( memoryStream ) ;
349- stream . Position = 0 ;
350- return memoryStream . ToArray ( ) ;
351- }
352- }
353-
354342 public FilePathResult ShouldRenderFilePath ( string fileName = null , string contentType = null )
355343 {
356344 ValidateActionReturnType < FilePathResult > ( ) ;
357-
358345 var fileResult = ( FilePathResult ) _actionResult ;
359346
360- if ( contentType != null && fileResult . ContentType != contentType )
361- {
362- throw new ActionResultAssertionException ( string . Format ( "Expected file to be of content type '{0}', but instead was given '{1}'." , contentType , fileResult . ContentType ) ) ;
363- }
347+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
364348
365349 if ( fileName != null && fileName != fileResult . FileName )
366350 {
367- throw new ActionResultAssertionException ( string . Format ( "Expected file name to be '{0}', but instead was given '{1}'." , fileName , fileResult . FileName ) ) ;
351+ throw new ActionResultAssertionException ( string . Format (
352+ "Expected file name to be '{0}', but instead was given '{1}'." ,
353+ fileName ,
354+ fileResult . FileName ) ) ;
368355 }
369356
370357 return fileResult ;
0 commit comments