Skip to content
Browse files

[WCF]: Fix MessageEncoder.IsContentTypeSupported().

Use the internal 'Channels.ContentType' class to parse the
content type, make then comparision case-insensitive and ignore
missing parameters.
  • Loading branch information...
1 parent cd8ca2f commit ea2f2cde4286fa7af223e0c6e4950f85e42e8208 @baulig committed Dec 22, 2012
View
20 mcs/class/System.ServiceModel/System.ServiceModel.Channels/MessageEncoder.cs
@@ -53,10 +53,22 @@ public virtual bool IsContentTypeSupported (string contentType)
{
if (contentType == null)
throw new ArgumentNullException ("contentType");
- int idx = contentType.IndexOf (';');
- if (idx > 0)
- return contentType.StartsWith (ContentType, StringComparison.Ordinal);
- return contentType == MediaType;
+
+ var expected = new ContentType (ContentType.ToLowerInvariant ());
+ expected.MediaType = MediaType.ToLowerInvariant ();
+ var check = new ContentType (contentType.ToLowerInvariant ());
+
+ if (!string.Equals (expected.MediaType, check.MediaType, StringComparison.InvariantCultureIgnoreCase))
+ return false;
+
+ foreach (string param in expected.Parameters.Keys) {
+ if (!check.Parameters.ContainsKey (param))
+ continue;
+ if (!object.Equals (expected.Parameters [param], check.Parameters [param]))
+ return false;
+ }
+
+ return true;
}
public Message ReadMessage (ArraySegment<byte> buffer,
View
18 mcs/class/System.ServiceModel/Test/System.ServiceModel.Channels/MessageEncoderTest.cs
@@ -33,6 +33,8 @@
using System.ServiceModel.Description;
using System.Text;
using NUnit.Framework;
+using NUnit.Framework.Constraints;
+using NUnit.Framework.SyntaxHelpers;
using TextElement = System.ServiceModel.Channels.TextMessageEncodingBindingElement;
using BinaryElement = System.ServiceModel.Channels.BinaryMessageEncodingBindingElement;
@@ -116,5 +118,21 @@ public void CreateSessionEncoder ()
Assert.AreEqual ("application/soap+msbinsession1", new BinaryMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#2"); // different from application/soap+msbin1
Assert.AreEqual ("multipart/related", new MtomMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#3");
}
+
+ [Test]
+ public void TestContentType ()
+ {
+ var element = new TextMessageEncodingBindingElement ();
+ element.WriteEncoding = Encoding.UTF8;
+ element.MessageVersion = MessageVersion.Soap11;
+ var factory = element.CreateMessageEncoderFactory ();
+ var encoder = factory.CreateSessionEncoder ();
+
+ Assert.That (encoder.IsContentTypeSupported ("text/xmL;chaRset=uTf-8"), Is.True, "#1");
+ Assert.That (encoder.IsContentTypeSupported ("text/xMl"), Is.True, "#2");
+ Assert.That (encoder.IsContentTypeSupported ("teXt/xml;foo=bar;charset=utf-8"), Is.True, "#3");
+ Assert.That (encoder.IsContentTypeSupported ("teXt/xml;charset=ascii"), Is.False, "#4");
+ }
+
}
}

0 comments on commit ea2f2cd

Please sign in to comment.
Something went wrong with that request. Please try again.