Skip to content

Commit d86dda5

Browse files
Yaroslav Yakhontovdougbu
Yaroslav Yakhontov
authored andcommitted
Added FormUrlEncodedMediaTypeFormatter to .NetStandard version of System.Net.Http.Formatting
1 parent 6662565 commit d86dda5

File tree

9 files changed

+73
-24
lines changed

9 files changed

+73
-24
lines changed

src/System.Net.Http.Formatting.NetCore/System.Net.Http.Formatting.NetCore.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedJson.cs">
6161
<Link>Formatting\FormUrlEncodedJson.cs</Link>
6262
</Compile>
63+
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedMediaTypeFormatter.cs">
64+
<Link>Formatting\FormUrlEncodedMediaTypeFormatter.cs</Link>
65+
</Compile>
6366
<Compile Include="..\System.Net.Http.Formatting\Formatting\IFormatterLogger.cs">
6467
<Link>Formatting\IFormatterLogger.cs</Link>
6568
</Compile>
@@ -117,6 +120,9 @@
117120
<Compile Include="..\System.Net.Http.Formatting\Formatting\XmlMediaTypeFormatter.cs">
118121
<Link>Formatting\XmlMediaTypeFormatter.cs</Link>
119122
</Compile>
123+
<Compile Include="..\System.Net.Http.Formatting\HttpContentFormDataExtensions.cs">
124+
<Link>HttpContentFormDataExtensions.cs</Link>
125+
</Compile>
120126
<Compile Include="..\System.Net.Http.Formatting\Internal\HttpValueCollection.cs">
121127
<Link>HttpValueCollection.cs</Link>
122128
</Compile>

src/System.Net.Http.Formatting.NetStandard/System.Net.Http.Formatting.NetStandard.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedJson.cs">
6060
<Link>Formatting\FormUrlEncodedJson.cs</Link>
6161
</Compile>
62+
<Compile Include="..\System.Net.Http.Formatting\Formatting\FormUrlEncodedMediaTypeFormatter.cs">
63+
<Link>Formatting\FormUrlEncodedMediaTypeFormatter.cs</Link>
64+
</Compile>
6265
<Compile Include="..\System.Net.Http.Formatting\Formatting\IFormatterLogger.cs">
6366
<Link>Formatting\IFormatterLogger.cs</Link>
6467
</Compile>
@@ -116,6 +119,9 @@
116119
<Compile Include="..\System.Net.Http.Formatting\Formatting\XmlMediaTypeFormatter.cs">
117120
<Link>Formatting\XmlMediaTypeFormatter.cs</Link>
118121
</Compile>
122+
<Compile Include="..\System.Net.Http.Formatting\HttpContentFormDataExtensions.cs">
123+
<Link>HttpContentFormDataExtensions.cs</Link>
124+
</Compile>
119125
<Compile Include="..\System.Net.Http.Formatting\Internal\HttpValueCollection.cs">
120126
<Link>Internal\HttpValueCollection.cs</Link>
121127
</Compile>

src/System.Net.Http.Formatting/Formatting/MediaTypeFormatterCollection.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ public JsonMediaTypeFormatter JsonFormatter
5959
get { return Items.OfType<JsonMediaTypeFormatter>().FirstOrDefault(); }
6060
}
6161

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

7270
internal MediaTypeFormatter[] WritingFormatters
7371
{
@@ -202,8 +200,8 @@ public static bool IsTypeExcludedFromValidation(Type type)
202200
return
203201
#if !NETFX_CORE
204202
typeof(XmlNode).IsAssignableFrom(type) ||
205-
typeof(FormDataCollection).IsAssignableFrom(type) ||
206203
#endif
204+
typeof(FormDataCollection).IsAssignableFrom(type) ||
207205
FormattingUtilities.IsJTokenType(type) ||
208206
typeof(XObject).IsAssignableFrom(type) ||
209207
typeof(Type).IsAssignableFrom(type) ||
@@ -260,9 +258,7 @@ private static IEnumerable<MediaTypeFormatter> CreateDefaultFormatters()
260258
{
261259
new JsonMediaTypeFormatter(),
262260
new XmlMediaTypeFormatter(),
263-
#if !NETFX_CORE
264261
new FormUrlEncodedMediaTypeFormatter()
265-
#endif
266262
};
267263
}
268264

src/System.Net.Http.Formatting/HttpContentFormDataExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using System.Web.Http;
11+
#if NETFX_CORE
12+
using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
13+
#endif
1114

1215
namespace System.Net.Http
1316
{

test/System.Net.Http.Formatting.NetCore.Test/System.Net.Http.Formatting.NetCore.Test.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,27 @@
6060
</CodeAnalysisDependentAssemblyPaths>
6161
</ItemGroup>
6262
<ItemGroup>
63+
<Compile Include="..\System.Net.Http.Formatting.Test\DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs">
64+
<Link>DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs</Link>
65+
</Compile>
6366
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\BsonMediaTypeFormatterTests.cs">
6467
<Link>Formatting\BsonMediaTypeFormatterTests.cs</Link>
6568
</Compile>
6669
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormDataCollectionTests.cs">
6770
<Link>Internal\FormDataCollectionTests.cs</Link>
6871
</Compile>
72+
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromContentTests.cs">
73+
<Link>Formatting\FormUrlEncodedFromContentTests.cs</Link>
74+
</Compile>
75+
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromUriQueryTests.cs">
76+
<Link>Formatting\FormUrlEncodedFromUriQueryTests.cs</Link>
77+
</Compile>
6978
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedJsonTests.cs">
7079
<Link>Internal\FormUrlEncodedJsonTests.cs</Link>
7180
</Compile>
81+
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedMediaTypeFormatterTests.cs">
82+
<Link>Formatting\FormUrlEncodedMediaTypeFormatterTests.cs</Link>
83+
</Compile>
7284
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\JsonNetSerializationTest.cs">
7385
<Link>Formatting\JsonNetSerializationTest.cs</Link>
7486
</Compile>
@@ -78,6 +90,9 @@
7890
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\Parsers\FormUrlEncodedParserTests.cs">
7991
<Link>Internal\FormUrlEncodedParserTests.cs</Link>
8092
</Compile>
93+
<Compile Include="..\System.Net.Http.Formatting.Test\HttpContentFormDataExtensionsTest.cs">
94+
<Link>HttpContentFormDataExtensionsTest.cs</Link>
95+
</Compile>
8196
<Compile Include="..\System.Net.Http.Formatting.Test\Internal\HttpValueCollectionTest.cs">
8297
<Link>HttpValueCollectionTest.cs</Link>
8398
</Compile>

test/System.Net.Http.Formatting.NetStandard.Test/System.Net.Http.Formatting.NetStandard.Test.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,27 @@
5353
</CodeAnalysisDependentAssemblyPaths>
5454
</ItemGroup>
5555
<ItemGroup>
56+
<Compile Include="..\System.Net.Http.Formatting.Test\DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs">
57+
<Link>DataSets\Types\DerivedFormUrlEncodedMediaTypeFormatter.cs</Link>
58+
</Compile>
5659
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\BsonMediaTypeFormatterTests.cs">
5760
<Link>Formatting\BsonMediaTypeFormatterTests.cs</Link>
5861
</Compile>
5962
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormDataCollectionTests.cs">
6063
<Link>Internal\FormDataCollectionTests.cs</Link>
6164
</Compile>
65+
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromContentTests.cs">
66+
<Link>Formatting\FormUrlEncodedFromContentTests.cs</Link>
67+
</Compile>
68+
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedFromUriQueryTests.cs">
69+
<Link>Formatting\FormUrlEncodedFromUriQueryTests.cs</Link>
70+
</Compile>
6271
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedJsonTests.cs">
6372
<Link>Internal\FormUrlEncodedJsonTests.cs</Link>
6473
</Compile>
74+
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\FormUrlEncodedMediaTypeFormatterTests.cs">
75+
<Link>Formatting\FormUrlEncodedMediaTypeFormatterTests.cs</Link>
76+
</Compile>
6577
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\JsonNetSerializationTest.cs">
6678
<Link>Formatting\JsonNetSerializationTest.cs</Link>
6779
</Compile>
@@ -71,6 +83,9 @@
7183
<Compile Include="..\System.Net.Http.Formatting.Test\Formatting\Parsers\FormUrlEncodedParserTests.cs">
7284
<Link>Internal\FormUrlEncodedParserTests.cs</Link>
7385
</Compile>
86+
<Compile Include="..\System.Net.Http.Formatting.Test\HttpContentFormDataExtensionsTest.cs">
87+
<Link>HttpContentFormDataExtensionsTest.cs</Link>
88+
</Compile>
7489
<Compile Include="..\System.Net.Http.Formatting.Test\Internal\HttpValueCollectionTest.cs">
7590
<Link>HttpValueCollectionTest.cs</Link>
7691
</Compile>

test/System.Net.Http.Formatting.Test/DataSets/HttpTestData.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,7 @@ public static TestData<MediaTypeFormatter> StandardFormatters
498498
{
499499
new XmlMediaTypeFormatter(),
500500
new JsonMediaTypeFormatter(),
501-
#if !NETFX_CORE // not present in portable library version
502501
new FormUrlEncodedMediaTypeFormatter()
503-
#endif
504502
});
505503
}
506504
}
@@ -521,9 +519,7 @@ public static TestData<MediaTypeFormatter> DerivedFormatters
521519
{
522520
new DerivedXmlMediaTypeFormatter(),
523521
new DerivedJsonMediaTypeFormatter(),
524-
#if !NETFX_CORE // not present in portable library version
525522
new DerivedFormUrlEncodedMediaTypeFormatter(),
526-
#endif
527523
});
528524
}
529525
}

test/System.Net.Http.Formatting.Test/Formatting/MediaTypeFormatterCollectionTests.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,11 @@ public void TypeIsCorrect()
2727
public void Constructor()
2828
{
2929
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection();
30-
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
30+
3131
Assert.Equal(3, collection.Count);
32-
#else
33-
Assert.Equal(2, collection.Count);
34-
#endif
3532
Assert.NotNull(collection.XmlFormatter);
3633
Assert.NotNull(collection.JsonFormatter);
37-
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
3834
Assert.NotNull(collection.FormUrlEncodedFormatter);
39-
#endif
4035
}
4136

4237
[Fact]
@@ -101,14 +96,11 @@ public void Constructor1_AcceptsDuplicateFormatterTypes()
10196
{
10297
new XmlMediaTypeFormatter(),
10398
new JsonMediaTypeFormatter(),
104-
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
10599
new FormUrlEncodedMediaTypeFormatter(),
106-
#endif
100+
107101
new XmlMediaTypeFormatter(),
108102
new JsonMediaTypeFormatter(),
109-
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
110103
new FormUrlEncodedMediaTypeFormatter(),
111-
#endif
112104
};
113105

114106
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(formatters);
@@ -185,7 +177,6 @@ public void JsonFormatter_ClearedByCtor()
185177
Assert.Null(collection.JsonFormatter);
186178
}
187179

188-
#if !NETFX_CORE // No FormUrlEncodedMediaTypeFormatter in portable library version
189180
[Fact]
190181
public void FormUrlEncodedFormatter_SetByCtor()
191182
{
@@ -200,8 +191,7 @@ public void FormUrlEncodedFormatter_ClearedByCtor()
200191
MediaTypeFormatterCollection collection = new MediaTypeFormatterCollection(new MediaTypeFormatter[0]);
201192
Assert.Null(collection.FormUrlEncodedFormatter);
202193
}
203-
#endif
204-
194+
205195
[Fact]
206196
public void Remove_SetsXmlFormatter()
207197
{
@@ -371,8 +361,8 @@ public void FindWriter_ReturnsFormatterOnMatch(Type variationType, object testDa
371361
[InlineData(typeof(byte[]))]
372362
#if !NETFX_CORE
373363
[InlineData(typeof(XmlElement))]
374-
[InlineData(typeof(FormDataCollection))]
375364
#endif
365+
[InlineData(typeof(FormDataCollection))]
376366
public void IsTypeExcludedFromValidation_ReturnsTrueForExcludedTypes(Type type)
377367
{
378368
Assert.True(MediaTypeFormatterCollection.IsTypeExcludedFromValidation(type));

test/System.Net.Http.Formatting.Test/HttpContentFormDataExtensionsTest.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
using System.Net.Http.Formatting;
66
using System.Net.Http.Headers;
77
using System.Text;
8+
using System.Linq;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.TestCommon;
12+
#if NETFX_CORE
13+
using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
14+
#endif
1115

1216
namespace System.Net.Http
1317
{
@@ -53,7 +57,6 @@ public static TheoryDataSet<string> FormData
5357
"a+c=d+e",
5458
"n1=v1&n2=v2",
5559
"n1=v1a+v1b&n2=v2a+v2b",
56-
"N=%c3%a6%c3%b8%c3%a5",
5760
};
5861
}
5962
}
@@ -137,6 +140,21 @@ public async Task ReadAsFormDataAsync_HandlesFormData(string formData)
137140
Assert.Equal(formData, data.ToString());
138141
}
139142

143+
[Fact]
144+
public async Task ReadAsFormDataAsync_HandlesFormData_Encoded()
145+
{
146+
// Arrange
147+
string formData = "N=%c3%a6%c3%b8%c3%a5";
148+
HttpContent content = new StringContent(formData);
149+
content.Headers.ContentType = MediaTypeConstants.ApplicationFormUrlEncodedMediaType;
150+
151+
// Act
152+
NameValueCollection data = await content.ReadAsFormDataAsync();
153+
154+
// Assert
155+
Assert.Equal(formData, data.ToString(), ignoreCase: true);
156+
}
157+
140158
[Theory]
141159
[PropertyData("IrregularFormData")]
142160
public async Task ReadAsFormDataAsync_HandlesIrregularFormData(string irregularFormData)
@@ -150,7 +168,11 @@ public async Task ReadAsFormDataAsync_HandlesIrregularFormData(string irregularF
150168

151169
// Assert
152170
Assert.Equal(1, data.Count);
171+
#if NETFX_CORE
172+
Assert.Equal(irregularFormData, data.First().Key);
173+
#else
153174
Assert.Equal(irregularFormData, data.AllKeys[0]);
175+
#endif
154176
}
155177

156178
[Fact]

0 commit comments

Comments
 (0)