#if !MOBILE && !MONOMAC using System; using System.IO; using System.ServiceModel; using System.ServiceModel.Channels; using System.Text; using NUnit.Framework; namespace MonoTests.System.ServiceModel { [TestFixture] public class WebMessageEncodingBindingElementTest { [Test] [ExpectedException (typeof (ArgumentNullException))] public void Constructor () { new WebMessageEncodingBindingElement (null); } [Test] public void DefaultPropertyValues () { WebMessageEncodingBindingElement be = new WebMessageEncodingBindingElement (); Assert.AreEqual (Encoding.UTF8, be.WriteEncoding, "#1"); } MessageEncoder CreateEncoder () { WebMessageEncodingBindingElement m = new WebMessageEncodingBindingElement (); return m.CreateMessageEncoderFactory ().Encoder; } [Test] public void MessageEncoder () { var e = CreateEncoder (); Assert.AreEqual ("application/xml", e.MediaType, "#1"); Assert.AreEqual ("application/xml; charset=utf-8", e.ContentType, "#2"); } [Test] [ExpectedException (typeof (ProtocolException))] public void MessageEncoderWriteMessageSoap12 () { var e = CreateEncoder (); e.WriteMessage (Message.CreateMessage (MessageVersion.Soap12, "urn:foo"), Stream.Null); } [Test] public void MessageEncoderWriteMessageXml () { var e = CreateEncoder (); MemoryStream ms = new MemoryStream (); // FIXME: nothing -> "" (right now it outputs ) //e.WriteMessage (Message.CreateMessage (MessageVersion.None, "urn:foo"), ms); //Assert.AreEqual ("", Encoding.UTF8.GetString (ms.ToArray ())); e.WriteMessage (Message.CreateMessage (MessageVersion.None, "urn:foo", 135), ms); Assert.AreEqual ("135", Encoding.UTF8.GetString (ms.ToArray ())); } BindingContext CreateBindingContext () { return new BindingContext (new CustomBinding (new HttpTransportBindingElement () { AllowCookies = true }), new BindingParameterCollection ()); } BindingContext CreateBindingContext2 () { return new BindingContext (new CustomBinding (new TcpTransportBindingElement ()), new BindingParameterCollection ()); } [Test] public void CanBuildChannelFactory () { // with HttpTransport var m = new WebMessageEncodingBindingElement (); Assert.IsTrue (m.CanBuildChannelFactory (CreateBindingContext ()), "#1"); Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext ()), "#2"); Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext ()), "#3"); Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext ()), "#4"); // actually they are from transport var h = new HttpTransportBindingElement (); Assert.IsTrue (h.CanBuildChannelFactory (CreateBindingContext ()), "#5"); Assert.IsFalse (h.CanBuildChannelFactory (CreateBindingContext ()), "#6"); Assert.IsFalse (h.CanBuildChannelFactory (CreateBindingContext ()), "#7"); Assert.IsFalse (h.CanBuildChannelFactory (CreateBindingContext ()), "#8"); // with TcpTransport Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext2 ()), "#9"); Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext2 ()), "#10"); Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext2 ()), "#11"); Assert.IsFalse (m.CanBuildChannelFactory (CreateBindingContext2 ()), "#12"); // ... yes, actually they are from transport var t = new TcpTransportBindingElement (); Assert.IsFalse (t.CanBuildChannelFactory (CreateBindingContext2 ()), "#13"); Assert.IsFalse (t.CanBuildChannelFactory (CreateBindingContext2 ()), "#14"); Assert.IsFalse (t.CanBuildChannelFactory (CreateBindingContext2 ()), "#15"); Assert.IsFalse (t.CanBuildChannelFactory (CreateBindingContext2 ()), "#16"); } [Test] public void CanBuildChannelListener () { // with HttpTransport var m = new WebMessageEncodingBindingElement (); Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext ()), "#1"); Assert.IsTrue (m.CanBuildChannelListener (CreateBindingContext ()), "#2"); Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext ()), "#3"); Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext ()), "#4"); // actually they are from transport var h = new HttpTransportBindingElement (); Assert.IsFalse (h.CanBuildChannelListener (CreateBindingContext ()), "#5"); Assert.IsTrue (h.CanBuildChannelListener (CreateBindingContext ()), "#6"); Assert.IsFalse (h.CanBuildChannelListener (CreateBindingContext ()), "#7"); Assert.IsFalse (h.CanBuildChannelListener (CreateBindingContext ()), "#8"); // with TcpTransport Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext2 ()), "#9"); Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext2 ()), "#10"); Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext2 ()), "#11"); Assert.IsFalse (m.CanBuildChannelListener (CreateBindingContext2 ()), "#12"); // ... yes, actually they are from transport var t = new TcpTransportBindingElement (); Assert.IsFalse (t.CanBuildChannelListener (CreateBindingContext2 ()), "#13"); Assert.IsFalse (t.CanBuildChannelListener (CreateBindingContext2 ()), "#14"); Assert.IsFalse (t.CanBuildChannelListener (CreateBindingContext2 ()), "#15"); Assert.IsFalse (t.CanBuildChannelListener (CreateBindingContext2 ()), "#16"); } [Test] public void BuildChannelFactory () { var m = new WebMessageEncodingBindingElement (); var f = m.BuildChannelFactory (CreateBindingContext ()); Assert.AreEqual (f.GetType (), new HttpTransportBindingElement ().BuildChannelFactory (CreateBindingContext ()).GetType (), "#1"); } [Test] public void GetPropertyMessageVersion () { var m = new WebMessageEncodingBindingElement (); var bc = new BindingContext (new CustomBinding (), new BindingParameterCollection ()); Assert.AreEqual (MessageVersion.None, m.GetProperty (bc), "#1"); } [Test] public void ReadMessageNullContentType () { var e = CreateEncoder (); e.ReadMessage (new MemoryStream (), 10, null); } [Test] public void MessageEncoderIsContentTypeSupported () { var enc = new WebMessageEncodingBindingElement ().CreateMessageEncoderFactory ().Encoder; Assert.IsTrue (enc.IsContentTypeSupported ("application/xml"), "#1"); Assert.IsTrue (enc.IsContentTypeSupported ("text/xml"), "#2"); Assert.IsTrue (enc.IsContentTypeSupported ("application/soap+xml"), "#3"); Assert.IsTrue (enc.IsContentTypeSupported ("application/foobar+xml"), "#4"); // wow. Assert.IsTrue (enc.IsContentTypeSupported ("application"), "#5"); // wow. Assert.IsTrue (enc.IsContentTypeSupported (String.Empty), "#6"); // wow. } } } #endif