Added failing test for #7546

This commit is contained in:
Kristian Hellang 2018-03-26 14:44:52 +02:00 committed by Pranav K
parent 7d1576a65d
commit a16504b941
2 changed files with 36 additions and 1 deletions

View File

@ -139,7 +139,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal
actionModel.Filters.Add(_modelStateInvalidFilter);
}
private void InferParameterBindingSources(ActionModel actionModel)
// Internal for unit testing
internal void InferParameterBindingSources(ActionModel actionModel)
{
if (_modelMetadataProvider == null || _apiBehaviorOptions.SuppressInferBindingSourcesForParameters)
{

View File

@ -5,6 +5,7 @@ using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Infrastructure;
@ -383,6 +384,31 @@ namespace Microsoft.AspNetCore.Mvc.Internal
Assert.Same(BindingSource.Body, result);
}
[Fact]
public void InferParameterBindingSources_SetsCorrectBindingSourceForComplexTypesWithCancellationToken()
{
// Arrange
var actionName = nameof(ParameterBindingController.ComplexTypeModelWithCancellationToken);
var actionModel = GetActionModel(typeof(ParameterBindingController), actionName);
// Go through MvcOptions and MvcCoreMvcOptionsSetup to make
// sure we pick up the proper ModelMetadataDetailsProviders from the options.
var options = new MvcOptions();
new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory()).Configure(options);
var metadataProvider = TestModelMetadataProvider.CreateProvider(options.ModelMetadataDetailsProviders);
var provider = GetProvider(modelMetadataProvider: metadataProvider);
// Act
provider.InferParameterBindingSources(actionModel);
// Assert
var model = GetParameterModel<TestModel>(actionModel);
Assert.Same(BindingSource.Body, model.BindingInfo.BindingSource);
var cancellationToken = GetParameterModel<CancellationToken>(actionModel);
Assert.Same(BindingSource.Special, cancellationToken.BindingInfo.BindingSource);
}
[Fact]
public void InferBindingSourceForParameter_ReturnsBodyForSimpleTypes()
{
@ -535,6 +561,11 @@ namespace Microsoft.AspNetCore.Mvc.Internal
return Assert.Single(action.Parameters);
}
private static ParameterModel GetParameterModel<T>(ActionModel action)
{
return Assert.Single(action.Parameters.Where(x => typeof(T).IsAssignableFrom(x.ParameterType)));
}
[ApiController]
[Route("TestApi")]
private class TestApiController : Controller
@ -612,6 +643,9 @@ namespace Microsoft.AspNetCore.Mvc.Internal
[HttpPost]
[Consumes("application/json")]
public IActionResult ActionWithConsumesAttribute([FromForm] string parameter) => null;
[HttpPut("cancellation")]
public IActionResult ComplexTypeModelWithCancellationToken(TestModel model, CancellationToken cancellationToken) => null;
}
[ApiController]