12
12
13
13
namespace Microsoft . AspNetCore . Mvc . Formatters
14
14
{
15
- public class TextPlainFormatterTests
15
+ public class StringOutputFormatterTests
16
16
{
17
- public static IEnumerable < object [ ] > OutputFormatterContextValues
17
+ public static IEnumerable < object [ ] > CanWriteResultForStringTypesData
18
18
{
19
19
get
20
20
{
21
- // object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult
22
- yield return new object [ ] { "valid value" , true , true } ;
23
- yield return new object [ ] { null , true , true } ;
24
- yield return new object [ ] { null , false , false } ;
25
- yield return new object [ ] { new object ( ) , false , false } ;
21
+ // object value, bool useDeclaredTypeAsString
22
+ yield return new object [ ] { "declared and runtime type are same" , true } ;
23
+ yield return new object [ ] { "declared and runtime type are different" , false } ;
24
+ yield return new object [ ] { null , true } ;
25
+ }
26
+ }
27
+
28
+ public static IEnumerable < object [ ] > CannotWriteResultForNonStringTypesData
29
+ {
30
+ get
31
+ {
32
+ // object value, bool useDeclaredTypeAsString
33
+ yield return new object [ ] { null , false } ;
34
+ yield return new object [ ] { new object ( ) , false } ;
26
35
}
27
36
}
28
37
29
38
[ Fact ]
30
- public void CanWriteResult_SetsAcceptContentType ( )
39
+ public void CannotWriteResult_ForNonTextPlainOrNonBrowserMediaTypes ( )
31
40
{
32
41
// Arrange
33
42
var formatter = new StringOutputFormatter ( ) ;
@@ -44,7 +53,7 @@ public void CanWriteResult_SetsAcceptContentType()
44
53
var result = formatter . CanWriteResult ( context ) ;
45
54
46
55
// Assert
47
- Assert . True ( result ) ;
56
+ Assert . False ( result ) ;
48
57
Assert . Equal ( expectedContentType , context . ContentType ) ;
49
58
}
50
59
@@ -53,13 +62,17 @@ public void CanWriteResult_DefaultContentType()
53
62
{
54
63
// Arrange
55
64
var formatter = new StringOutputFormatter ( ) ;
56
-
57
65
var context = new OutputFormatterWriteContext (
58
66
new DefaultHttpContext ( ) ,
59
67
new TestHttpResponseStreamWriterFactory ( ) . CreateWriter ,
60
68
typeof ( string ) ,
61
69
"Thisisastring" ) ;
62
70
71
+ // For example, this can happen when a request is received without any Accept header OR a request
72
+ // is from a browser (in which case this ContentType is set to null by the infrastructure when
73
+ // RespectBrowserAcceptHeader is set to false)
74
+ context . ContentType = new StringSegment ( ) ;
75
+
63
76
// Act
64
77
var result = formatter . CanWriteResult ( context ) ;
65
78
@@ -69,14 +82,13 @@ public void CanWriteResult_DefaultContentType()
69
82
}
70
83
71
84
[ Theory ]
72
- [ MemberData ( nameof ( OutputFormatterContextValues ) ) ]
73
- public void CanWriteResult_ReturnsTrueForStringTypes (
85
+ [ MemberData ( nameof ( CanWriteResultForStringTypesData ) ) ]
86
+ public void CanWriteResult_ForStringTypes (
74
87
object value ,
75
- bool useDeclaredTypeAsString ,
76
- bool expectedCanWriteResult )
88
+ bool useDeclaredTypeAsString )
77
89
{
78
90
// Arrange
79
- var expectedContentType = new StringSegment ( "application/json " ) ;
91
+ var expectedContentType = new StringSegment ( "text/plain; charset=utf-8 " ) ;
80
92
81
93
var formatter = new StringOutputFormatter ( ) ;
82
94
var type = useDeclaredTypeAsString ? typeof ( string ) : typeof ( object ) ;
@@ -86,16 +98,42 @@ public void CanWriteResult_ReturnsTrueForStringTypes(
86
98
new TestHttpResponseStreamWriterFactory ( ) . CreateWriter ,
87
99
type ,
88
100
value ) ;
89
- context . ContentType = expectedContentType ;
101
+ context . ContentType = new StringSegment ( "text/plain" ) ;
90
102
91
103
// Act
92
104
var result = formatter . CanWriteResult ( context ) ;
93
105
94
106
// Assert
95
- Assert . Equal ( expectedCanWriteResult , result ) ;
107
+ Assert . True ( result ) ;
96
108
Assert . Equal ( expectedContentType , context . ContentType ) ;
97
109
}
98
110
111
+ [ Theory ]
112
+ [ MemberData ( nameof ( CannotWriteResultForNonStringTypesData ) ) ]
113
+ public void CannotWriteResult_ForNonStringTypes (
114
+ object value ,
115
+ bool useDeclaredTypeAsString )
116
+ {
117
+ // Arrange
118
+ var expectedContentType = new StringSegment ( "text/plain; charset=utf-8" ) ;
119
+
120
+ var formatter = new StringOutputFormatter ( ) ;
121
+ var type = useDeclaredTypeAsString ? typeof ( string ) : typeof ( object ) ;
122
+
123
+ var context = new OutputFormatterWriteContext (
124
+ new DefaultHttpContext ( ) ,
125
+ new TestHttpResponseStreamWriterFactory ( ) . CreateWriter ,
126
+ type ,
127
+ value ) ;
128
+ context . ContentType = new StringSegment ( "text/plain" ) ;
129
+
130
+ // Act
131
+ var result = formatter . CanWriteResult ( context ) ;
132
+
133
+ // Assert
134
+ Assert . False ( result ) ;
135
+ }
136
+
99
137
[ Fact ]
100
138
public async Task WriteAsync_DoesNotWriteNullStrings ( )
101
139
{
0 commit comments