Skip to content

Commit

Permalink
Issue #47 Formatters need to clone headers before adding to Supported…
Browse files Browse the repository at this point in the history
…MediaTypes

Fix Xml and Json media type formatters so that each instance has new instances in the SupportedMediaTypes collection.
Also fixed a copy/paste issue in an unrelated test
  • Loading branch information
marcind committed Apr 20, 2012
1 parent ef6b375 commit e78eba5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,10 +26,7 @@ public class FormUrlEncodedMediaTypeFormatter : MediaTypeFormatter
/// </summary>
public FormUrlEncodedMediaTypeFormatter()
{
foreach (MediaTypeHeaderValue value in _supportedMediaTypes)
{
SupportedMediaTypes.Add(value);
}
SupportedMediaTypes.Add(MediaTypeConstants.ApplicationFormUrlEncodedMediaType);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ namespace System.Net.Http.Formatting
/// </summary>
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;
Expand All @@ -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);
Expand Down
12 changes: 2 additions & 10 deletions src/System.Net.Http.Formatting/Formatting/XmlMediaTypeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ namespace System.Net.Http.Formatting
/// </summary>
public class XmlMediaTypeFormatter : MediaTypeFormatter
{
private static readonly MediaTypeHeaderValue[] _supportedMediaTypes = new MediaTypeHeaderValue[]
{
MediaTypeConstants.ApplicationXmlMediaType,
MediaTypeConstants.TextXmlMediaType
};

private ConcurrentDictionary<Type, object> _serializerCache = new ConcurrentDictionary<Type, object>();
private XmlDictionaryReaderQuotas _readerQuotas = FormattingUtilities.CreateDefaultReaderQuotas();

Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ public void TypeIsCorrect()
Assert.Type.HasProperties<TFormatter, MediaTypeFormatter>(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()
{
Expand All @@ -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]
Expand Down

0 comments on commit e78eba5

Please sign in to comment.