2
0

MessageTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // MessageTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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.Globalization;
  30. using System.IO;
  31. using System.Runtime.Serialization;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.Xml;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.ServiceModel.Channels
  37. {
  38. [TestFixture]
  39. public class MessageTest
  40. {
  41. public static string GetSerializedMessage (Message msg)
  42. {
  43. StringWriter sw = new StringWriter ();
  44. XmlWriterSettings settings = new XmlWriterSettings ();
  45. settings.OmitXmlDeclaration = true;
  46. using (XmlWriter xw = XmlWriter.Create (sw, settings)) {
  47. msg.WriteMessage (xw);
  48. }
  49. return sw.ToString ();
  50. }
  51. [Test]
  52. public void CreateNullAction ()
  53. {
  54. StringWriter sw = new StringWriter ();
  55. using (XmlWriter w = XmlWriter.Create (sw)) {
  56. Message.CreateMessage (MessageVersion.Default, (string) null).WriteMessage (w);
  57. }
  58. Assert.IsTrue (sw.ToString ().IndexOf ("Action") < 0);
  59. }
  60. [Test]
  61. [ExpectedException (typeof (ArgumentNullException))]
  62. public void CreateNullVersion ()
  63. {
  64. StringWriter sw = new StringWriter ();
  65. Message.CreateMessage (null, "http://tempuri.org/MyAction");
  66. }
  67. [Test]
  68. public void CreateSimpleAndWrite ()
  69. {
  70. 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>";
  71. Message msg = Message.CreateMessage (
  72. MessageVersion.Default, "Ack");
  73. // It should be XML comparison, not string comparison
  74. Assert.AreEqual (xml, GetSerializedMessage (msg));
  75. }
  76. [Test]
  77. public void CreateSimpleNonPrimitive ()
  78. {
  79. 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>";
  80. Message msg = Message.CreateMessage (
  81. MessageVersion.Default, "Sample1", new Sample1 ());
  82. // It should be XML comparison, not string comparison
  83. Assert.AreEqual (xml, GetSerializedMessage (msg));
  84. }
  85. [DataContract]
  86. class Sample1
  87. {
  88. [DataMember]
  89. public string Member1 = "member1";
  90. [DataMember]
  91. public Sample2 Member2 = new Sample2 ();
  92. }
  93. [DataContract]
  94. class Sample2
  95. {
  96. [DataMember]
  97. public string Member = "sample2 member";
  98. }
  99. [Test]
  100. public void CreateSimpleSoap11WSA1_0 ()
  101. {
  102. 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>";
  103. Message msg = Message.CreateMessage (
  104. MessageVersion.Soap11WSAddressing10,
  105. "http://tempuri.org/IFoo/Echo",
  106. "hoge");
  107. // It should be XML comparison, not string comparison
  108. Assert.AreEqual (xml, GetSerializedMessage (msg));
  109. }
  110. [Test]
  111. public void CreateFaultMessageAndWrite ()
  112. {
  113. 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>";
  114. FaultCode fc = new FaultCode ("me", "urn:me");
  115. FaultReasonText ft = new FaultReasonText ("am so lazy", CultureInfo.InvariantCulture);
  116. Message msg = Message.CreateMessage (
  117. MessageVersion.Default,
  118. MessageFault.CreateFault (fc, new FaultReason (ft)),
  119. "http://www.w3.org/2005/08/addressing/fault");
  120. // It should be XML comparison, not string comparison
  121. Assert.AreEqual (xml, GetSerializedMessage (msg));
  122. }
  123. [Test]
  124. public void CreateAndWriteBodyObject ()
  125. {
  126. 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>";
  127. Message msg = Message.CreateMessage (
  128. MessageVersion.Default, "Ack", "TEST");
  129. // It should be XML comparison, not string comparison
  130. Assert.AreEqual (xml, GetSerializedMessage (msg));
  131. }
  132. // From XmlReader
  133. [Test]
  134. public void CreateFromXmlReader ()
  135. {
  136. 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>";
  137. XmlReader r = XmlReader.Create (new StringReader (xml));
  138. Message msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
  139. Assert.AreEqual (MessageVersion.Default, msg.Version, "#2");
  140. Assert.AreEqual (2, msg.Headers.Count, "#4");
  141. Assert.AreEqual ("Ack", msg.Headers.Action, "#3");
  142. Assert.IsNull (msg.Headers.FaultTo, "#5");
  143. Assert.IsNull (msg.Headers.From, "#6");
  144. Assert.IsNull (msg.Headers.MessageId, "#7");
  145. Assert.IsTrue (msg.IsEmpty, "#8");
  146. MessageHeaderInfo hi = msg.Headers [0];
  147. Assert.AreEqual ("Action", hi.Name, "#h1-1");
  148. Assert.AreEqual ("http://www.w3.org/2005/08/addressing", hi.Namespace, "#h1-2");
  149. Assert.AreEqual (String.Empty, hi.Actor, "#h1-3");
  150. Assert.IsTrue (hi.MustUnderstand, "#h1-4");
  151. Assert.IsFalse (hi.Relay, "#h1-5");
  152. Assert.IsFalse (hi.IsReferenceParameter, "#h1-6");
  153. int count = 0;
  154. /* FIXME: test UnderstoodHeaders later
  155. foreach (MessageHeaderInfo i in msg.Headers.UnderstoodHeaders) {
  156. count++;
  157. Assert.AreEqual ("", i.Actor, "#9");
  158. Assert.IsFalse (i.IsReferenceParameter, "#10");
  159. Assert.IsTrue (i.MustUnderstand, "#11");
  160. Assert.AreEqual ("Action", i.Name, "#12");
  161. Assert.AreEqual ("http://www.w3.org/2005/08/addressing", i.Namespace, "#13");
  162. Assert.AreEqual (false, i.Relay, "#14");
  163. }
  164. Assert.AreEqual (1, count, "#15"); // allow only once
  165. */
  166. // MS implementation closes XmlReader regardless of
  167. // its initial state, which isn't good.
  168. //Assert.AreEqual (ReadState.Closed, r.ReadState, "#1");
  169. r = XmlReader.Create (new StringReader (xml));
  170. r.MoveToContent ();
  171. msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
  172. // ditto
  173. // Assert.AreEqual (ReadState.Closed, r.ReadState, "#1-2");
  174. 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>";
  175. r = XmlReader.Create (new StringReader (xml2));
  176. r.MoveToContent ();
  177. r.Read (); // s:Envelope
  178. msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
  179. // ditto
  180. // Assert.AreEqual (ReadState.Closed, r.ReadState, "#1-3");
  181. }
  182. [Test]
  183. public void CreateFromXmlReader2 ()
  184. {
  185. 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>";
  186. XmlReader r = XmlReader.Create (new StringReader (xml));
  187. Message msg = Message.CreateMessage (r, 10000, MessageVersion.Soap11WSAddressing10);
  188. Assert.AreEqual (0, msg.Headers.Count, "#1");
  189. Assert.IsNull (msg.Headers.Action, "#2");
  190. Assert.IsFalse (msg.IsEmpty, "#3");
  191. Assert.AreEqual ("hoge", msg.GetBody<string> (), "#4");
  192. }
  193. [Test]
  194. [ExpectedException (typeof (InvalidOperationException))]
  195. public void GetReaderAtBodyContentsEmptyError ()
  196. {
  197. 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>";
  198. XmlReader r = XmlReader.Create (new StringReader (xml));
  199. Message msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
  200. msg.GetReaderAtBodyContents ();
  201. }
  202. [Test]
  203. [ExpectedException (typeof (InvalidOperationException))]
  204. public void GetReaderAtBodyContentsTwice ()
  205. {
  206. 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>";
  207. XmlReader r = XmlReader.Create (new StringReader (xml));
  208. Message msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
  209. msg.GetReaderAtBodyContents ();
  210. msg.GetReaderAtBodyContents ();
  211. }
  212. [Test]
  213. public void GetReaderAtBodyContentsAfterSourceClosed ()
  214. {
  215. 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>";
  216. Message msg;
  217. using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
  218. msg = Message.CreateMessage (r, 10000, MessageVersion.Default);
  219. }
  220. // The reader is already closed by nature of using statement.
  221. XmlDictionaryReader r2 = msg.GetReaderAtBodyContents ();
  222. Assert.AreEqual (ReadState.Closed, r2.ReadState);
  223. }
  224. [Test]
  225. public void WriteMessagePOX ()
  226. {
  227. 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/\" />";
  228. Message m = Message.CreateMessage (MessageVersion.None, "Blah", (object) null);
  229. StringWriter sw = new StringWriter ();
  230. using (XmlWriter w = XmlWriter.Create (sw)) {
  231. m.WriteMessage (w);
  232. }
  233. Assert.AreEqual (xml, sw.ToString ());
  234. }
  235. [Test]
  236. public void ReadHeaders1 ()
  237. {
  238. 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>";
  239. using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
  240. Message m = Message.CreateMessage (r, 1000, MessageVersion.Default);
  241. Assert.AreEqual (2, m.Headers.Count, "#1");
  242. Assert.AreEqual (String.Empty, m.Headers.GetHeader<string> (0), "#2");
  243. Assert.AreEqual ("bah", m.Headers.GetHeader<string> (1), "#3");
  244. // 0, 1, then 0
  245. Assert.AreEqual (String.Empty, m.Headers.GetHeader<string> (0), "#2");
  246. }
  247. }
  248. [Test]
  249. public void ReadHeaders2 ()
  250. {
  251. 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>";
  252. using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
  253. Message m = Message.CreateMessage (r, 1000, MessageVersion.Default);
  254. Assert.AreEqual (1, m.Headers.Count, "#1");
  255. }
  256. }
  257. [Test]
  258. public void ReadHeadersMustUnderstand ()
  259. {
  260. // it includes mustUnderstand, but is ignored at
  261. // this stage.
  262. 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>";
  263. using (XmlReader r = XmlReader.Create (new StringReader (xml))) {
  264. Message m = Message.CreateMessage (r, 1000, MessageVersion.Default);
  265. Assert.AreEqual (1, m.Headers.Count, "#1");
  266. }
  267. }
  268. [Test]
  269. [Category ("NotWorking")]
  270. public void ToStringSomehowDoesNotConsumeMessage ()
  271. {
  272. Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
  273. Assert.AreEqual (m.ToString (), m.ToString ());
  274. }
  275. [Test]
  276. public void IsFault ()
  277. {
  278. Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
  279. Assert.IsFalse (m.IsFault, "#1");
  280. m = Message.CreateMessage (MessageVersion.Default, new FaultCode ("ActionNotSupported", "urn:myfault"), "I dunno", "urn:myaction");
  281. Assert.IsTrue (m.IsFault, "#2");
  282. }
  283. [Test]
  284. public void State ()
  285. {
  286. var msg = Message.CreateMessage (MessageVersion.Soap11, "urn:foo", (object) null);
  287. var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (TextWriter.Null));
  288. msg.WriteStartEnvelope (xw);
  289. Assert.AreEqual (MessageState.Created, msg.State, "#1");
  290. msg.WriteStartBody (xw);
  291. Assert.AreEqual (MessageState.Created, msg.State, "#2");
  292. }
  293. }
  294. }