File tree 2 files changed +34
-0
lines changed
Mvc.Core/src/ModelBinding/Binders 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -143,6 +143,11 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)
143
143
144
144
if ( formatter == null )
145
145
{
146
+ if ( AllowEmptyBody && httpContext . Request . ContentLength == 0 )
147
+ {
148
+ return ;
149
+ }
150
+
146
151
_logger . NoInputFormatterSelected ( formatterContext ) ;
147
152
148
153
var message = Resources . FormatUnsupportedContentType ( httpContext . Request . ContentType ) ;
Original file line number Diff line number Diff line change @@ -188,6 +188,20 @@ public async Task BodyIsRequiredByDefault()
188
188
} ) ;
189
189
}
190
190
191
+ [ Fact ]
192
+ public async Task BodyIsRequiredByDefaultFailsWithEmptyBody ( )
193
+ {
194
+ var content = new ByteArrayContent ( Array . Empty < byte > ( ) ) ;
195
+ Assert . Null ( content . Headers . ContentType ) ;
196
+ Assert . Equal ( 0 , content . Headers . ContentLength ) ;
197
+
198
+ // Act
199
+ var response = await Client . PostAsync ( $ "Home/{ nameof ( HomeController . DefaultBody ) } ", content ) ;
200
+
201
+ // Assert
202
+ await response . AssertStatusCodeAsync ( HttpStatusCode . UnsupportedMediaType ) ;
203
+ }
204
+
191
205
[ Fact ]
192
206
public async Task OptionalFromBodyWorks ( )
193
207
{
@@ -197,5 +211,20 @@ public async Task OptionalFromBodyWorks()
197
211
// Assert
198
212
await response . AssertStatusCodeAsync ( HttpStatusCode . OK ) ;
199
213
}
214
+
215
+ [ Fact ]
216
+ public async Task OptionalFromBodyWorksWithEmptyRequest ( )
217
+ {
218
+ // Arrange
219
+ var content = new ByteArrayContent ( Array . Empty < byte > ( ) ) ;
220
+ Assert . Null ( content . Headers . ContentType ) ;
221
+ Assert . Equal ( 0 , content . Headers . ContentLength ) ;
222
+
223
+ // Act
224
+ var response = await Client . PostAsync ( $ "Home/{ nameof ( HomeController . OptionalBody ) } ", content ) ;
225
+
226
+ // Assert
227
+ await response . AssertStatusCodeAsync ( HttpStatusCode . OK ) ;
228
+ }
200
229
}
201
230
}
You can’t perform that action at this time.
0 commit comments