367 lines
13 KiB
C#
367 lines
13 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.Http;
|
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNet.Mvc.IntegrationTests
|
|
{
|
|
// Integration tests targeting the behavior of the DictionaryModelBinder with other model binders.
|
|
public class DictionaryModelBinderIntegrationTest
|
|
{
|
|
[Fact]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithPrefixAndKVP_Success()
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, int>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString("?parameter[0].Key=key0¶meter[0].Value=10");
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, int>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, int>() { { "key0", 10 } }, model);
|
|
|
|
Assert.Equal(2, modelState.Count);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
|
|
var entry = Assert.Single(modelState, kvp => kvp.Key == "parameter[0].Key").Value;
|
|
Assert.Equal("key0", entry.AttemptedValue);
|
|
Assert.Equal("key0", entry.RawValue);
|
|
|
|
entry = Assert.Single(modelState, kvp => kvp.Key == "parameter[0].Value").Value;
|
|
Assert.Equal("10", entry.AttemptedValue);
|
|
Assert.Equal("10", entry.RawValue);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithPrefixAndItem_Success()
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, int>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString("?parameter[key0]=10");
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, int>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, int>() { { "key0", 10 } }, model);
|
|
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
|
|
var kvp = Assert.Single(modelState);
|
|
Assert.Equal("parameter[key0]", kvp.Key);
|
|
var entry = kvp.Value;
|
|
Assert.Equal("10", entry.AttemptedValue);
|
|
Assert.Equal("10", entry.RawValue);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("?prefix[key0]=10")]
|
|
[InlineData("?prefix[0].Key=key0&prefix[0].Value=10")]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithExplicitPrefix_Success(
|
|
string queryString)
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
BindingInfo = new BindingInfo()
|
|
{
|
|
BinderModelName = "prefix",
|
|
},
|
|
ParameterType = typeof(Dictionary<string, int>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString(queryString);
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, int>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, int>() { { "key0", 10 }, }, model);
|
|
|
|
Assert.NotEmpty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("?[key0]=10")]
|
|
[InlineData("?[0].Key=key0&[0].Value=10")]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_EmptyPrefix_Success(string queryString)
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, int>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString(queryString);
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, int>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, int>() { { "key0", 10 }, }, model);
|
|
|
|
Assert.NotEmpty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_NoData()
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, int>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString("?");
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, int>>(modelBindingResult.Model);
|
|
Assert.Empty(model);
|
|
|
|
Assert.Empty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
|
|
private class Person
|
|
{
|
|
public int Id { get; set; }
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
var other = obj as Person;
|
|
|
|
return other != null && Id == other.Id;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return Id.GetHashCode();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{{ { Id } }}";
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("?parameter[key0].Id=10")]
|
|
[InlineData("?parameter[0].Key=key0¶meter[0].Value.Id=10")]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_WithPrefix_Success(string queryString)
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, Person>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString(queryString);
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, Person>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, Person> { { "key0", new Person { Id = 10 } }, }, model);
|
|
|
|
Assert.NotEmpty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("?prefix[key0].Id=10")]
|
|
[InlineData("?prefix[0].Key=key0&prefix[0].Value.Id=10")]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_WithExplicitPrefix_Success(
|
|
string queryString)
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
BindingInfo = new BindingInfo()
|
|
{
|
|
BinderModelName = "prefix",
|
|
},
|
|
ParameterType = typeof(Dictionary<string, Person>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString(queryString);
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, Person>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, Person> { { "key0", new Person { Id = 10 } }, }, model);
|
|
|
|
Assert.NotEmpty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("?[key0].Id=10")]
|
|
[InlineData("?[0].Key=key0&[0].Value.Id=10")]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_EmptyPrefix_Success(string queryString)
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, Person>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString(queryString);
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, Person>>(modelBindingResult.Model);
|
|
Assert.Equal(new Dictionary<string, Person> { { "key0", new Person { Id = 10 } }, }, model);
|
|
|
|
Assert.NotEmpty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_NoData()
|
|
{
|
|
// Arrange
|
|
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
|
|
var parameter = new ParameterDescriptor()
|
|
{
|
|
Name = "parameter",
|
|
ParameterType = typeof(Dictionary<string, Person>)
|
|
};
|
|
|
|
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
|
|
{
|
|
request.QueryString = new QueryString("?");
|
|
});
|
|
|
|
var modelState = new ModelStateDictionary();
|
|
|
|
// Act
|
|
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
|
|
|
|
// Assert
|
|
Assert.NotNull(modelBindingResult);
|
|
Assert.True(modelBindingResult.IsModelSet);
|
|
|
|
var model = Assert.IsType<Dictionary<string, Person>>(modelBindingResult.Model);
|
|
Assert.Empty(model);
|
|
|
|
Assert.Empty(modelState);
|
|
Assert.Equal(0, modelState.ErrorCount);
|
|
Assert.True(modelState.IsValid);
|
|
}
|
|
}
|
|
} |