Jelajahi Sumber

[WCF]: Fix MessageEncoder.IsContentTypeSupported().

Use the internal 'Channels.ContentType' class to parse the
content type, make then comparision case-insensitive and ignore
missing parameters.
Martin Baulig 13 tahun lalu
induk
melakukan
ea2f2cde42

+ 16 - 4
mcs/class/System.ServiceModel/System.ServiceModel.Channels/MessageEncoder.cs

@@ -53,10 +53,22 @@ namespace System.ServiceModel.Channels
 		{
 			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,

+ 18 - 0
mcs/class/System.ServiceModel/Test/System.ServiceModel.Channels/MessageEncoderTest.cs

@@ -33,6 +33,8 @@ using System.ServiceModel.Channels;
 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 @@ namespace MonoTests.System.ServiceModel.Channels
 			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");
+		}
+
 	}
 }