19560b9315095ec4183d79f33e2af1de49fe1114
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Channels / MessageEncoderTest.cs
1 //
2 // MessageEncoderTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.ObjectModel;
30 using System.IO;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using System.Text;
35 using NUnit.Framework;
36 using NUnit.Framework.Constraints;
37 using NUnit.Framework.SyntaxHelpers;
38
39 using TextElement = System.ServiceModel.Channels.TextMessageEncodingBindingElement;
40 using BinaryElement = System.ServiceModel.Channels.BinaryMessageEncodingBindingElement;
41 using MtomElement = System.ServiceModel.Channels.MtomMessageEncodingBindingElement;
42
43 namespace MonoTests.System.ServiceModel.Channels
44 {
45         [TestFixture]
46         public class MessageEncoderTest
47         {
48                 class MyEncodingBindingElement : MessageEncodingBindingElement
49                 {
50                         public override BindingElement Clone ()
51                         {
52                                 throw new Exception ();
53                         }
54
55                         public override T GetProperty<T> (BindingContext ctx)
56                         {
57                                 throw new Exception ();
58                         }
59
60                         public override MessageEncoderFactory CreateMessageEncoderFactory ()
61                         {
62                                 throw new Exception ();
63                         }
64
65                         public override MessageVersion MessageVersion {
66                                 get { throw new Exception (); }
67                                 set { throw new Exception (); }
68                         }
69                 }
70
71                 [Test]
72                 public void Properties ()
73                 {
74                         MessageEncoder t = new TextElement ().CreateMessageEncoderFactory ().Encoder;
75                         MessageEncoder b = new BinaryElement ().CreateMessageEncoderFactory ().Encoder;
76                         MessageEncoder m = new MtomElement ().CreateMessageEncoderFactory ().Encoder;
77
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");
90
91                         MessageEncoder t2 = new TextElement (
92                                 MessageVersion.Soap11WSAddressing10,
93                                 Encoding.UTF8)
94                                 .CreateMessageEncoderFactory ().Encoder;
95
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");
100                 }
101
102                 [Test]
103                 [ExpectedException (typeof (ProtocolException))]
104                 public void MessageVersionMismatch ()
105                 {
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 ());
112                 }
113
114                 [Test]
115                 public void CreateSessionEncoder ()
116                 {
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");
120                 }
121
122                 [Test]
123                 public void TestContentType ()
124                 {
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 ();
130                         
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");
135                 }
136
137         }
138 }