@@ -24,7 +24,7 @@ public class ContentResultTest
24
24
MemoryPoolHttpResponseStreamWriterFactory . DefaultBufferSize ;
25
25
26
26
[ Fact ]
27
- public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding ( )
27
+ public async Task ContentResult_ExecuteResultAsync_Response_NullContent_SetsContentTypeAndEncoding ( )
28
28
{
29
29
// Arrange
30
30
var contentResult = new ContentResult
@@ -45,6 +45,28 @@ public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding(
45
45
MediaTypeAssert . Equal ( "text/plain; charset=utf-16" , httpContext . Response . ContentType ) ;
46
46
}
47
47
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
+
48
70
public static TheoryData < MediaTypeHeaderValue , string , string , string , byte [ ] > ContentResultContentTypeData
49
71
{
50
72
get
@@ -143,6 +165,36 @@ public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnR
143
165
Assert . Equal ( expectedContentData . Length , httpContext . Response . ContentLength ) ;
144
166
}
145
167
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
+
146
198
public static TheoryData < string , string > ContentResult_WritesDataCorrectly_ForDifferentContentSizesData
147
199
{
148
200
get
@@ -246,6 +298,31 @@ public async Task ContentResult_WritesDataCorrectly_ForDifferentContentSizes(str
246
298
Assert . Equal ( content , actualContent ) ;
247
299
}
248
300
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
+
249
326
private static ActionContext GetActionContext ( HttpContext httpContext )
250
327
{
251
328
var routeData = new RouteData ( ) ;
@@ -270,6 +347,7 @@ private static IServiceCollection CreateServices()
270
347
services . AddSingleton < IActionResultExecutor < ContentResult > > ( new ContentResultExecutor (
271
348
new Logger < ContentResultExecutor > ( NullLoggerFactory . Instance ) ,
272
349
new MemoryPoolHttpResponseStreamWriterFactory ( ArrayPool < byte > . Shared , charArrayPool . Object ) ) ) ;
350
+ services . AddSingleton < ILoggerFactory > ( NullLoggerFactory . Instance ) ;
273
351
return services ;
274
352
}
275
353
0 commit comments