2009-05-26 Atsushi Enomoto <atsushi@ximian.com>
[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
37 using TextElement = System.ServiceModel.Channels.TextMessageEncodingBindingElement;
38 using BinaryElement = System.ServiceModel.Channels.BinaryMessageEncodingBindingElement;
39 using MtomElement = System.ServiceModel.Channels.MtomMessageEncodingBindingElement;
40
41 namespace MonoTests.System.ServiceModel.Channels
42 {
43         [TestFixture]
44         public class MessageEncoderTest
45         {
46                 class MyEncodingBindingElement : MessageEncodingBindingElement
47                 {
48                         public override BindingElement Clone ()
49                         {
50                                 throw new Exception ();
51                         }
52
53                         public override T GetProperty<T> (BindingContext ctx)
54                         {
55                                 throw new Exception ();
56                         }
57
58                         public override MessageEncoderFactory CreateMessageEncoderFactory ()
59                         {
60                                 throw new Exception ();
61                         }
62
63                         public override MessageVersion MessageVersion {
64                                 get { throw new Exception (); }
65                                 set { throw new Exception (); }
66                         }
67                 }
68
69                 [Test]
70                 public void Properties ()
71                 {
72                         MessageEncoder t = new TextElement ().CreateMessageEncoderFactory ().Encoder;
73                         MessageEncoder b = new BinaryElement ().CreateMessageEncoderFactory ().Encoder;
74                         MessageEncoder m = new MtomElement ().CreateMessageEncoderFactory ().Encoder;
75
76                         // TextMessageEncodingBindingElement.WriteEncoding
77                         // default value is UTF8.
78                         ServiceAssert.AssertMessageEncoder (
79                                 // Those curly double quotations are smelly, very implementation specific.
80                                 "application/soap+xml; charset=utf-8", "application/soap+xml",
81                                 MessageVersion.Default, t, "Text");
82                         ServiceAssert.AssertMessageEncoder (
83                                 "application/soap+msbin1", "application/soap+msbin1",
84                                 MessageVersion.Default, b, "Binary");
85                         ServiceAssert.AssertMessageEncoder (
86                                 "multipart/related; type=\"application/xop+xml\"", "multipart/related",
87                                 MessageVersion.Default, m, "Mtom");
88
89                         MessageEncoder t2 = new TextElement (
90                                 MessageVersion.Soap11WSAddressing10,
91                                 Encoding.UTF8)
92                                 .CreateMessageEncoderFactory ().Encoder;
93
94                         ServiceAssert.AssertMessageEncoder (
95                                 // Those curly double quotations are smelly, very implementation specific.
96                                 "text/xml; charset=utf-8", "text/xml",
97                                 MessageVersion.Soap11WSAddressing10, t2, "Text2");
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ProtocolException))]
102                 public void MessageVersionMismatch ()
103                 {
104                         Message msg = Message.CreateMessage (
105                                 // ... while BasicHttpBinding expects Soap11.
106                                 MessageVersion.Soap11WSAddressing10,
107                                 "http://tempuri.org/IFoo/Echo", "TEST");
108                         MessageEncoderFactory f = new TextMessageEncodingBindingElement (MessageVersion.Soap11, Encoding.UTF8).CreateMessageEncoderFactory ();
109                         f.Encoder.WriteMessage (msg, new MemoryStream ());
110                 }
111
112                 [Test]
113                 public void CreateSessionEncoder ()
114                 {
115                         Assert.AreEqual ("application/soap+xml", new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#1");
116                         Assert.AreEqual ("application/soap+msbinsession1", new BinaryMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#2"); // different from application/soap+msbin1
117                         Assert.AreEqual ("multipart/related", new MtomMessageEncodingBindingElement ().CreateMessageEncoderFactory ().CreateSessionEncoder ().MediaType, "#3");
118                 }
119         }
120 }