ExportTests.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // TestExport.cs
  3. //
  4. // Author:
  5. // Martin Baulig <[email protected]>
  6. //
  7. // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Net;
  28. using System.Xml;
  29. using System.Collections.Generic;
  30. using System.ServiceModel;
  31. using System.ServiceModel.Channels;
  32. using System.ServiceModel.Description;
  33. using QName = System.Xml.XmlQualifiedName;
  34. using WS = System.Web.Services.Description;
  35. using NUnit.Framework;
  36. using NUnit.Framework.Constraints;
  37. using NUnit.Framework.SyntaxHelpers;
  38. namespace MonoTests.System.ServiceModel.MetadataTests {
  39. [TestFixture]
  40. public class TestExport {
  41. internal const string HttpUri = "http://tempuri.org/TestHttp/";
  42. [Test]
  43. public void SimpleExport ()
  44. {
  45. var label = new TestLabel ("DuplicateContract");
  46. var cd = new ContractDescription ("MyContract");
  47. var endpoint = new ServiceEndpoint (
  48. cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
  49. var exporter = new WsdlExporter ();
  50. exporter.ExportContract (cd);
  51. exporter.ExportEndpoint (endpoint);
  52. CheckExport (
  53. exporter, new QName ("MyContract", "http://tempuri.org/"),
  54. "BasicHttpBinding", 1, label);
  55. }
  56. [Test]
  57. public void DuplicateContract ()
  58. {
  59. var label = new TestLabel ("DuplicateContract");
  60. var cd = new ContractDescription ("MyContract");
  61. var endpoint = new ServiceEndpoint (
  62. cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
  63. var exporter = new WsdlExporter ();
  64. exporter.ExportContract (cd);
  65. exporter.ExportContract (cd);
  66. exporter.ExportEndpoint (endpoint);
  67. CheckExport (
  68. exporter, new QName ("MyContract", "http://tempuri.org/"),
  69. "BasicHttpBinding", 1, label);
  70. }
  71. [Test]
  72. public void DuplicateEndpoint ()
  73. {
  74. var label = new TestLabel ("DuplicateEndpoint");
  75. var cd = new ContractDescription ("MyContract");
  76. var endpoint = new ServiceEndpoint (
  77. cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
  78. var exporter = new WsdlExporter ();
  79. exporter.ExportEndpoint (endpoint);
  80. exporter.ExportEndpoint (endpoint);
  81. CheckExport (
  82. exporter, new QName ("MyContract", "http://tempuri.org/"),
  83. "BasicHttpBinding", 1, label);
  84. }
  85. [Test]
  86. public void DuplicateEndpoint2 ()
  87. {
  88. var label = new TestLabel ("DuplicateEndpoint2");
  89. var cd = new ContractDescription ("MyContract");
  90. var endpoint = new ServiceEndpoint (
  91. cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
  92. var endpoint2 = new ServiceEndpoint (
  93. cd, new BasicHttpBinding (), new EndpointAddress (HttpUri));
  94. var exporter = new WsdlExporter ();
  95. exporter.ExportEndpoint (endpoint);
  96. exporter.ExportEndpoint (endpoint);
  97. exporter.ExportEndpoint (endpoint2);
  98. CheckExport (
  99. exporter, new QName ("MyContract", "http://tempuri.org/"),
  100. "BasicHttpBinding", 2, label);
  101. }
  102. public static void CheckExport (
  103. WsdlExporter exporter, QName contractName, string bindingName,
  104. int countEndpoints, TestLabel label)
  105. {
  106. Assert.That (exporter.GeneratedWsdlDocuments, Is.Not.Null, label.Get ());
  107. Assert.That (exporter.GeneratedWsdlDocuments.Count, Is.EqualTo (1), label.Get ());
  108. var wsdl = exporter.GeneratedWsdlDocuments [0];
  109. CheckExport (wsdl, contractName, bindingName, countEndpoints, label);
  110. }
  111. public static void CheckExport (
  112. WS.ServiceDescription wsdl, QName contractName, string bindingName,
  113. int countEndpoints, TestLabel label)
  114. {
  115. label.EnterScope ("ServiceDescription");
  116. Assert.That (wsdl.TargetNamespace, Is.EqualTo (contractName.Namespace), label.Get ());
  117. Assert.That (wsdl.Name, Is.EqualTo ("service"), label.Get ());
  118. label.LeaveScope ();
  119. label.EnterScope ("Bindings");
  120. Assert.That (wsdl.Bindings, Is.Not.Null, label.Get ());
  121. Assert.That (wsdl.Bindings.Count, Is.EqualTo (countEndpoints), label.Get ());
  122. for (int i = 0; i < countEndpoints; i++) {
  123. label.EnterScope (string.Format ("#{0}", i+1));
  124. var binding = wsdl.Bindings [i];
  125. var expectedName = string.Format (
  126. "{0}_{1}{2}", bindingName, contractName.Name,
  127. i > 0 ? i.ToString () : "");
  128. Assert.That (binding.Name, Is.EqualTo (expectedName), label.Get ());
  129. Assert.That (binding.Type, Is.EqualTo (contractName), label.Get ());
  130. label.LeaveScope ();
  131. }
  132. label.LeaveScope ();
  133. label.EnterScope ("PortTypes");
  134. Assert.That (wsdl.PortTypes, Is.Not.Null, label.Get ());
  135. Assert.That (wsdl.PortTypes.Count, Is.EqualTo (1), label.Get ());
  136. var portType = wsdl.PortTypes [0];
  137. Assert.That (portType.Name, Is.EqualTo (contractName.Name), label.Get ());
  138. label.LeaveScope ();
  139. label.EnterScope ("Services");
  140. Assert.That (wsdl.Services, Is.Not.Null, label.Get ());
  141. Assert.That (wsdl.Services.Count, Is.EqualTo (1), label.Get ());
  142. var service = wsdl.Services [0];
  143. Assert.That (service.Name, Is.EqualTo ("service"), label.Get ());
  144. label.LeaveScope ();
  145. label.EnterScope ("Ports");
  146. Assert.That (service.Ports, Is.Not.Null, label.Get ());
  147. Assert.That (service.Ports.Count, Is.EqualTo (countEndpoints), label.Get ());
  148. for (int i = 0; i < countEndpoints; i++) {
  149. label.EnterScope (string.Format ("#{0}", i+1));
  150. var port = service.Ports [i];
  151. var expectedName = string.Format (
  152. "{0}_{1}{2}", bindingName, contractName.Name,
  153. i > 0 ? i.ToString () : "");
  154. var qname = new QName (expectedName, contractName.Namespace);
  155. Assert.That (port.Name, Is.EqualTo (qname.Name), label.Get ());
  156. Assert.That (port.Binding, Is.EqualTo (qname), label.Get ());
  157. label.LeaveScope ();
  158. }
  159. label.LeaveScope ();
  160. }
  161. [Test]
  162. public void Mtom_Policy ()
  163. {
  164. var label = new TestLabel ("Mtom_Policy");
  165. var contract = new ContractDescription ("MyContract");
  166. var binding = new BasicHttpBinding ();
  167. binding.MessageEncoding = WSMessageEncoding.Mtom;
  168. var endpoint = new ServiceEndpoint (
  169. contract, binding, new EndpointAddress (HttpUri));
  170. var exporter = new WsdlExporter ();
  171. exporter.ExportEndpoint (endpoint);
  172. Assert.That (exporter.GeneratedWsdlDocuments, Is.Not.Null, label.Get ());
  173. Assert.That (exporter.GeneratedWsdlDocuments.Count, Is.EqualTo (1), label.Get ());
  174. var wsdl = exporter.GeneratedWsdlDocuments [0];
  175. Assert.That (wsdl.Bindings, Is.Not.Null, label.Get ());
  176. Assert.That (wsdl.Bindings.Count, Is.EqualTo (1), label.Get ());
  177. var wsb = wsdl.Bindings [0];
  178. label.EnterScope ("Binding");
  179. Assert.That (wsb.Extensions, Is.Not.Null, label.Get ());
  180. Assert.That (wsb.Extensions.Count, Is.EqualTo (2), label.Get ());
  181. label.LeaveScope ();
  182. label.EnterScope ("Extensions");
  183. WS.SoapBinding soap = null;
  184. XmlElement xml = null;
  185. foreach (var extension in wsb.Extensions) {
  186. if (extension is WS.SoapBinding)
  187. soap = (WS.SoapBinding)extension;
  188. else if (extension is XmlElement)
  189. xml = (XmlElement)extension;
  190. else
  191. Assert.Fail ("Unknown extension.", label);
  192. }
  193. Assert.That (soap, Is.Not.Null, label.Get ());
  194. Assert.That (xml, Is.Not.Null, label.Get ());
  195. label.LeaveScope ();
  196. label.EnterScope ("Policy");
  197. var assertions = BindingTestAssertions.AssertPolicy (wsdl, xml, label);
  198. Assert.That (assertions.Count, Is.EqualTo (1), label.Get ());
  199. var assertion = assertions [0];
  200. Assert.That (assertion.NamespaceURI, Is.EqualTo ("http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization"), label.Get ());
  201. Assert.That (assertion.LocalName, Is.EqualTo ("OptimizedMimeSerialization"), label.Get ());
  202. label.LeaveScope ();
  203. }
  204. }
  205. }