HttpTransportBindingElementTest.cs 22 KB

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