Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Channels / TextMessageEncodingBindingElementTest.cs
1 //
2 // TextMessageEncodingBindingElementTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 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 System.Xml;
36 using NUnit.Framework;
37
38 using Element = System.ServiceModel.Channels.TextMessageEncodingBindingElement;
39
40 namespace MonoTests.System.ServiceModel.Channels
41 {
42         [TestFixture]
43         public class TextMessageEncodingBindingElementTest
44         {
45                 [Test]
46                 public void DefaultValues ()
47                 {
48                         Element el = new Element ();
49                         Assert.AreEqual (64, el.MaxReadPoolSize, "#1");
50                         Assert.AreEqual (16, el.MaxWritePoolSize, "#2");
51                         Assert.AreEqual (MessageVersion.Default, el.MessageVersion, "#3");
52                         // FIXME: test ReaderQuotas
53
54                         Assert.AreEqual (Encoding.UTF8, el.WriteEncoding, "#4");
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (ArgumentNullException))]
59                 public void BuildChannelListenerNullArg ()
60                 {
61                         new Element ().BuildChannelListener<IReplyChannel> (null);
62                 }
63
64                 [Test]
65                 public void CanBuildChannelFactory ()
66                 {
67                         CustomBinding cb = new CustomBinding (
68                                 new HttpTransportBindingElement ());
69                         BindingContext ctx = new BindingContext (
70                                 cb, new BindingParameterCollection ());
71                         Element el = new Element ();
72                         Assert.IsTrue (el.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
73                         Assert.IsFalse (el.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#2");
74                 }
75
76                 [Test]
77                 public void BuildChannelFactory ()
78                 {
79                         CustomBinding cb = new CustomBinding (
80                                 new HttpTransportBindingElement ());
81                         BindingContext ctx = new BindingContext (
82                                 cb, new BindingParameterCollection ());
83                         Element el = new Element ();
84                         IChannelFactory<IRequestChannel> cf =
85                                 el.BuildChannelFactory<IRequestChannel> (ctx);
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (InvalidOperationException))]
90                 public void BuildChannelListenerEmptyCustomBinding ()
91                 {
92                         CustomBinding cb = new CustomBinding ();
93                         BindingContext ctx = new BindingContext (
94                                 cb, new BindingParameterCollection ());
95                         new Element ().BuildChannelListener<IReplyChannel> (ctx);
96                 }
97
98                 [Test]
99                 public void BuildChannelListenerWithTransport ()
100                 {
101                         CustomBinding cb = new CustomBinding (
102                                 new HttpTransportBindingElement ());
103                         BindingContext ctx = new BindingContext (
104                                 cb, new BindingParameterCollection (),
105                                 new Uri ("http://localhost:8080"), String.Empty, ListenUriMode.Unique);
106                         new Element ().BuildChannelListener<IReplyChannel> (ctx);
107                 }
108
109                 [Test]
110                 public void MessageEncoderIsContentTypeSupported ()
111                 {
112                         var enc = new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().Encoder;
113                         Assert.IsFalse (enc.IsContentTypeSupported ("application/xml"), "#1");
114                         Assert.IsFalse (enc.IsContentTypeSupported ("text/xml"), "#2");
115                         Assert.IsTrue (enc.IsContentTypeSupported ("application/soap+xml"), "#3");
116                 }
117                 
118                 [Test]
119                 [ExpectedException (typeof (ArgumentNullException))]
120                 public void ReadNullStream ()
121                 {
122                         var enc = new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().Encoder;
123                         enc.ReadMessage (null, 10, "text/xml");
124                 }
125                 
126                 [Test]
127                 [ExpectedException (typeof (ArgumentNullException))]
128                 public void ReadNullBufferManager ()
129                 {
130                         var enc = new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().Encoder;
131                         enc.ReadMessage (new ArraySegment<byte> (new byte [0]), null, "text/xml");
132                 }
133                 
134                 [Test]
135                 [ExpectedException (typeof (XmlException))] // (document is expected)
136                 public void ReadEmptyBuffer ()
137                 {
138                         var enc = new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().Encoder;
139                         enc.ReadMessage (new ArraySegment<byte> (new byte [0]), BufferManager.CreateBufferManager (1000, 1000), "text/xml");
140                 }
141                 
142                 [Test]
143                 public void ActionContentTypeParameter ()
144                 {
145                         var enc = new TextMessageEncodingBindingElement ().CreateMessageEncoderFactory ().Encoder;
146                         var msg = Message.CreateMessage (MessageVersion.Soap12, "urn:foo");
147                         var ms = new MemoryStream ();
148                         using (var xw = XmlWriter.Create (ms))
149                                 msg.WriteMessage (xw);
150                         ms.Position = 0;
151                         msg = enc.ReadMessage (ms, 0x1000, "application/soap+xml; action=urn:bar");
152                         Assert.AreEqual ("urn:bar", msg.Headers.Action, "#1");
153                 }
154         }
155 }