2 // MessageEncoderTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.ObjectModel;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
35 using NUnit.Framework;
36 using NUnit.Framework.Constraints;
37 using NUnit.Framework.SyntaxHelpers;
39 using TextElement = System.ServiceModel.Channels.TextMessageEncodingBindingElement;
40 using BinaryElement = System.ServiceModel.Channels.BinaryMessageEncodingBindingElement;
41 using MtomElement = System.ServiceModel.Channels.MtomMessageEncodingBindingElement;
43 namespace MonoTests.System.ServiceModel.Channels
46 public class MessageEncoderTest
48 class MyEncodingBindingElement : MessageEncodingBindingElement
50 public override BindingElement Clone ()
52 throw new Exception ();
55 public override T GetProperty<T> (BindingContext ctx)
57 throw new Exception ();
60 public override MessageEncoderFactory CreateMessageEncoderFactory ()
62 throw new Exception ();
65 public override MessageVersion MessageVersion {
66 get { throw new Exception (); }
67 set { throw new Exception (); }
72 public void Properties ()
74 MessageEncoder t = new TextElement ().CreateMessageEncoderFactory ().Encoder;
75 MessageEncoder b = new BinaryElement ().CreateMessageEncoderFactory ().Encoder;
76 MessageEncoder m = new MtomElement ().CreateMessageEncoderFactory ().Encoder;
78 // TextMessageEncodingBindingElement.WriteEncoding
79 // default value is UTF8.
80 ServiceAssert.AssertMessageEncoder (
81 // Those curly double quotations are smelly, very implementation specific.
82 "application/soap+xml; charset=utf-8", "application/soap+xml",
83 MessageVersion.Default, t, "Text");
84 ServiceAssert.AssertMessageEncoder (
85 "application/soap+msbin1", "application/soap+msbin1",
86 MessageVersion.Default, b, "Binary");
87 ServiceAssert.AssertMessageEncoder (
88 "multipart/related; type=\"application/xop+xml\"", "multipart/related",
89 MessageVersion.Default, m, "Mtom");
91 MessageEncoder t2 = new TextElement (
92 MessageVersion.Soap11WSAddressing10,
94 .CreateMessageEncoderFactory ().Encoder;
96 ServiceAssert.AssertMessageEncoder (
97 // Those curly double quotations are smelly, very implementation specific.
98 "text/xml; charset=utf-8", "text/xml",
99 MessageVersion.Soap11WSAddressing10, t2, "Text2");
103 [ExpectedException (typeof (ProtocolException))]
104 public void MessageVersionMismatch ()
106 Message msg = Message.CreateMessage (
107 // ... while BasicHttpBinding expects Soap11.
108 MessageVersion.Soap11WSAddressing10,
109 "http://tempuri.org/IFoo/Echo", "TEST");
110 MessageEncoderFactory f = new TextMessageEncodingBindingElement (MessageVersion.Soap11, Encoding.UTF8).CreateMessageEncoderFactory ();
111 f.Encoder.WriteMessage (msg, new MemoryStream ());
115 public void CreateSessionEncoder ()
117 Assert.AreEqual ("application/soap+xml", new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#1");
118 Assert.AreEqual ("application/soap+msbinsession1", new BinaryMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#2"); // different from application/soap+msbin1
119 Assert.AreEqual ("multipart/related", new MtomMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#3");
123 public void TestContentType ()
125 var element = new TextMessageEncodingBindingElement ();
126 element.WriteEncoding = Encoding.UTF8;
127 element.MessageVersion = MessageVersion.Soap11;
128 var factory = element.CreateMessageEncoderFactory ();
129 var encoder = factory.CreateSessionEncoder ();
131 Assert.That (encoder.IsContentTypeSupported ("text/xmL;chaRset=uTf-8"), Is.True, "#1");
132 Assert.That (encoder.IsContentTypeSupported ("text/xMl"), Is.True, "#2");
133 Assert.That (encoder.IsContentTypeSupported ("teXt/xml;foo=bar;charset=utf-8"), Is.True, "#3");
134 Assert.That (encoder.IsContentTypeSupported ("teXt/xml;charset=ascii"), Is.False, "#4");