ConsumesAttribute accepts requests without content type

Fixes #8174
This commit is contained in:
Pranav K 2018-08-02 15:36:27 -07:00
parent 0989231ed5
commit ce8fc29728
4 changed files with 6 additions and 29 deletions

View File

@ -10,8 +10,6 @@ using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.Net.Http.Headers;
using Resources = Microsoft.AspNetCore.Mvc.Core.Resources;
@ -79,7 +77,7 @@ namespace Microsoft.AspNetCore.Mvc
// Confirm the request's content type is more specific than a media type this action supports e.g. OK
// if client sent "text/plain" data and this action supports "text/*".
if (requestContentType != null && !IsSubsetOfAnyContentType(requestContentType))
if (requestContentType == null || !IsSubsetOfAnyContentType(requestContentType))
{
context.Result = new UnsupportedMediaTypeResult();
}

View File

@ -326,7 +326,7 @@ namespace Microsoft.AspNetCore.Mvc
[Theory]
[InlineData("")]
[InlineData(null)]
public void OnResourceExecuting_NullOrEmptyRequestContentType_IsNoOp(string contentType)
public void OnResourceExecuting_NullOrEmptyRequestContentType_SetsUnsupportedMediaTypeResult(string contentType)
{
// Arrange
var httpContext = new DefaultHttpContext();
@ -349,7 +349,8 @@ namespace Microsoft.AspNetCore.Mvc
consumesFilter.OnResourceExecuting(resourceExecutingContext);
// Assert
Assert.Null(resourceExecutingContext.Result);
Assert.NotNull(resourceExecutingContext.Result);
Assert.IsType<UnsupportedMediaTypeResult>(resourceExecutingContext.Result);
}
[Theory]

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xunit;
@ -30,22 +29,5 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.True(result);
}
// The endpoint routing version of this feature has fixed https://github.com/aspnet/Mvc/issues/8174
[Fact]
public override async Task NoRequestContentType_Selects_IfASingleActionWithConstraintIsPresent()
{
// Arrange
var request = new HttpRequestMessage(
HttpMethod.Post,
"http://localhost/ConsumesAttribute_PassThrough/CreateProduct");
// Act
var response = await Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.UnsupportedMediaType, response.StatusCode);
}
}
}

View File

@ -49,7 +49,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
}
[Fact]
public virtual async Task NoRequestContentType_Selects_IfASingleActionWithConstraintIsPresent()
public async Task NoRequestContentType_Selects_IfASingleActionWithConstraintIsPresent_ReturnsUnsupported()
{
// Arrange
var request = new HttpRequestMessage(
@ -58,11 +58,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Act
var response = await Client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("ConsumesAttribute_PassThrough_Product_Json", body);
await response.AssertStatusCodeAsync(HttpStatusCode.UnsupportedMediaType);
}
[Theory]