diff --git a/src/System.Net.Http.Formatting/Formatting/FormUrlEncodedMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/FormUrlEncodedMediaTypeFormatter.cs
index 0a38a0c4..4b23d9a2 100644
--- a/src/System.Net.Http.Formatting/Formatting/FormUrlEncodedMediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/FormUrlEncodedMediaTypeFormatter.cs
@@ -18,11 +18,6 @@ public class FormUrlEncodedMediaTypeFormatter : MediaTypeFormatter
private const int MinBufferSize = 256;
private const int DefaultBufferSize = 32 * 1024;
- private static readonly MediaTypeHeaderValue[] _supportedMediaTypes = new MediaTypeHeaderValue[]
- {
- MediaTypeConstants.ApplicationFormUrlEncodedMediaType
- };
-
private int _readBufferSize = DefaultBufferSize;
private int _maxDepth = FormattingUtilities.DefaultMaxDepth;
@@ -31,10 +26,7 @@ public class FormUrlEncodedMediaTypeFormatter : MediaTypeFormatter
///
public FormUrlEncodedMediaTypeFormatter()
{
- foreach (MediaTypeHeaderValue value in _supportedMediaTypes)
- {
- SupportedMediaTypes.Add(value);
- }
+ SupportedMediaTypes.Add(MediaTypeConstants.ApplicationFormUrlEncodedMediaType);
}
///
diff --git a/src/System.Net.Http.Formatting/Formatting/JsonMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/JsonMediaTypeFormatter.cs
index 5f7a9ea7..d95d49b0 100644
--- a/src/System.Net.Http.Formatting/Formatting/JsonMediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/JsonMediaTypeFormatter.cs
@@ -22,12 +22,6 @@ namespace System.Net.Http.Formatting
///
public class JsonMediaTypeFormatter : MediaTypeFormatter
{
- private static readonly MediaTypeHeaderValue[] _supportedMediaTypes = new MediaTypeHeaderValue[]
- {
- MediaTypeConstants.ApplicationJsonMediaType,
- MediaTypeConstants.TextJsonMediaType
- };
-
private JsonSerializerSettings _jsonSerializerSettings;
private readonly IContractResolver _defaultContractResolver;
private int _maxDepth = FormattingUtilities.DefaultMaxDepth;
@@ -42,10 +36,8 @@ public class JsonMediaTypeFormatter : MediaTypeFormatter
public JsonMediaTypeFormatter()
{
// Set default supported media types
- foreach (MediaTypeHeaderValue value in _supportedMediaTypes)
- {
- SupportedMediaTypes.Add(value);
- }
+ SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType);
+ SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType);
// Initialize serializer
_defaultContractResolver = new JsonContractResolver(this);
diff --git a/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs b/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
index db3692e5..6e216387 100644
--- a/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
+++ b/src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
@@ -21,12 +21,6 @@ namespace System.Net.Http.Formatting
///
public class XmlMediaTypeFormatter : MediaTypeFormatter
{
- private static readonly MediaTypeHeaderValue[] _supportedMediaTypes = new MediaTypeHeaderValue[]
- {
- MediaTypeConstants.ApplicationXmlMediaType,
- MediaTypeConstants.TextXmlMediaType
- };
-
private ConcurrentDictionary _serializerCache = new ConcurrentDictionary();
private XmlDictionaryReaderQuotas _readerQuotas = FormattingUtilities.CreateDefaultReaderQuotas();
@@ -36,10 +30,8 @@ public class XmlMediaTypeFormatter : MediaTypeFormatter
public XmlMediaTypeFormatter()
{
// Set default supported media types
- foreach (MediaTypeHeaderValue value in _supportedMediaTypes)
- {
- SupportedMediaTypes.Add(value);
- }
+ SupportedMediaTypes.Add(MediaTypeConstants.ApplicationXmlMediaType);
+ SupportedMediaTypes.Add(MediaTypeConstants.TextXmlMediaType);
// Set default supported character encodings
SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
diff --git a/test/System.Net.Http.Formatting.Test.Unit/Formatting/FormUrlEncodedMediaTypeFormatterTests.cs b/test/System.Net.Http.Formatting.Test.Unit/Formatting/FormUrlEncodedMediaTypeFormatterTests.cs
index 318dcc64..cecc0bc0 100644
--- a/test/System.Net.Http.Formatting.Test.Unit/Formatting/FormUrlEncodedMediaTypeFormatterTests.cs
+++ b/test/System.Net.Http.Formatting.Test.Unit/Formatting/FormUrlEncodedMediaTypeFormatterTests.cs
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
+using System.Linq;
using System.Net.Http.Formatting.DataSets;
using System.Net.Http.Headers;
using System.Text;
@@ -25,6 +26,32 @@ public void TypeIsCorrect()
Assert.Type.HasProperties(typeof(FormUrlEncodedMediaTypeFormatter), TypeAssert.TypeProperties.IsPublicVisibleClass);
}
+ [Fact]
+ public void SupportedMediaTypes_HeaderValuesAreNotSharedBetweenInstances()
+ {
+ var formatter1 = new FormUrlEncodedMediaTypeFormatter();
+ var formatter2 = new FormUrlEncodedMediaTypeFormatter();
+
+ foreach (MediaTypeHeaderValue mediaType1 in formatter1.SupportedMediaTypes)
+ {
+ MediaTypeHeaderValue mediaType2 = formatter2.SupportedMediaTypes.Single(m => m.Equals(mediaType1));
+ Assert.NotSame(mediaType1, mediaType2);
+ }
+ }
+
+ [Fact]
+ public void SupportEncodings_ValuesAreNotSharedBetweenInstances()
+ {
+ var formatter1 = new FormUrlEncodedMediaTypeFormatter();
+ var formatter2 = new FormUrlEncodedMediaTypeFormatter();
+
+ foreach (Encoding encoding1 in formatter1.SupportedEncodings)
+ {
+ Encoding encoding2 = formatter2.SupportedEncodings.Single(e => e.Equals(encoding1));
+ Assert.NotSame(encoding1, encoding2);
+ }
+ }
+
[Theory]
[TestDataSet(typeof(HttpUnitTestDataSets), "StandardFormUrlEncodedMediaTypes")]
[Trait("Description", "FormUrlEncodedMediaTypeFormatter() constructor sets standard form URL encoded media types in SupportedMediaTypes.")]
diff --git a/test/System.Net.Http.Formatting.Test.Unit/Formatting/MediaTypeFormatterTestBase.cs b/test/System.Net.Http.Formatting.Test.Unit/Formatting/MediaTypeFormatterTestBase.cs
index ffba78da..a43ef14e 100644
--- a/test/System.Net.Http.Formatting.Test.Unit/Formatting/MediaTypeFormatterTestBase.cs
+++ b/test/System.Net.Http.Formatting.Test.Unit/Formatting/MediaTypeFormatterTestBase.cs
@@ -41,6 +41,32 @@ public void TypeIsCorrect()
Assert.Type.HasProperties(TypeAssert.TypeProperties.IsPublicVisibleClass);
}
+ [Fact]
+ public void SupportedMediaTypes_HeaderValuesAreNotSharedBetweenInstances()
+ {
+ var formatter1 = new TFormatter();
+ var formatter2 = new TFormatter();
+
+ foreach (MediaTypeHeaderValue mediaType1 in formatter1.SupportedMediaTypes)
+ {
+ MediaTypeHeaderValue mediaType2 = formatter2.SupportedMediaTypes.Single(m => m.Equals(mediaType1));
+ Assert.NotSame(mediaType1, mediaType2);
+ }
+ }
+
+ [Fact]
+ public void SupportEncodings_ValuesAreNotSharedBetweenInstances()
+ {
+ var formatter1 = new TFormatter();
+ var formatter2 = new TFormatter();
+
+ foreach (Encoding mediaType1 in formatter1.SupportedEncodings)
+ {
+ Encoding mediaType2 = formatter2.SupportedEncodings.Single(m => m.Equals(mediaType1));
+ Assert.NotSame(mediaType1, mediaType2);
+ }
+ }
+
[Fact]
public void SupportEncoding_DefaultSupportedMediaTypes()
{
@@ -60,7 +86,7 @@ public void ReadFromStreamAsync_ThrowsOnNull()
{
TFormatter formatter = new TFormatter();
Assert.ThrowsArgumentNull(() => { formatter.ReadFromStreamAsync(null, Stream.Null, null, null); }, "type");
- Assert.ThrowsArgumentNull(() => { formatter.WriteToStreamAsync(typeof(object), null, null, null, null); }, "stream");
+ Assert.ThrowsArgumentNull(() => { formatter.ReadFromStreamAsync(typeof(object), null, null, null); }, "stream");
}
[Fact]