Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
ConsumesAttribute accepts requests without content type
Browse files Browse the repository at this point in the history
Fixes #8174
  • Loading branch information
pranavkm committed Aug 2, 2018
1 parent 0989231 commit ce8fc29
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/Microsoft.AspNetCore.Mvc.Core/ConsumesAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
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;

Expand Down Expand Up @@ -79,7 +77,7 @@ public void OnResourceExecuting(ResourceExecutingContext context)

// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public void OnResourceExecuting_ForNoContentTypeMatch_SetsUnsupportedMediaTypeRe
[Theory]
[InlineData("")]
[InlineData(null)]
public void OnResourceExecuting_NullOrEmptyRequestContentType_IsNoOp(string contentType)
public void OnResourceExecuting_NullOrEmptyRequestContentType_SetsUnsupportedMediaTypeResult(string contentType)
{
// Arrange
var httpContext = new DefaultHttpContext();
Expand All @@ -349,7 +349,8 @@ public void OnResourceExecuting_NullOrEmptyRequestContentType_IsNoOp(string cont
consumesFilter.OnResourceExecuting(resourceExecutingContext);

// Assert
Assert.Null(resourceExecutingContext.Result);
Assert.NotNull(resourceExecutingContext.Result);
Assert.IsType<UnsupportedMediaTypeResult>(resourceExecutingContext.Result);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,22 +29,5 @@ public async override Task HasEndpointMatch()

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task NoRequestContentType_SelectsActionWithoutConstraint()
}

[Fact]
public virtual async Task NoRequestContentType_Selects_IfASingleActionWithConstraintIsPresent()
public async Task NoRequestContentType_Selects_IfASingleActionWithConstraintIsPresent_ReturnsUnsupported()
{
// Arrange
var request = new HttpRequestMessage(
Expand All @@ -58,11 +58,7 @@ public virtual async Task NoRequestContentType_Selects_IfASingleActionWithConstr

// 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]
Expand Down

0 comments on commit ce8fc29

Please sign in to comment.