File: MessageBufferTest.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (208 lines) | stat: -rw-r--r-- 5,973 bytes parent folder | download | duplicates (8)
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
using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using NUnit.Framework;

namespace MonoTests.System.ServiceModel.Channels
{
	[TestFixture]
	public class MessageBufferTest
	{
#if !MOBILE && !XAMMAC_4_5
		[Test]
		public void TestCreateNavigator ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "http://tempuri.org/");
			MessageBuffer mb = m.CreateBufferedCopy (1024);

			XPathNavigator nav = mb.CreateNavigator ();

			Assert.IsTrue (nav is SeekableXPathNavigator);
		}
#endif
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void TestCreateBufferedCopyTwice ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
			m.CreateBufferedCopy (64);
			m.CreateBufferedCopy (64);
		}

		[Test]
		public void TestCreateMessage ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
			Message m2 = Message.CreateMessage (MessageVersion.Default, "action", 1);
			MessageBuffer mb = m.CreateBufferedCopy (64);

			Message n = mb.CreateMessage ();
			Assert.AreEqual (m2.ToString (), n.ToString (), "#2");
		}

		[Test, ExpectedException (typeof (ArgumentNullException))]
		public void TestWriteMessageNull ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
			MessageBuffer mb = m.CreateBufferedCopy (64);

			mb.WriteMessage (null);
		}
		
		[Test]
		public void WriteMessageWithDictionaryWriter ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);

			StringBuilder sb = new StringBuilder ();
			XmlWriter w = XmlWriter.Create (sb);
			XmlDictionaryWriter dw = XmlDictionaryWriter.CreateDictionaryWriter (w);			
			m.WriteMessage (dw);
			dw.Close ();
		}

		[Test]
		public void TestWriteMessage ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, "action", 1);
			Message n = Message.CreateMessage (MessageVersion.Default, "action", 1);
			Assert.AreEqual (m.ToString (), n.ToString ());

			// Write out a message using a Binary XmlDictionaryWriter
			MemoryStream ms = new MemoryStream ();
			XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter (ms);
			m.WriteMessage (w);
			w.Close ();
			ms.Close ();
			byte [] expected = ms.GetBuffer ();
			StringBuilder sb = new StringBuilder ();
			sb.AppendLine ("expected: " + expected.Length);

			for (int i = 0; i < 24; i++) {
				byte b = expected [i];
				sb.Append (String.Format ("{0:X02} ", b));
			}
			string exp = sb.ToString ();

			// Write out an equivalent MessageBuffer
			MessageBuffer mb = n.CreateBufferedCopy (1024);
			sb = new StringBuilder ();
			ms = new MemoryStream ();
			mb.WriteMessage (ms);
			ms.Close ();
			byte [] actual = ms.GetBuffer ();
			sb.AppendLine ("actual: " + actual.Length);
			for (int i = 0; i < 24; i++) {
				byte b = actual [i];
				sb.Append (String.Format ("{0:X02} ", b));
			}
			string act = sb.ToString ();

//			Console.WriteLine (exp + "\n" + act);

			// So the lengths of the buffer are the same....
			Assert.AreEqual (expected.Length, actual.Length);

			// TODO: 
			// There are three possible XmlDictionaryWriters:
			// Binary, Dictionary, Mtom
			// 
			// It doesn't have a MIME type header, so it's definitely not Mtom.
			// Dictionary outputs text, so it's not that either.
			// It's got to be the Binary one.
			//
			// But the content differs, why? 

			// FIXME: we don't have AreNotEqual
			// Assert.AreNotEqual (expected, actual);
		}
		
		[Test]
		public void TestEmptyMessageBuffer ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, String.Empty);
			MessageBuffer b = m.CreateBufferedCopy (0);

			Assert.IsTrue (m.IsEmpty, "#1");
			Assert.AreEqual (0, b.BufferSize, "#2");
			Assert.AreEqual ("application/soap+msbin1", b.MessageContentType, "#3");
		}

		[Test]
		public void TestSimpleMessageBuffer ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, 
							   String.Empty,
							   new MyBodyWriter ());
			MessageBuffer b = m.CreateBufferedCopy (Int32.MaxValue);

			Assert.IsFalse (m.IsEmpty, "#1");
			Assert.AreEqual (0, b.BufferSize, "#2");
			Assert.AreEqual ("application/soap+msbin1", b.MessageContentType, "#3");
			// BodyWriter must not be used twice.
			using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
				b.CreateMessage ().WriteMessage (w);
			}
			using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
				b.CreateMessage ().WriteMessage (w);
			}
		}

		[Test, ExpectedException (typeof (ObjectDisposedException))]
		public void TestCreateMessageFromClosedBuffer ()
		{
			Message m = Message.CreateMessage (MessageVersion.Default, String.Empty);
			MessageBuffer b = m.CreateBufferedCopy (0);

			b.Close ();
			b.CreateMessage ();
		}

		[Test, ExpectedException (typeof (InvalidOperationException))]
		public void CreateBufferedCopyConsumesMessage ()
		{
			Message msg = Message.CreateMessage (
				MessageVersion.Default, "foo", new MyBodyWriter ());
			MessageBuffer buf = msg.CreateBufferedCopy (1);
			// This WriteMessage() complains that the Message
			// is already consumed.
			using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
				msg.WriteMessage (w);
			}
		}

		[Test]
		public void IsFaultCopied ()
		{
			var ret = Message.CreateMessage (MessageVersion.Soap12,
				MessageFault.CreateFault (new FaultCode ("mycode"), "private affair"),
				"http://tempuri.org/IFoo/Test");
			Assert.IsTrue (ret.IsFault, "#1");
			var mb = ret.CreateBufferedCopy (0x1000);
			ret = mb.CreateMessage ();
			Assert.IsTrue (ret.IsFault, "#2");
		}
	}

	internal class MyBodyWriter : BodyWriter
	{
		bool done;

		internal MyBodyWriter ()
			: base (false)
		{
		}

		protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
		{
			if (done)
				Assert.Fail ("Allow two or more write.");
			done = true;
		}
	}
}