| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- //
- // MessageTest.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2006 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- using System.Xml;
- using NUnit.Framework;
- namespace MonoTests.System.ServiceModel.Channels
- {
- [TestFixture]
- public class MessageTest
- {
- public static string GetSerializedMessage (Message msg)
- {
- StringWriter sw = new StringWriter ();
- XmlWriterSettings settings = new XmlWriterSettings ();
- settings.OmitXmlDeclaration = true;
- using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
- msg.WriteMessage (xw);
- }
- return sw.ToString ();
- }
- [Test]
- public void CreateNullAction ()
- {
- StringWriter sw = new StringWriter ();
- using (XmlWriter w = XmlWriter.Create (sw)) {
- Message.CreateMessage (MessageVersion.Default, (string) null).WriteMessage (w);
- }
- Assert.IsTrue (sw.ToString ().IndexOf ("Action") < 0);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void CreateNullVersion ()
- {
- StringWriter sw = new StringWriter ();
- Message.CreateMessage (null, "http://tempuri.org/MyAction");
- }
- [Test]
- public void CreateSimpleAndWrite ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">Ack</a:Action></s:Header><s:Body /></s:Envelope>";
- Message msg = Message.CreateMessage (
- MessageVersion.Default, "Ack");
- // It should be XML comparison, not string comparison
- Assert.AreEqual (xml, GetSerializedMessage (msg));
- }
- [Test]
- public void CreateSimpleNonPrimitive ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">Sample1</a:Action></s:Header><s:Body><MessageTest.Sample1 xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/MonoTests.System.ServiceModel.Channels""><Member1>member1</Member1><Member2><Member>sample2 member</Member></Member2></MessageTest.Sample1></s:Body></s:Envelope>";
- Message msg = Message.CreateMessage (
- MessageVersion.Default, "Sample1", new Sample1 ());
- // It should be XML comparison, not string comparison
- Assert.AreEqual (xml, GetSerializedMessage (msg));
- }
- [DataContract]
- class Sample1
- {
- [DataMember]
- public string Member1 = "member1";
- [DataMember]
- public Sample2 Member2 = new Sample2 ();
- }
- [DataContract]
- class Sample2
- {
- [DataMember]
- public string Member = "sample2 member";
- }
- [Test]
- public void CreateSimpleSoap11WSA1_0 ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Header><a:Action s:mustUnderstand=""1"">http://tempuri.org/IFoo/Echo</a:Action></s:Header><s:Body><string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">hoge</string></s:Body></s:Envelope>";
- Message msg = Message.CreateMessage (
- MessageVersion.Soap11WSAddressing10,
- "http://tempuri.org/IFoo/Echo",
- "hoge");
- // It should be XML comparison, not string comparison
- Assert.AreEqual (xml, GetSerializedMessage (msg));
- }
- [Test]
- public void CreateFaultMessageAndWrite ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">http://www.w3.org/2005/08/addressing/fault</a:Action></s:Header><s:Body><s:Fault><s:Code><s:Value xmlns:a=""urn:me"">a:me</s:Value></s:Code><s:Reason><s:Text xml:lang="""">am so lazy</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>";
- FaultCode fc = new FaultCode ("me", "urn:me");
- FaultReasonText ft = new FaultReasonText ("am so lazy", CultureInfo.InvariantCulture);
- Message msg = Message.CreateMessage (
- MessageVersion.Default,
- MessageFault.CreateFault (fc, new FaultReason (ft)),
- "http://www.w3.org/2005/08/addressing/fault");
- // It should be XML comparison, not string comparison
- Assert.AreEqual (xml, GetSerializedMessage (msg));
- }
- [Test]
- public void CreateAndWriteBodyObject ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">Ack</a:Action></s:Header><s:Body><string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">TEST</string></s:Body></s:Envelope>";
- Message msg = Message.CreateMessage (
- MessageVersion.Default, "Ack", "TEST");
- // It should be XML comparison, not string comparison
- Assert.AreEqual (xml, GetSerializedMessage (msg));
- }
- // From XmlReader
- [Test]
- public void CreateFromXmlReader ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">Ack</a:Action><TestHeaderItem>test</TestHeaderItem></s:Header><s:Body></s:Body></s:Envelope>";
- XmlReader r = XmlReader.Create (new StringReader (xml));
- Message msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
- Assert.AreEqual (MessageVersion.Default, msg.Version, "#2");
- Assert.AreEqual (2, msg.Headers.Count, "#4");
- Assert.AreEqual ("Ack", msg.Headers.Action, "#3");
- Assert.IsNull (msg.Headers.FaultTo, "#5");
- Assert.IsNull (msg.Headers.From, "#6");
- Assert.IsNull (msg.Headers.MessageId, "#7");
- Assert.IsTrue (msg.IsEmpty, "#8");
- MessageHeaderInfo hi = msg.Headers [0];
- Assert.AreEqual ("Action", hi.Name, "#h1-1");
- Assert.AreEqual ("http://www.w3.org/2005/08/addressing", hi.Namespace, "#h1-2");
- Assert.AreEqual (String.Empty, hi.Actor, "#h1-3");
- Assert.IsTrue (hi.MustUnderstand, "#h1-4");
- Assert.IsFalse (hi.Relay, "#h1-5");
- Assert.IsFalse (hi.IsReferenceParameter, "#h1-6");
- int count = 0;
- /* FIXME: test UnderstoodHeaders later
- foreach (MessageHeaderInfo i in msg.Headers.UnderstoodHeaders) {
- count++;
- Assert.AreEqual ("", i.Actor, "#9");
- Assert.IsFalse (i.IsReferenceParameter, "#10");
- Assert.IsTrue (i.MustUnderstand, "#11");
- Assert.AreEqual ("Action", i.Name, "#12");
- Assert.AreEqual ("http://www.w3.org/2005/08/addressing", i.Namespace, "#13");
- Assert.AreEqual (false, i.Relay, "#14");
- }
- Assert.AreEqual (1, count, "#15"); // allow only once
- */
- // MS implementation closes XmlReader regardless of
- // its initial state, which isn't good.
- //Assert.AreEqual (ReadState.Closed, r.ReadState, "#1");
- r = XmlReader.Create (new StringReader (xml));
- r.MoveToContent ();
- msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
- // ditto
- // Assert.AreEqual (ReadState.Closed, r.ReadState, "#1-2");
- string xml2 = @"<Wrap><s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">Ack</a:Action></s:Header><s:Body /></s:Envelope></Wrap>";
- r = XmlReader.Create (new StringReader (xml2));
- r.MoveToContent ();
- r.Read (); // s:Envelope
- msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
- // ditto
- // Assert.AreEqual (ReadState.Closed, r.ReadState, "#1-3");
- }
- [Test]
- public void CreateFromXmlReader2 ()
- {
- string xml = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Body><string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/"">hoge</string></s:Body></s:Envelope>";
- XmlReader r = XmlReader.Create (new StringReader (xml));
- Message msg = Message.CreateMessage (r, 10000, MessageVersion.Soap11WSAddressing10);
- Assert.AreEqual (0, msg.Headers.Count, "#1");
- Assert.IsNull (msg.Headers.Action, "#2");
- Assert.IsFalse (msg.IsEmpty, "#3");
- Assert.AreEqual ("hoge", msg.GetBody<string> (), "#4");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void GetReaderAtBodyContentsEmptyError ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header></s:Header><s:Body></s:Body></s:Envelope>";
- XmlReader r = XmlReader.Create (new StringReader (xml));
- Message msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
- msg.GetReaderAtBodyContents ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void GetReaderAtBodyContentsTwice ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header></s:Header><s:Body>TEST</s:Body></s:Envelope>";
- XmlReader r = XmlReader.Create (new StringReader (xml));
- Message msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
- msg.GetReaderAtBodyContents ();
- msg.GetReaderAtBodyContents ();
- }
- [Test]
- public void GetReaderAtBodyContentsAfterSourceClosed ()
- {
- string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header></s:Header><s:Body>TEST</s:Body></s:Envelope>";
- Message msg;
- using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
- msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
- }
- // The reader is already closed by nature of using statement.
- XmlDictionaryReader r2 = msg.GetReaderAtBodyContents ();
- Assert.AreEqual (ReadState.Closed, r2.ReadState);
- }
- [Test]
- public void WriteMessagePOX ()
- {
- string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><z:anyType i:nil=\"true\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:z=\"http://schemas.microsoft.com/2003/10/Serialization/\" />";
- Message m = Message.CreateMessage (MessageVersion.None, "Blah", (object) null);
- StringWriter sw = new StringWriter ();
- using (XmlWriter w = XmlWriter.Create (sw)) {
- m.WriteMessage (w);
- }
- Assert.AreEqual (xml, sw.ToString ());
- }
- [Test]
- public void ReadHeaders1 ()
- {
- string xml = "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'><s:Header><custom1 /><custom2>bah</custom2></s:Header><s:Body/></s:Envelope>";
- using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
- Message m = Message.CreateMessage (r, 1000, MessageVersion.Default);
- Assert.AreEqual (2, m.Headers.Count, "#1");
- Assert.AreEqual (String.Empty, m.Headers.GetHeader<string> (0), "#2");
- Assert.AreEqual ("bah", m.Headers.GetHeader<string> (1), "#3");
- // 0, 1, then 0
- Assert.AreEqual (String.Empty, m.Headers.GetHeader<string> (0), "#2");
- }
- }
- [Test]
- public void ReadHeaders2 ()
- {
- string xml = "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'><s:Header><u:Timestamp xmlns:u='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'><u:Created>blah</u:Created><u:Expires>bleh</u:Expires></u:Timestamp></s:Header><s:Body/></s:Envelope>";
- using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
- Message m = Message.CreateMessage (r, 1000, MessageVersion.Default);
- Assert.AreEqual (1, m.Headers.Count, "#1");
- }
- }
- [Test]
- public void ReadHeadersMustUnderstand ()
- {
- // it includes mustUnderstand, but is ignored at
- // this stage.
- string xml = "<s:Envelope xmlns:s='http://www.w3.org/2003/05/soap-envelope'><s:Header><blah s:mustUnderstand='true'>foo</blah></s:Header><s:Body/></s:Envelope>";
- using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
- Message m = Message.CreateMessage (r, 1000, MessageVersion.Default);
- Assert.AreEqual (1, m.Headers.Count, "#1");
- }
- }
- [Test]
- [Category ("NotWorking")]
- public void ToStringSomehowDoesNotConsumeMessage ()
- {
- Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
- Assert.AreEqual (m.ToString (), m.ToString ());
- }
- [Test]
- public void IsFault ()
- {
- Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
- Assert.IsFalse (m.IsFault, "#1");
- m = Message.CreateMessage (MessageVersion.Default, new FaultCode ("ActionNotSupported", "urn:myfault"), "I dunno", "urn:myaction");
- Assert.IsTrue (m.IsFault, "#2");
- }
- [Test]
- public void State ()
- {
- var msg = Message.CreateMessage (MessageVersion.Soap11, "urn:foo", (object) null);
- var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (TextWriter.Null));
- msg.WriteStartEnvelope (xw);
- Assert.AreEqual (MessageState.Created, msg.State, "#1");
- msg.WriteStartBody (xw);
- Assert.AreEqual (MessageState.Created, msg.State, "#2");
- }
- }
- }
|