Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Channels / HttpTransportBindingElementTest.cs
1 //
2 // HttpTransportBindingElementTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 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.Collections.ObjectModel;
30 using System.IO;
31 using System.Net;
32 using System.Net.Security;
33 using System.Reflection;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Description;
37 using System.Threading;
38 using System.Xml;
39 using NUnit.Framework;
40
41 #if NET_4_0
42 using System.Security.Authentication.ExtendedProtection;
43 #endif
44
45 namespace MonoTests.System.ServiceModel.Channels
46 {
47         [TestFixture]
48         public class HttpTransportBindingElementTest
49         {
50                 static BindingParameterCollection empty_params =
51                         new BindingParameterCollection ();
52
53                 [Test]
54                 public void DefaultValues ()
55                 {
56                         HttpTransportBindingElement be =
57                                 new HttpTransportBindingElement ();
58                         Assert.AreEqual (false, be.AllowCookies, "#1");
59                         Assert.AreEqual (AuthenticationSchemes.Anonymous,
60                                 be.AuthenticationScheme, "#2");
61                         Assert.AreEqual (false, be.BypassProxyOnLocal, "#3");
62                         Assert.AreEqual (default (HostNameComparisonMode),
63                                 be.HostNameComparisonMode, "#4");
64                         Assert.AreEqual (0x10000, be.MaxBufferSize, "#6");
65                         Assert.IsNull (be.ProxyAddress, "#7");
66                         Assert.AreEqual (AuthenticationSchemes.Anonymous,
67                                 be.ProxyAuthenticationScheme, "#8");
68                         Assert.AreEqual (String.Empty, be.Realm, "#9");
69                         Assert.AreEqual ("http", be.Scheme, "#10");
70                         Assert.AreEqual (default (TransferMode),
71                                 be.TransferMode, "#11");
72                         Assert.AreEqual (false,
73                                 be.UnsafeConnectionNtlmAuthentication, "#12");
74                         Assert.AreEqual (true, be.UseDefaultWebProxy, "#13");
75                 }
76
77                 [Test]
78                 public void CanBuildChannelFactory ()
79                 {
80                         HttpTransportBindingElement be =
81                                 new HttpTransportBindingElement ();
82                         BindingContext ctx = new BindingContext (
83                                 new CustomBinding (), empty_params);
84                         Assert.IsTrue (be.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
85                         Assert.IsFalse (be.CanBuildChannelFactory<IInputChannel> (ctx), "#2");
86                         Assert.IsFalse (be.CanBuildChannelFactory<IReplyChannel> (ctx), "#3");
87                         Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (ctx), "#4");
88                         // seems like it does not support session channels by itself ?
89                         Assert.IsFalse (be.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#5");
90                         Assert.IsFalse (be.CanBuildChannelFactory<IInputSessionChannel> (ctx), "#6");
91                         Assert.IsFalse (be.CanBuildChannelFactory<IReplySessionChannel> (ctx), "#7");
92                         Assert.IsFalse (be.CanBuildChannelFactory<IOutputSessionChannel> (ctx), "#8");
93
94                         // IServiceChannel is not supported
95                         Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
96                 }
97
98                 [Test]
99                 public void CanBuildChannelListener ()
100                 {
101                         HttpTransportBindingElement be =
102                                 new HttpTransportBindingElement ();
103                         BindingContext ctx = new BindingContext (
104                                 new CustomBinding (), empty_params);
105                         Assert.IsTrue (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
106                         Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
107                         Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
108                         Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
109                         // seems like it does not support session channels by itself ?
110                         Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
111                         Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
112                         Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
113                         Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
114
115                         // IServiceChannel is not supported
116                         Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
117                 }
118
119                 [Test]
120                 public void BuildChannelFactory ()
121                 {
122                         BindingContext ctx = new BindingContext (
123                                 new CustomBinding (
124                                         new HttpTransportBindingElement ()),
125                                 empty_params);
126                         // returns HttpChannelFactory
127                         IChannelFactory<IRequestChannel> f =
128                                 ctx.BuildInnerChannelFactory<IRequestChannel> ();
129                         f.Open (); // required
130                         IChannel c = f.CreateChannel (new EndpointAddress (
131                                 "http://www.mono-project.com"));
132                 }
133
134                 [Test]
135                 [ExpectedException (typeof (InvalidOperationException))]
136                 public void CreateChannelWithoutOpen ()
137                 {
138                         BindingContext ctx = new BindingContext (
139                                 new CustomBinding (
140                                         new HttpTransportBindingElement ()),
141                                 empty_params);
142                         // returns HttpChannelFactory
143                         IChannelFactory<IRequestChannel> f =
144                                 ctx.BuildInnerChannelFactory<IRequestChannel> ();
145                         IChannel c = f.CreateChannel (new EndpointAddress (
146                                 "http://www.mono-project.com"));
147                 }
148
149                 [Test]
150                 public void BuildChannelFactoryTwoHttp ()
151                 {
152                         BindingContext ctx = new BindingContext (
153                                 new CustomBinding (
154                                         new HttpTransportBindingElement (),
155                                         new HttpTransportBindingElement ()),
156                                 empty_params);
157                         ctx.BuildInnerChannelFactory<IRequestChannel> ();
158                 }
159
160                 [Test]
161                 public void BuildChannelFactoryHttpThenMessage ()
162                 {
163                         BindingContext ctx = new BindingContext (
164                                 new CustomBinding (
165                                         new HttpTransportBindingElement (),
166                                         new BinaryMessageEncodingBindingElement ()),
167                                 empty_params);
168                         IChannelFactory<IRequestChannel> cf =
169                                 ctx.BuildInnerChannelFactory<IRequestChannel> ();
170                         cf.Open ();
171                 }
172
173                 [Test]
174                 // with July CTP it still works ...
175                 public void BuildChannelFactoryHttpNoMessage ()
176                 {
177                         BindingContext ctx = new BindingContext (
178                                 new CustomBinding (
179                                         new HttpTransportBindingElement ()),
180                                 empty_params);
181                         IChannelFactory<IRequestChannel> cf =
182                                 ctx.BuildInnerChannelFactory<IRequestChannel> ();
183                         cf.Open ();
184                 }
185
186                 [Test]
187                 public void BuildChannelFactoryIgnoresRemaining ()
188                 {
189                         BindingContext ctx = new BindingContext (
190                                 new CustomBinding (
191                                         new HttpTransportBindingElement (),
192                                         new InvalidBindingElement ()),
193                                 empty_params);
194                         ctx.BuildInnerChannelFactory<IRequestChannel> ();
195                 }
196
197                 // Disable this test anytime when HttpTransportBindingElement.BuildChannelFactory() doesn't return ChannelFactoryBase`1 anymore. It's not an API requirement.
198                 [Test]
199                 [ExpectedException (typeof (ArgumentNullException))]
200                 public void BuildChannelFactory_CreateChannelNullVia ()
201                 {
202                         var ctx = new BindingContext (new CustomBinding (), empty_params);
203                         var cf = new HttpTransportBindingElement ().BuildChannelFactory<IRequestChannel> (ctx);
204                         Assert.IsTrue (cf is ChannelFactoryBase<IRequestChannel>, "#1");
205                         cf.Open ();
206                         cf.CreateChannel (new EndpointAddress ("http://localhost:8080"), null);
207                 }
208
209                 [Test]
210                 [ExpectedException (typeof (ArgumentException))]
211                 public void CreateChannelInvalidScheme ()
212                 {
213                         IChannelFactory<IRequestChannel> f = new BasicHttpBinding ().BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
214                         f.Open ();
215                         f.CreateChannel (new EndpointAddress ("stream:dummy"));
216                 }
217
218                 [Test]
219                 public void BuildChannelListenerWithoutListenUri ()
220                 {
221                         new BasicHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
222                 }
223
224                 // when AddressingVersion is None (in MessageVersion), then
225                 // EndpointAddress.Uri and via URIs must match.
226                 [Test]
227                 [ExpectedException (typeof (ArgumentException))]
228                 public void EndpointAddressAndViaMustMatchOnAddressingNone ()
229                 {
230                         try {
231                                 var ch = ChannelFactory<IFoo>.CreateChannel (new BasicHttpBinding (), new EndpointAddress ("http://localhost:37564/"), new Uri ("http://localhost:8080/HogeService"));
232                                 ((ICommunicationObject) ch).Close ();
233                         } catch (TargetInvocationException) {
234                                 // we throw this exception so far. Since it is
235                                 // very internal difference (channel is created
236                                 // inside ClientRuntimeChannel.ctor() while .NET
237                                 // does it in ChannelFactory<T>.CreateChannel(),
238                                 // there is no point of treating it as failure).
239                                 throw new ArgumentException ();
240                         }
241                 }
242
243                 [Test]
244                 public void GetPropertyMessageVersion ()
245                 {
246                         var be = new HttpTransportBindingElement ();
247                         var mv = be.GetProperty<MessageVersion> (new BindingContext (new CustomBinding (), empty_params));
248                         Assert.AreEqual (MessageVersion.Soap12WSAddressing10, mv, "#1");
249                 }
250
251                 [Test]
252                 public void GetPrpertyBindingDeliveryCapabilities ()
253                 {
254                         var be = new HttpTransportBindingElement ();
255                         var dc = be.GetProperty<IBindingDeliveryCapabilities> (new BindingContext (new CustomBinding (), empty_params));
256                         Assert.IsFalse (dc.AssuresOrderedDelivery, "#1");
257                         Assert.IsFalse (dc.QueuedDelivery, "#2");
258                 }
259
260                 [Test]
261                 public void GetPrpertySecurityCapabilities ()
262                 {
263                         var be = new HttpTransportBindingElement ();
264                         var sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
265                         Assert.IsNotNull (sec, "#1.1");
266                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#1.2");
267                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#1.3");
268                         Assert.IsFalse (sec.SupportsClientAuthentication, "#1.4");
269                         Assert.IsFalse (sec.SupportsClientWindowsIdentity, "#1.5");
270                         Assert.IsFalse (sec.SupportsServerAuthentication , "#1.6");
271
272                         be = new HttpTransportBindingElement ();
273                         be.AuthenticationScheme = AuthenticationSchemes.Negotiate;
274                         sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
275                         Assert.IsNotNull (sec, "#2.1");
276                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#2.2");
277                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#2.3");
278                         Assert.IsTrue (sec.SupportsClientAuthentication, "#2.4");
279                         Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#2.5");
280                         Assert.IsTrue (sec.SupportsServerAuthentication , "#2.6");
281
282                         // almost the same, only differ at SupportsServerAuth
283                         be = new HttpTransportBindingElement ();
284                         be.AuthenticationScheme = AuthenticationSchemes.Ntlm;
285                         sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
286                         Assert.IsNotNull (sec, "#3.1");
287                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#3.2");
288                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#3.3");
289                         Assert.IsTrue (sec.SupportsClientAuthentication, "#3.4");
290                         Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#3.5");
291                         Assert.IsFalse (sec.SupportsServerAuthentication , "#3.6");
292
293                         be = new HttpTransportBindingElement ();
294                         be.AuthenticationScheme = AuthenticationSchemes.Basic;
295                         sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
296                         Assert.IsNotNull (sec, "#4.1");
297                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#4.2");
298                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#4.3");
299                         Assert.IsTrue (sec.SupportsClientAuthentication, "#4.4");
300                         Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#4.5");
301                         Assert.IsFalse (sec.SupportsServerAuthentication , "#4.6");
302
303                         be = new HttpTransportBindingElement ();
304                         be.AuthenticationScheme = AuthenticationSchemes.Digest;
305                         sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
306                         Assert.IsNotNull (sec, "#5.1");
307                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#5.2");
308                         Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#5.3");
309                         Assert.IsTrue (sec.SupportsClientAuthentication, "#5.4");
310                         Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#5.5");
311                         Assert.IsFalse (sec.SupportsServerAuthentication , "#5.6");
312                 }
313
314                 #region contracts
315
316                 [ServiceContract]
317                 interface IFoo
318                 {
319                         [OperationContract]
320                         string DoWork (string s1, string s2);
321                 }
322
323                 #endregion
324
325                 #region connection test
326
327                 string svcret;
328
329                 [Test]
330                 [Ignore ("It somehow fails...")]
331                 // It is almost identical to http-low-level-binding
332                 public void LowLevelHttpConnection ()
333                 {
334                         HttpTransportBindingElement lel =
335                                 new HttpTransportBindingElement ();
336
337                         // Service
338                         BindingContext lbc = new BindingContext (
339                                 new CustomBinding (),
340                                 new BindingParameterCollection (),
341                                 new Uri ("http://localhost:37564"),
342                                 String.Empty, ListenUriMode.Explicit);
343                         listener = lel.BuildChannelListener<IReplyChannel> (lbc);
344
345                         try {
346
347                         listener.Open ();
348
349                         svcret = "";
350
351                         Thread svc = new Thread (delegate () {
352                                 try {
353                                         svcret = LowLevelHttpConnection_SetupService ();
354                                 } catch (Exception ex) {
355                                         svcret = ex.ToString ();
356                                 }
357                         });
358                         svc.Start ();
359
360                         // Client code goes here.
361
362                         HttpTransportBindingElement el =
363                                 new HttpTransportBindingElement ();
364                         BindingContext ctx = new BindingContext (
365                                 new CustomBinding (),
366                                 new BindingParameterCollection ());
367                         IChannelFactory<IRequestChannel> factory =
368                                 el.BuildChannelFactory<IRequestChannel> (ctx);
369
370                         factory.Open ();
371
372                         IRequestChannel request = factory.CreateChannel (
373                                 new EndpointAddress ("http://localhost:37564"));
374
375                         request.Open ();
376
377                         try {
378                         try {
379                                 Message reqmsg = Message.CreateMessage (
380                                         MessageVersion.Default, "Echo");
381                                 // sync version does not work here.
382                                 Message msg = request.Request (reqmsg, TimeSpan.FromSeconds (5));
383
384                                 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
385                                         msg.WriteMessage (w);
386                                 }
387
388                                 if (svcret != null)
389                                         Assert.Fail (svcret.Length > 0 ? svcret : "service code did not finish until this test expected.");
390                         } finally {
391                                 if (request.State == CommunicationState.Opened)
392                                         request.Close ();
393                         }
394                         } finally {
395                                 if (factory.State == CommunicationState.Opened)
396                                         factory.Close ();
397                         }
398                         } finally {
399                                 if (listener.State == CommunicationState.Opened)
400                                         listener.Close ();
401                         }
402                 }
403
404                 IChannelListener<IReplyChannel> listener;
405
406                 string LowLevelHttpConnection_SetupService ()
407                 {
408                         IReplyChannel reply = listener.AcceptChannel ();
409                         reply.Open ();
410                         if (!reply.WaitForRequest (TimeSpan.FromSeconds (10)))
411                                 return "No request reached here.";
412
413                         svcret = "Receiving request ...";
414                         RequestContext ctx = reply.ReceiveRequest ();
415                         if (ctx == null)
416                                 return "No request context returned.";
417
418                         svcret = "Starting reply ...";
419                         ctx.Reply (Message.CreateMessage (MessageVersion.Default, "Ack"));
420                         return null; // OK
421                 }
422
423                 #endregion
424
425                 #region metadata
426
427                 [Test]
428                 public void ExportPolicyDefault ()
429                 {
430                         IPolicyExportExtension binding_element = new HttpTransportBindingElement ();
431                         PolicyConversionContext conversion_context = new CustomPolicyConversionContext ();
432                         binding_element.ExportPolicy (new WsdlExporter (), conversion_context);
433
434                         PolicyAssertionCollection binding_assertions = conversion_context.GetBindingAssertions ();
435                         BindingElementCollection binding_elements = conversion_context.BindingElements;
436                         Assert.AreEqual (1, binding_assertions.Count, "#A0");
437                         Assert.AreEqual (0, binding_elements.Count, "#A1");
438
439                         // wsaw:UsingAddressing
440                         XmlNode using_addressing_node = FindAssertion (binding_assertions, "wsaw:UsingAddressing");
441                         Assert.AreEqual (true, using_addressing_node != null, "#B0");
442                         Assert.AreEqual ("UsingAddressing", using_addressing_node.LocalName, "#B1");
443                         Assert.AreEqual ("http://www.w3.org/2006/05/addressing/wsdl", using_addressing_node.NamespaceURI, "#B2");
444                         Assert.AreEqual (0, using_addressing_node.Attributes.Count, "#B3");
445                         Assert.AreEqual (0, using_addressing_node.ChildNodes.Count, "#B4");
446                         Assert.AreEqual (String.Empty, using_addressing_node.InnerText, "#B5");
447                 }
448
449                 [Test]
450                 public void ExportPolicy ()
451                 {
452                         HttpTransportBindingElement http_binding_element = new HttpTransportBindingElement ();
453
454                         //
455                         // Specify some non-default values
456                         //
457                         http_binding_element.AllowCookies = !http_binding_element.AllowCookies;
458                         http_binding_element.AuthenticationScheme = AuthenticationSchemes.Ntlm;
459                         http_binding_element.BypassProxyOnLocal = !http_binding_element.BypassProxyOnLocal;
460                         http_binding_element.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
461                         http_binding_element.KeepAliveEnabled = !http_binding_element.KeepAliveEnabled;
462                         http_binding_element.ManualAddressing = !http_binding_element.ManualAddressing;
463                         http_binding_element.MaxBufferPoolSize = http_binding_element.MaxBufferPoolSize / 2;
464                         http_binding_element.MaxBufferSize = http_binding_element.MaxBufferSize / 2;
465                         http_binding_element.MaxReceivedMessageSize = http_binding_element.MaxReceivedMessageSize / 2;
466                         http_binding_element.ProxyAddress = new Uri ("http://proxyaddress.com");
467                         http_binding_element.ProxyAuthenticationScheme = AuthenticationSchemes.Basic;
468                         http_binding_element.Realm = "RandomRealm";
469                         http_binding_element.TransferMode = TransferMode.Streamed;
470                         http_binding_element.UnsafeConnectionNtlmAuthentication = !http_binding_element.UnsafeConnectionNtlmAuthentication;
471                         http_binding_element.UseDefaultWebProxy = !http_binding_element.UseDefaultWebProxy;
472 #if NET_4_0
473                         http_binding_element.DecompressionEnabled = !http_binding_element.DecompressionEnabled;
474                         http_binding_element.ExtendedProtectionPolicy = new ExtendedProtectionPolicy (PolicyEnforcement.WhenSupported);
475 #endif
476
477                         // 
478                         // Actual call to ExportPolicy
479                         //
480                         IPolicyExportExtension binding_element = http_binding_element as IPolicyExportExtension;
481                         PolicyConversionContext conversion_context = new CustomPolicyConversionContext ();
482                         binding_element.ExportPolicy (new WsdlExporter (), conversion_context);
483
484                         PolicyAssertionCollection binding_assertions = conversion_context.GetBindingAssertions ();
485                         BindingElementCollection binding_elements = conversion_context.BindingElements;
486                         Assert.AreEqual (2, binding_assertions.Count, "#A0");
487                         Assert.AreEqual (0, binding_elements.Count, "#A1");
488
489                         // AuthenticationScheme - the only property that causes information to be exported.
490                         XmlNode authentication_node = FindAssertion (binding_assertions, "http:NtlmAuthentication");
491                         Assert.AreEqual (true, authentication_node != null, "#B0");
492                         Assert.AreEqual ("NtlmAuthentication", authentication_node.LocalName, "#B1");
493                         Assert.AreEqual ("http://schemas.microsoft.com/ws/06/2004/policy/http", authentication_node.NamespaceURI, "#B2");
494                         Assert.AreEqual (String.Empty, authentication_node.InnerText, "#B3");
495                         Assert.AreEqual (0, authentication_node.Attributes.Count, "#B4");
496                 }
497
498                 // For some reason PolicyAssertionCollection.Find is not working as expected,
499                 // so do the lookup manually.
500                 XmlNode FindAssertion (PolicyAssertionCollection assertionCollection, string name)
501                 {
502                         foreach (XmlNode node in assertionCollection)
503                                 if (node.Name == name)
504                                         return node;
505
506                         return null;
507                 }
508
509                 XmlNode FindAssertionByLocalName (PolicyAssertionCollection assertionCollection, string name)
510                 {
511                         foreach (XmlNode node in assertionCollection)
512                                 if (node.LocalName == name)
513                                         return node;
514                         
515                         return null;
516                 }
517
518                 class MyMessageEncodingElement : MessageEncodingBindingElement {
519                         MessageVersion version;
520                         
521                         public MyMessageEncodingElement (MessageVersion version)
522                         {
523                                 this.version = version;
524                         }
525                         
526                         public override BindingElement Clone ()
527                         {
528                                 return new MyMessageEncodingElement (version);
529                         }
530                         public override MessageEncoderFactory CreateMessageEncoderFactory ()
531                         {
532                                 throw new NotImplementedException ();
533                         }
534                         public override MessageVersion MessageVersion {
535                                 get {
536                                         return version;
537                                 }
538                                 set {
539                                         throw new NotImplementedException ();
540                                 }
541                         }
542                 }
543                 
544                 [Test]
545                 public void ExportPolicy_CustomEncoding_Soap12 ()
546                 {
547                         HttpTransportBindingElement binding_element = new HttpTransportBindingElement ();
548                         IPolicyExportExtension export_extension = binding_element as IPolicyExportExtension;
549                         PolicyConversionContext conversion_context = new CustomPolicyConversionContext ();
550                         conversion_context.BindingElements.Add (new MyMessageEncodingElement (MessageVersion.Soap12));
551                         export_extension.ExportPolicy (new WsdlExporter (), conversion_context);
552                         
553                         PolicyAssertionCollection binding_assertions = conversion_context.GetBindingAssertions ();
554                         BindingElementCollection binding_elements = conversion_context.BindingElements;
555                         Assert.AreEqual (0, binding_assertions.Count, "#A0");
556                         Assert.AreEqual (1, binding_elements.Count, "#A1");
557                 }
558                 
559                 [Test]
560                 public void ExportPolicy_CustomEncoding_Soap12August2004 ()
561                 {
562                         HttpTransportBindingElement binding_element = new HttpTransportBindingElement ();
563                         IPolicyExportExtension export_extension = binding_element as IPolicyExportExtension;
564                         PolicyConversionContext conversion_context = new CustomPolicyConversionContext ();
565                         conversion_context.BindingElements.Add (new MyMessageEncodingElement (MessageVersion.Soap12WSAddressingAugust2004));
566                         export_extension.ExportPolicy (new WsdlExporter (), conversion_context);
567                         
568                         PolicyAssertionCollection binding_assertions = conversion_context.GetBindingAssertions ();
569                         BindingElementCollection binding_elements = conversion_context.BindingElements;
570                         Assert.AreEqual (1, binding_assertions.Count, "#A0");
571                         Assert.AreEqual (1, binding_elements.Count, "#A1");
572                         
573                         // UsingAddressing
574                         XmlNode using_addressing_node = FindAssertionByLocalName (binding_assertions, "UsingAddressing");
575                         Assert.AreEqual (true, using_addressing_node != null, "#B0");
576                         Assert.AreEqual ("UsingAddressing", using_addressing_node.LocalName, "#B1");
577                         Assert.AreEqual ("http://schemas.xmlsoap.org/ws/2004/08/addressing/policy", using_addressing_node.NamespaceURI, "#B2");
578                         Assert.AreEqual (String.Empty, using_addressing_node.InnerText, "#B3");
579                         Assert.AreEqual (0, using_addressing_node.Attributes.Count, "#B4");
580                         Assert.AreEqual (0, using_addressing_node.ChildNodes.Count, "#B5");
581                 }
582
583                 #endregion
584     }
585 }