@@ -217,30 +217,42 @@ public ViewResultTest ShouldRenderDefaultPartialView()
217
217
218
218
#region File Results
219
219
220
- public FileResult ShouldRenderAnyFile ( string contentType = null )
220
+ private static void EnsureContentTypeIsSame ( string actual , string expected )
221
221
{
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
+ }
225
229
226
- if ( contentType != null && fileResult . ContentType != contentType )
230
+ private static byte [ ] ConvertStreamToArray ( Stream stream )
231
+ {
232
+ using ( var memoryStream = new MemoryStream ( ) )
227
233
{
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 ( ) ;
229
237
}
238
+ }
239
+
240
+ public FileResult ShouldRenderAnyFile ( string contentType = null )
241
+ {
242
+ ValidateActionReturnType < FileResult > ( ) ;
243
+ var fileResult = ( FileResult ) _actionResult ;
230
244
245
+ EnsureContentTypeIsSame ( fileResult . ContentType , contentType ) ;
246
+
231
247
return fileResult ;
232
248
}
233
249
234
250
public FileContentResult ShouldRenderFileContents ( byte [ ] contents = null , string contentType = null )
235
251
{
236
252
ValidateActionReturnType < FileContentResult > ( ) ;
237
-
238
253
var fileResult = ( FileContentResult ) _actionResult ;
239
254
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 ) ;
244
256
245
257
if ( contents != null && ! fileResult . FileContents . SequenceEqual ( contents ) )
246
258
{
@@ -256,45 +268,37 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
256
268
public FileContentResult ShouldRenderFileContents ( string contents , string contentType = null , Encoding encoding = null )
257
269
{
258
270
ValidateActionReturnType < FileContentResult > ( ) ;
259
-
260
271
var fileResult = ( FileContentResult ) _actionResult ;
261
272
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 ) ;
268
274
269
275
if ( encoding == null )
270
276
encoding = Encoding . UTF8 ;
271
277
272
278
var reconstitutedText = encoding . GetString ( fileResult . FileContents ) ;
273
279
if ( contents != reconstitutedText )
274
280
{
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 ) ) ;
276
285
}
277
286
278
287
return fileResult ;
279
288
}
280
289
281
290
public FileStreamResult ShouldRenderFileStream ( byte [ ] content , string contentType = null )
282
291
{
283
- return ShouldRenderFileStream ( new MemoryStream ( content ) , contentType ) ;
292
+ var reconstitutedStream = new MemoryStream ( content ) ;
293
+ return ShouldRenderFileStream ( reconstitutedStream , contentType ) ;
284
294
}
285
295
286
296
public FileStreamResult ShouldRenderFileStream ( Stream stream = null , string contentType = null )
287
297
{
288
298
ValidateActionReturnType < FileStreamResult > ( ) ;
289
299
var fileResult = ( FileStreamResult ) _actionResult ;
290
300
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 ) ;
298
302
299
303
if ( stream != null )
300
304
{
@@ -304,7 +308,7 @@ public FileStreamResult ShouldRenderFileStream(Stream stream = null, string cont
304
308
if ( ! expected . SequenceEqual ( actual ) )
305
309
{
306
310
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}]." ,
308
312
string . Join ( ", " , expected ) ,
309
313
string . Join ( ", " , actual ) ) ) ;
310
314
}
@@ -318,13 +322,7 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
318
322
ValidateActionReturnType < FileStreamResult > ( ) ;
319
323
var fileResult = ( FileStreamResult ) _actionResult ;
320
324
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 ) ;
328
326
329
327
if ( encoding == null )
330
328
encoding = Encoding . UTF8 ;
@@ -341,30 +339,19 @@ public FileStreamResult ShouldRenderFileStream(string contents, string contentTy
341
339
return fileResult ;
342
340
}
343
341
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
-
354
342
public FilePathResult ShouldRenderFilePath ( string fileName = null , string contentType = null )
355
343
{
356
344
ValidateActionReturnType < FilePathResult > ( ) ;
357
-
358
345
var fileResult = ( FilePathResult ) _actionResult ;
359
346
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 ) ;
364
348
365
349
if ( fileName != null && fileName != fileResult . FileName )
366
350
{
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 ) ) ;
368
355
}
369
356
370
357
return fileResult ;
0 commit comments