File: MessageTest.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (381 lines) | stat: -rw-r--r-- 15,652 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// MessageTest.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//
// 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]
		public void ToStringSomehowDoesNotConsumeMessage ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
			Assert.AreEqual (m.ToString (), m.ToString ());
		}

		[Test]
		public void ToStringThenWriteMessageTwice ()
		{
			var mmm = Message.CreateMessage (MessageVersion.None, "urn:foo", XmlReader.Create (new StringReader ("<root>test</root>")));
			mmm.ToString ();
			mmm.ToString ();
			mmm.WriteMessage (XmlWriter.Create (TextWriter.Null));
			try {
				mmm.WriteMessage (XmlWriter.Create (TextWriter.Null));
				Assert.Fail ("not allowed; should raise InvalidOperationException");
			} catch (InvalidOperationException) {
				// dare avoid ExpectedException, to verify that the first call to WriteMessage() is valid.
			}
		}

		[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 IsFault2 ()
		{
			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 xmlns:s='http://www.w3.org/2003/05/soap-envelope'>
      <s:Code>
        <s:Value>s:Sender</s:Value>
        <s:Subcode>
          <s:Value>a:ActionNotSupported</s:Value>
        </s:Subcode>
      </s:Code>
      <s:Reason>
        <s:Text xml:lang='ja-JP'>message</s:Text>
      </s:Reason>
    </s:Fault>
  </s:Body>
</s:Envelope>";
			var msg = Message.CreateMessage (MessageVersion.Soap11, "urn:foo", XmlReader.Create (new StringReader (xml)));
			Assert.AreEqual ("urn:foo", msg.Headers.Action, "#1");
			msg.ToString ();
			Assert.IsFalse (msg.IsFault, "#2"); // version mismatch

			msg = Message.CreateMessage (MessageVersion.Soap12, "urn:foo", XmlReader.Create (new StringReader (xml)));
			Assert.AreEqual ("urn:foo", msg.Headers.Action, "#3");
			msg.ToString ();
			Assert.IsFalse (msg.IsFault, "#4"); // version match, but it doesn't set as true. It is set true only when it is constructed with fault objects.
		}

		[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");
		}
	}
}