Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FormUrlEncodedMediaTypeFormatter to .NetStandard version of System.Net.Http.Formatting #82

Merged
merged 1 commit into from
Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedJson.cs">
<Link>Formatting\FormUrlEncodedJson.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedMediaTypeFormatter.cs">
<Link>Formatting\FormUrlEncodedMediaTypeFormatter.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\Formatting\IFormatterLogger.cs">
<Link>Formatting\IFormatterLogger.cs</Link>
</Compile>
Expand Down Expand Up @@ -117,6 +120,9 @@
<Compile Include="..\System.Net.Http.Formatting\Formatting\XmlMediaTypeFormatter.cs">
<Link>Formatting\XmlMediaTypeFormatter.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\HttpContentFormDataExtensions.cs">
<Link>HttpContentFormDataExtensions.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\Internal\HttpValueCollection.cs">
<Link>HttpValueCollection.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedJson.cs">
<Link>Formatting\FormUrlEncodedJson.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedMediaTypeFormatter.cs">
<Link>Formatting\FormUrlEncodedMediaTypeFormatter.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\Formatting\IFormatterLogger.cs">
<Link>Formatting\IFormatterLogger.cs</Link>
</Compile>
Expand Down Expand Up @@ -116,6 +119,9 @@
<Compile Include="..\System.Net.Http.Formatting\Formatting\XmlMediaTypeFormatter.cs">
<Link>Formatting\XmlMediaTypeFormatter.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\HttpContentFormDataExtensions.cs">
<Link>HttpContentFormDataExtensions.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting\Internal\HttpValueCollection.cs">
<Link>Internal\HttpValueCollection.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ public JsonMediaTypeFormatter JsonFormatter
get { return Items.OfType<JsonMediaTypeFormatter>().FirstOrDefault(); }
}

#if !NETFX_CORE // FormUrlEncodedMediaTypeFormatter is not supported in portable library.
/// <summary>
/// Gets the <see cref="MediaTypeFormatter"/> to use for <c>application/x-www-form-urlencoded</c> data.
/// </summary>
public FormUrlEncodedMediaTypeFormatter FormUrlEncodedFormatter
{
get { return Items.OfType<FormUrlEncodedMediaTypeFormatter>().FirstOrDefault(); }
}
#endif

internal MediaTypeFormatter[] WritingFormatters
{
Expand Down Expand Up @@ -202,8 +200,8 @@ public static bool IsTypeExcludedFromValidation(Type type)
return
#if !NETFX_CORE
typeof(XmlNode).IsAssignableFrom(type) ||
typeof(FormDataCollection).IsAssignableFrom(type) ||
#endif
typeof(FormDataCollection).IsAssignableFrom(type) ||
FormattingUtilities.IsJTokenType(type) ||
typeof(XObject).IsAssignableFrom(type) ||
typeof(Type).IsAssignableFrom(type) ||
Expand Down Expand Up @@ -260,9 +258,7 @@ private static IEnumerable<MediaTypeFormatter> CreateDefaultFormatters()
{
new JsonMediaTypeFormatter(),
new XmlMediaTypeFormatter(),
#if !NETFX_CORE
new FormUrlEncodedMediaTypeFormatter()
#endif
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
#if NETFX_CORE
using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
#endif

namespace System.Net.Http
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,27 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="..\System.Net.Http.Formatting.Test\DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs">
<Link>DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\BsonMediaTypeFormatterTests.cs">
<Link>Formatting\BsonMediaTypeFormatterTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormDataCollectionTests.cs">
<Link>Internal\FormDataCollectionTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromContentTests.cs">
<Link>Formatting\FormUrlEncodedFromContentTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromUriQueryTests.cs">
<Link>Formatting\FormUrlEncodedFromUriQueryTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedJsonTests.cs">
<Link>Internal\FormUrlEncodedJsonTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedMediaTypeFormatterTests.cs">
<Link>Formatting\FormUrlEncodedMediaTypeFormatterTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\JsonNetSerializationTest.cs">
<Link>Formatting\JsonNetSerializationTest.cs</Link>
</Compile>
Expand All @@ -78,6 +90,9 @@
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\Parsers\FormUrlEncodedParserTests.cs">
<Link>Internal\FormUrlEncodedParserTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\HttpContentFormDataExtensionsTest.cs">
<Link>HttpContentFormDataExtensionsTest.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Internal\HttpValueCollectionTest.cs">
<Link>HttpValueCollectionTest.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,27 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="..\System.Net.Http.Formatting.Test\DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs">
<Link>DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\BsonMediaTypeFormatterTests.cs">
<Link>Formatting\BsonMediaTypeFormatterTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormDataCollectionTests.cs">
<Link>Internal\FormDataCollectionTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromContentTests.cs">
<Link>Formatting\FormUrlEncodedFromContentTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromUriQueryTests.cs">
<Link>Formatting\FormUrlEncodedFromUriQueryTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedJsonTests.cs">
<Link>Internal\FormUrlEncodedJsonTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedMediaTypeFormatterTests.cs">
<Link>Formatting\FormUrlEncodedMediaTypeFormatterTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\JsonNetSerializationTest.cs">
<Link>Formatting\JsonNetSerializationTest.cs</Link>
</Compile>
Expand All @@ -71,6 +83,9 @@
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\Parsers\FormUrlEncodedParserTests.cs">
<Link>Internal\FormUrlEncodedParserTests.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\HttpContentFormDataExtensionsTest.cs">
<Link>HttpContentFormDataExtensionsTest.cs</Link>
</Compile>
<Compile Include="..\System.Net.Http.Formatting.Test\Internal\HttpValueCollectionTest.cs">
<Link>HttpValueCollectionTest.cs</Link>
</Compile>
Expand Down
4 changes: 0 additions & 4 deletions test/System.Net.Http.Formatting.Test/DataSets/HttpTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,7 @@ public static TestData<MediaTypeFormatter> StandardFormatters
{
new XmlMediaTypeFormatter(),
new JsonMediaTypeFormatter(),
#if !NETFX_CORE // not present in portable library version
new FormUrlEncodedMediaTypeFormatter()
#endif
});
}
}
Expand All @@ -521,9 +519,7 @@ public static TestData<MediaTypeFormatter> DerivedFormatters
{
new DerivedXmlMediaTypeFormatter(),
new DerivedJsonMediaTypeFormatter(),
#if !NETFX_CORE // not present in portable library version
new DerivedFormUrlEncodedMediaTypeFormatter(),
#endif
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@ public void TypeIsCorrect()
public void Constructor()
{
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version

Assert.Equal(3, collection.Count);
#else
Assert.Equal(2, collection.Count);
#endif
Assert.NotNull(collection.XmlFormatter);
Assert.NotNull(collection.JsonFormatter);
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
Assert.NotNull(collection.FormUrlEncodedFormatter);
#endif
}

[Fact]
Expand Down Expand Up @@ -101,14 +96,11 @@ public void Constructor1_AcceptsDuplicateFormatterTypes()
{
new XmlMediaTypeFormatter(),
new JsonMediaTypeFormatter(),
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
new FormUrlEncodedMediaTypeFormatter(),
#endif

new XmlMediaTypeFormatter(),
new JsonMediaTypeFormatter(),
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
new FormUrlEncodedMediaTypeFormatter(),
#endif
};

MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
Expand Down Expand Up @@ -185,7 +177,6 @@ public void JsonFormatter_ClearedByCtor()
Assert.Null(collection.JsonFormatter);
}

#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
[Fact]
public void FormUrlEncodedFormatter_SetByCtor()
{
Expand All @@ -200,8 +191,7 @@ public void FormUrlEncodedFormatter_ClearedByCtor()
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[0]);
Assert.Null(collection.FormUrlEncodedFormatter);
}
#endif


[Fact]
public void Remove_SetsXmlFormatter()
{
Expand Down Expand Up @@ -371,8 +361,8 @@ public void FindWriter_ReturnsFormatterOnMatch(Type variationType, object testDa
[InlineData(typeof(byte[]))]
#if !NETFX_CORE
[InlineData(typeof(XmlElement))]
[InlineData(typeof(FormDataCollection))]
#endif
[InlineData(typeof(FormDataCollection))]
public void IsTypeExcludedFromValidation_ReturnsTrueForExcludedTypes(Type type)
{
Assert.True(MediaTypeFormatterCollection.IsTypeExcludedFromValidation(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
#if NETFX_CORE
using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
#endif

namespace System.Net.Http
{
Expand Down Expand Up @@ -53,7 +57,6 @@ public static TheoryDataSet<string> FormData
"a+c=d+e",
"n1=v1&n2=v2",
"n1=v1a+v1b&n2=v2a+v2b",
"N=%c3%a6%c3%b8%c3%a5",
};
}
}
Expand Down Expand Up @@ -137,6 +140,21 @@ public async Task ReadAsFormDataAsync_HandlesFormData(string formData)
Assert.Equal(formData, data.ToString());
}

[Fact]
public async Task ReadAsFormDataAsync_HandlesFormData_Encoded()
{
// Arrange
string formData = "N=%c3%a6%c3%b8%c3%a5";
HttpContent content = new StringContent(formData);
content.Headers.ContentType = MediaTypeConstants.ApplicationFormUrlEncodedMediaType;

// Act
NameValueCollection data = await content.ReadAsFormDataAsync();

// Assert
Assert.Equal(formData, data.ToString(), ignoreCase: true);
}

[Theory]
[PropertyData("IrregularFormData")]
public async Task ReadAsFormDataAsync_HandlesIrregularFormData(string irregularFormData)
Expand All @@ -150,7 +168,11 @@ public async Task ReadAsFormDataAsync_HandlesIrregularFormData(string irregularF

// Assert
Assert.Equal(1, data.Count);
#if NETFX_CORE
Assert.Equal(irregularFormData, data.First().Key);
#else
Assert.Equal(irregularFormData, data.AllKeys[0]);
#endif
}

[Fact]
Expand Down