@@ -80,7 +80,7 @@ public async Task ObjectResult_WithMultipleContentTypesAndAcceptHeaders_Performs
80
80
result . ContentTypes = contentTypes . Select ( contentType => MediaTypeHeaderValue . Parse ( contentType ) ) . ToList ( ) ;
81
81
result . Formatters = new List < OutputFormatter >
82
82
{
83
- new TestOutputFormatter ( ) ,
83
+ new CannotWriteFormatter ( ) ,
84
84
new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ,
85
85
} ;
86
86
@@ -146,7 +146,7 @@ public async Task ObjectResult_MultipleContentTypes_PicksFirstFormatterWhichSupp
146
146
. ToList ( ) ;
147
147
result . Formatters = new List < OutputFormatter >
148
148
{
149
- new TestOutputFormatter ( ) ,
149
+ new CannotWriteFormatter ( ) ,
150
150
new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ,
151
151
} ;
152
152
// Act
@@ -180,7 +180,7 @@ public async Task ObjectResult_MultipleFormattersSupportingTheSameContentType_Se
180
180
{
181
181
mockFormatter . Object ,
182
182
new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ,
183
- new TestOutputFormatter ( )
183
+ new CannotWriteFormatter ( )
184
184
} ;
185
185
// Act
186
186
await result . ExecuteResultAsync ( actionContext ) ;
@@ -208,7 +208,7 @@ public async Task ObjectResult_NoContentTypeSetWithAcceptHeaders_PicksFormatterO
208
208
// Set more than one formatters. The test output formatter throws on write.
209
209
result . Formatters = new List < OutputFormatter >
210
210
{
211
- new TestOutputFormatter ( ) ,
211
+ new CannotWriteFormatter ( ) ,
212
212
new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ,
213
213
} ;
214
214
@@ -239,7 +239,7 @@ public async Task ObjectResult_NoContentTypeSetWithNoAcceptHeaders_PicksFormatte
239
239
// Set more than one formatters. The test output formatter throws on write.
240
240
result . Formatters = new List < OutputFormatter >
241
241
{
242
- new TestOutputFormatter ( ) ,
242
+ new CannotWriteFormatter ( ) ,
243
243
new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ,
244
244
} ;
245
245
// Act
@@ -250,6 +250,65 @@ public async Task ObjectResult_NoContentTypeSetWithNoAcceptHeaders_PicksFormatte
250
250
httpResponse . VerifySet ( r => r . ContentType = expectedContentType ) ;
251
251
}
252
252
253
+ [ Fact ]
254
+ public async Task
255
+ ObjectResult_NoContentTypeSetWithNoAcceptHeadersAndNoRequestContentType_PicksFirstFormatterWhichCanWrite ( )
256
+ {
257
+ // Arrange
258
+ var stream = new MemoryStream ( ) ;
259
+ var expectedContentType = "application/json;charset=utf-8" ;
260
+ var httpResponse = new Mock < HttpResponse > ( ) ;
261
+ httpResponse . SetupProperty < string > ( o => o . ContentType ) ;
262
+ httpResponse . SetupGet ( r => r . Body ) . Returns ( stream ) ;
263
+
264
+ var actionContext = CreateMockActionContext ( httpResponse . Object ,
265
+ requestAcceptHeader : null ,
266
+ requestContentType : null ) ;
267
+ var input = "testInput" ;
268
+ var result = new ObjectResult ( input ) ;
269
+
270
+ // Set more than one formatters. The test output formatter throws on write.
271
+ result . Formatters = new List < OutputFormatter >
272
+ {
273
+ new CannotWriteFormatter ( ) ,
274
+ new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ,
275
+ } ;
276
+ // Act
277
+ await result . ExecuteResultAsync ( actionContext ) ;
278
+
279
+ // Assert
280
+ // Asserts that content type is not text/custom.
281
+ httpResponse . VerifySet ( r => r . ContentType = expectedContentType ) ;
282
+ }
283
+
284
+ [ Fact ]
285
+ public async Task ObjectResult_NoFormatterFound_Returns406 ( )
286
+ {
287
+ // Arrange
288
+ var stream = new MemoryStream ( ) ;
289
+ var httpResponse = new Mock < HttpResponse > ( ) ;
290
+ httpResponse . SetupProperty < string > ( o => o . ContentType ) ;
291
+ httpResponse . SetupGet ( r => r . Body ) . Returns ( stream ) ;
292
+
293
+ var actionContext = CreateMockActionContext ( httpResponse . Object ,
294
+ requestAcceptHeader : null ,
295
+ requestContentType : null ) ;
296
+ var input = "testInput" ;
297
+ var result = new ObjectResult ( input ) ;
298
+
299
+ // Set more than one formatters. The test output formatter throws on write.
300
+ result . Formatters = new List < OutputFormatter >
301
+ {
302
+ new CannotWriteFormatter ( ) ,
303
+ } ;
304
+ // Act
305
+ await result . ExecuteResultAsync ( actionContext ) ;
306
+
307
+ // Assert
308
+ // Asserts that content type is not text/custom.
309
+ httpResponse . VerifySet ( r => r . StatusCode = 406 ) ;
310
+ }
311
+
253
312
// TODO: Disabling since this scenario is no longer dealt with in object result.
254
313
// Re-enable once we do.
255
314
//[Fact]
@@ -300,7 +359,7 @@ public async Task ObjectResult_Execute_CallsJsonResult_SetsContent()
300
359
var formatterContext = new OutputFormatterContext ( )
301
360
{
302
361
ActionContext = tempActionContext ,
303
- ObjectResult = new ObjectResult ( nonStringValue ) ,
362
+ Object = nonStringValue ,
304
363
DeclaredType = nonStringValue . GetType ( )
305
364
} ;
306
365
var formatter = new JsonOutputFormatter ( JsonOutputFormatter . CreateDefaultSettings ( ) , true ) ;
@@ -352,9 +411,9 @@ private static Mock<HttpResponse> GetMockHttpResponse()
352
411
return httpResponse ;
353
412
}
354
413
355
- private static Mock < TestOutputFormatter > GetMockFormatter ( )
414
+ private static Mock < CannotWriteFormatter > GetMockFormatter ( )
356
415
{
357
- var mockFormatter = new Mock < TestOutputFormatter > ( ) ;
416
+ var mockFormatter = new Mock < CannotWriteFormatter > ( ) ;
358
417
mockFormatter . Setup ( o => o . CanWriteResult ( It . IsAny < OutputFormatterContext > ( ) ,
359
418
It . IsAny < MediaTypeHeaderValue > ( ) ) )
360
419
. Returns ( true ) ;
@@ -378,13 +437,31 @@ private static IServiceProvider GetServiceProvider()
378
437
return serviceCollection . BuildServiceProvider ( ) ;
379
438
}
380
439
381
- public class TestOutputFormatter : OutputFormatter
440
+ private class UnsupportedOutputFormatter : OutputFormatter
441
+ {
442
+ public override bool CanWriteResult ( OutputFormatterContext context , MediaTypeHeaderValue contentType )
443
+ {
444
+ return false ;
445
+ }
446
+
447
+ public override Task WriteAsync ( OutputFormatterContext context , CancellationToken cancellationToken )
448
+ {
449
+ throw new NotImplementedException ( ) ;
450
+ }
451
+ }
452
+
453
+ public class CannotWriteFormatter : OutputFormatter
382
454
{
383
- public TestOutputFormatter ( )
455
+ public CannotWriteFormatter ( )
384
456
{
385
457
SupportedMediaTypes . Add ( MediaTypeHeaderValue . Parse ( "text/custom" ) ) ;
386
458
}
387
459
460
+ public override bool CanWriteResult ( OutputFormatterContext context , MediaTypeHeaderValue contentType )
461
+ {
462
+ return false ;
463
+ }
464
+
388
465
public override Task WriteAsync ( OutputFormatterContext context , CancellationToken cancellationToken )
389
466
{
390
467
throw new NotImplementedException ( ) ;
0 commit comments