@@ -24,7 +24,7 @@ public class ContentResultTest
2424 MemoryPoolHttpResponseStreamWriterFactory . DefaultBufferSize ;
2525
2626 [ Fact ]
27- public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding ( )
27+ public async Task ContentResult_ExecuteResultAsync_Response_NullContent_SetsContentTypeAndEncoding ( )
2828 {
2929 // Arrange
3030 var contentResult = new ContentResult
@@ -45,6 +45,28 @@ public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding(
4545 MediaTypeAssert . Equal ( "text/plain; charset=utf-16" , httpContext . Response . ContentType ) ;
4646 }
4747
48+ [ Fact ]
49+ public async Task ContentResult_ExecuteAsync_Response_NullContent_SetsContentTypeAndEncoding ( )
50+ {
51+ // Arrange
52+ var contentResult = new ContentResult
53+ {
54+ Content = null ,
55+ ContentType = new MediaTypeHeaderValue ( "text/plain" )
56+ {
57+ Encoding = Encoding . Unicode
58+ } . ToString ( )
59+ } ;
60+ var httpContext = GetHttpContext ( ) ;
61+ var actionContext = GetActionContext ( httpContext ) ;
62+
63+ // Act
64+ await ( ( IResult ) contentResult ) . ExecuteAsync ( httpContext ) ;
65+
66+ // Assert
67+ MediaTypeAssert . Equal ( "text/plain; charset=utf-16" , httpContext . Response . ContentType ) ;
68+ }
69+
4870 public static TheoryData < MediaTypeHeaderValue , string , string , string , byte [ ] > ContentResultContentTypeData
4971 {
5072 get
@@ -143,6 +165,36 @@ public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnR
143165 Assert . Equal ( expectedContentData . Length , httpContext . Response . ContentLength ) ;
144166 }
145167
168+ [ Theory ]
169+ [ MemberData ( nameof ( ContentResultContentTypeData ) ) ]
170+ public async Task ContentResult_ExecuteAsync_SetContentTypeAndEncoding_OnResponse (
171+ MediaTypeHeaderValue contentType ,
172+ string content ,
173+ string responseContentType ,
174+ string expectedContentType ,
175+ byte [ ] expectedContentData )
176+ {
177+ // Arrange
178+ var contentResult = new ContentResult
179+ {
180+ Content = content ,
181+ ContentType = contentType ? . ToString ( )
182+ } ;
183+ var httpContext = GetHttpContext ( ) ;
184+ var memoryStream = new MemoryStream ( ) ;
185+ httpContext . Response . Body = memoryStream ;
186+ httpContext . Response . ContentType = responseContentType ;
187+
188+ // Act
189+ await ( ( IResult ) contentResult ) . ExecuteAsync ( httpContext ) ;
190+
191+ // Assert
192+ var finalResponseContentType = httpContext . Response . ContentType ;
193+ Assert . Equal ( expectedContentType , finalResponseContentType ) ;
194+ Assert . Equal ( expectedContentData , memoryStream . ToArray ( ) ) ;
195+ Assert . Equal ( expectedContentData . Length , httpContext . Response . ContentLength ) ;
196+ }
197+
146198 public static TheoryData < string , string > ContentResult_WritesDataCorrectly_ForDifferentContentSizesData
147199 {
148200 get
@@ -246,6 +298,31 @@ public async Task ContentResult_WritesDataCorrectly_ForDifferentContentSizes(str
246298 Assert . Equal ( content , actualContent ) ;
247299 }
248300
301+ [ Theory ]
302+ [ MemberData ( nameof ( ContentResult_WritesDataCorrectly_ForDifferentContentSizesData ) ) ]
303+ public async Task ContentResult_ExecuteAsync_WritesDataCorrectly_ForDifferentContentSizes ( string content , string contentType )
304+ {
305+ // Arrange
306+ var contentResult = new ContentResult
307+ {
308+ Content = content ,
309+ ContentType = contentType
310+ } ;
311+ var httpContext = GetHttpContext ( ) ;
312+ var memoryStream = new MemoryStream ( ) ;
313+ httpContext . Response . Body = memoryStream ;
314+ var encoding = MediaTypeHeaderValue . Parse ( contentType ) . Encoding ;
315+
316+ // Act
317+ await ( ( IResult ) contentResult ) . ExecuteAsync ( httpContext ) ;
318+
319+ // Assert
320+ memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
321+ var streamReader = new StreamReader ( memoryStream , encoding ) ;
322+ var actualContent = await streamReader . ReadToEndAsync ( ) ;
323+ Assert . Equal ( content , actualContent ) ;
324+ }
325+
249326 private static ActionContext GetActionContext ( HttpContext httpContext )
250327 {
251328 var routeData = new RouteData ( ) ;
@@ -270,6 +347,7 @@ private static IServiceCollection CreateServices()
270347 services . AddSingleton < IActionResultExecutor < ContentResult > > ( new ContentResultExecutor (
271348 new Logger < ContentResultExecutor > ( NullLoggerFactory . Instance ) ,
272349 new MemoryPoolHttpResponseStreamWriterFactory ( ArrayPool < byte > . Shared , charArrayPool . Object ) ) ) ;
350+ services . AddSingleton < ILoggerFactory > ( NullLoggerFactory . Instance ) ;
273351 return services ;
274352 }
275353
0 commit comments