Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

124 lines
3.6 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using Microsoft.Web.UnitTestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Test
{
public class ByteArrayModelBinderTest
{
internal const string Base64TestString = "Fys1";
internal static readonly byte[] Base64TestBytes = new byte[] { 23, 43, 53 };
[Fact]
public void BindModelWithNonExistentValueReturnsNull()
{
// Arrange
SimpleValueProvider valueProvider = new SimpleValueProvider()
{
{ "foo", null }
};
ModelBindingContext bindingContext = new ModelBindingContext()
{
ModelName = "foo",
ValueProvider = valueProvider
};
ByteArrayModelBinder binder = new ByteArrayModelBinder();
// Act
object binderResult = binder.BindModel(null, bindingContext);
// Assert
Assert.Null(binderResult);
}
[Fact]
public void BinderWithEmptyStringValueReturnsNull()
{
// Arrange
SimpleValueProvider valueProvider = new SimpleValueProvider()
{
{ "foo", "" }
};
ModelBindingContext bindingContext = new ModelBindingContext()
{
ModelName = "foo",
ValueProvider = valueProvider
};
ByteArrayModelBinder binder = new ByteArrayModelBinder();
// Act
object binderResult = binder.BindModel(null, bindingContext);
// Assert
Assert.Null(binderResult);
}
[Fact]
public void BindModelThrowsIfBindingContextIsNull()
{
// Arrange
ByteArrayModelBinder binder = new ByteArrayModelBinder();
// Act & assert
Assert.ThrowsArgumentNull(
delegate { binder.BindModel(null, null); }, "bindingContext");
}
[Fact]
public void BindModelWithBase64QuotedValueReturnsByteArray()
{
// Arrange
string base64Value = Base64TestString;
SimpleValueProvider valueProvider = new SimpleValueProvider()
{
{ "foo", "\"" + base64Value + "\"" }
};
ModelBindingContext bindingContext = new ModelBindingContext()
{
ModelName = "foo",
ValueProvider = valueProvider
};
ByteArrayModelBinder binder = new ByteArrayModelBinder();
// Act
byte[] boundValue = binder.BindModel(null, bindingContext) as byte[];
// Assert
Assert.Equal(Base64TestBytes, boundValue);
}
[Fact]
public void BindModelWithBase64UnquotedValueReturnsByteArray()
{
// Arrange
string base64Value = Base64TestString;
SimpleValueProvider valueProvider = new SimpleValueProvider()
{
{ "foo", base64Value }
};
ModelBindingContext bindingContext = new ModelBindingContext()
{
ModelName = "foo",
ValueProvider = valueProvider
};
ByteArrayModelBinder binder = new ByteArrayModelBinder();
// Act
byte[] boundValue = binder.BindModel(null, bindingContext) as byte[];
// Assert
Assert.Equal(Base64TestBytes, boundValue);
}
}
}