From f1c4a8e4974c44456929a1179c0b26af6542abfa Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 20 Mar 2014 21:01:07 -0700 Subject: [PATCH] Change FormValueProviderFactory to use GetContentType for media type matching Fixes #68 --- .../Formatters/TempInputFormatterProvider.cs | 1 - .../Internal/ContentTypeHeaderValue.cs | 12 +++--------- .../Internal/HttpRequestExtensions.cs | 4 ++-- .../ValueProviders/FormValueProviderFactory.cs | 6 +++--- .../ValueProviders/FormValueProviderFactoryTests.cs | 8 +++++--- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Formatters/TempInputFormatterProvider.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Formatters/TempInputFormatterProvider.cs index 409b9845e9..83837f830a 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Formatters/TempInputFormatterProvider.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Formatters/TempInputFormatterProvider.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; -using Microsoft.AspNet.Mvc.ModelBinding.Internal; namespace Microsoft.AspNet.Mvc.ModelBinding { diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/ContentTypeHeaderValue.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/ContentTypeHeaderValue.cs index 7fcc051bb9..c1326bc1fc 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/ContentTypeHeaderValue.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/ContentTypeHeaderValue.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Microsoft.AspNet.Mvc.ModelBinding.Internal + +namespace Microsoft.AspNet.Mvc.ModelBinding { - public class ContentTypeHeaderValue + internal class ContentTypeHeaderValue { public ContentTypeHeaderValue([NotNull] string contentType, string charSet) @@ -18,6 +13,5 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Internal public string ContentType { get; private set; } public string CharSet { get; set; } - } } diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/HttpRequestExtensions.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/HttpRequestExtensions.cs index 80672e36e9..2d0004e7dd 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/HttpRequestExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/Internal/HttpRequestExtensions.cs @@ -1,9 +1,9 @@ using System; using Microsoft.AspNet.Abstractions; -namespace Microsoft.AspNet.Mvc.ModelBinding.Internal +namespace Microsoft.AspNet.Mvc.ModelBinding { - public static class HttpRequestExtensions + internal static class HttpRequestExtensions { private const string ContentTypeHeader = "Content-Type"; private const string CharSetToken = "charset="; diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/FormValueProviderFactory.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/FormValueProviderFactory.cs index c16765e150..c081a67d9a 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/FormValueProviderFactory.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/ValueProviders/FormValueProviderFactory.cs @@ -25,9 +25,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding private bool IsSupportedContentType(HttpRequest request) { - var contentType = request.Headers["Content-Type"]; - return !String.IsNullOrEmpty(contentType) && - contentType.Equals(FormEncodedContentType, StringComparison.OrdinalIgnoreCase); + var contentType = request.GetContentType(); + return contentType != null && + contentType.ContentType.Equals(FormEncodedContentType, StringComparison.OrdinalIgnoreCase); } private static CultureInfo GetCultureInfo(HttpRequest request) diff --git a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/ValueProviders/FormValueProviderFactoryTests.cs b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/ValueProviders/FormValueProviderFactoryTests.cs index ba335f8616..f2c2712aac 100644 --- a/test/Microsoft.AspNet.Mvc.ModelBinding.Test/ValueProviders/FormValueProviderFactoryTests.cs +++ b/test/Microsoft.AspNet.Mvc.ModelBinding.Test/ValueProviders/FormValueProviderFactoryTests.cs @@ -24,11 +24,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test Assert.Null(result); } - [Fact] - public async Task GetValueProvider_ReturnsValueProviderInstaceWithInvariantCulture() + [Theory] + [InlineData("application/x-www-form-urlencoded")] + [InlineData("application/x-www-form-urlencoded;charset=utf-8")] + public async Task GetValueProvider_ReturnsValueProviderInstaceWithInvariantCulture(string contentType) { // Arrange - var requestContext = CreateRequestContext("application/x-www-form-urlencoded"); + var requestContext = CreateRequestContext(contentType); var factory = new FormValueProviderFactory(); // Act