ServiceDescriptionReflectorTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // ServiceDescriptionReflectorTest.cs
  3. //
  4. // Author:
  5. // Gert Driesen <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // Copyright (C) 2007 Gert Driesen
  9. // Copyright (C) 2006 Novell, Inc.
  10. //
  11. #if !MOBILE
  12. using NUnit.Framework;
  13. using System;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Web.Services;
  17. using System.Web.Services.Description;
  18. using System.Web.Services.Protocols;
  19. using System.Xml.Schema;
  20. using System.Xml.Serialization;
  21. namespace MonoTests.System.Web.Services.Description
  22. {
  23. [TestFixture]
  24. public class ServiceDescriptionReflectorTest
  25. {
  26. [Test]
  27. public void ReflectNullableInt ()
  28. {
  29. ServiceDescriptionReflector r =
  30. new ServiceDescriptionReflector ();
  31. r.Reflect (typeof (NullableContainer), null);
  32. ServiceDescription sd = r.ServiceDescriptions [0];
  33. XmlSchema xs = sd.Types.Schemas [0];
  34. XmlSchemaElement el = null;
  35. foreach (XmlSchemaElement e in xs.Items) {
  36. if (e.Name != "GetNullResponse")
  37. continue;
  38. el = e;
  39. break;
  40. }
  41. XmlSchemaComplexType ct =
  42. el.SchemaType as XmlSchemaComplexType;
  43. XmlSchemaSequence s = ct.Particle as XmlSchemaSequence;
  44. XmlSchemaElement e2 = s.Items [0] as XmlSchemaElement;
  45. Assert.IsTrue (e2.IsNillable);
  46. }
  47. [Test]
  48. [Category ("NotWorking")]
  49. public void IncludeTest ()
  50. {
  51. ServiceDescriptionReflector reflector = new ServiceDescriptionReflector ();
  52. reflector.Reflect (typeof (IncludeTestServices), "http://localhost/IncludeTestServices.asmx");
  53. Assert.AreEqual (0, reflector.Schemas.Count, "#1");
  54. Assert.AreEqual (1, reflector.ServiceDescriptions.Count, "#2");
  55. ServiceDescription sd = reflector.ServiceDescriptions[0];
  56. Assert.IsNull (sd.Name, "#3");
  57. Assert.AreEqual (1, sd.Types.Schemas.Count, "#4");
  58. StringWriter sw = new StringWriter ();
  59. sd.Write (sw);
  60. Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
  61. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  62. "<wsdl:definitions xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tm=\"http://microsoft.com/wsdl/mime/textMatching/\"" +
  63. " xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\"" +
  64. " xmlns:tns=\"http://tempuri.org/\" xmlns:s=\"http://www.w3.org/2001/XMLSchema\"" +
  65. " xmlns:soap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\"" +
  66. " xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" targetNamespace=\"http://tempuri.org/\"" +
  67. " xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\">{0}" +
  68. " <wsdl:types>{0}" +
  69. " <s:schema elementFormDefault=\"qualified\" targetNamespace=\"http://tempuri.org/\">{0}" +
  70. " <s:element name=\"EchoString\">{0}" +
  71. " <s:complexType>{0}" +
  72. " <s:sequence>{0}" +
  73. " <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"strval\" type=\"s:string\" />{0}" +
  74. " </s:sequence>{0}" +
  75. " </s:complexType>{0}" +
  76. " </s:element>{0}" +
  77. " <s:element name=\"EchoStringResponse\">{0}" +
  78. " <s:complexType>{0}" +
  79. " <s:sequence>{0}" +
  80. " <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"MyTime\" type=\"s:time\" />{0}" +
  81. " </s:sequence>{0}" +
  82. " </s:complexType>{0}" +
  83. " </s:element>{0}" +
  84. " <s:element name=\"Vehicle\">{0}" +
  85. " <s:complexType>{0}" +
  86. " <s:sequence>{0}" +
  87. " <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"licenseNumber\" type=\"s:string\" />{0}" +
  88. " </s:sequence>{0}" +
  89. " </s:complexType>{0}" +
  90. " </s:element>{0}" +
  91. " <s:element name=\"VehicleResponse\">{0}" +
  92. " <s:complexType>{0}" +
  93. " <s:sequence>{0}" +
  94. " <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"NewVehicle\" nillable=\"true\" type=\"tns:Vehicle\" />{0}" +
  95. " </s:sequence>{0}" +
  96. " </s:complexType>{0}" +
  97. " </s:element>{0}" +
  98. " <s:complexType name=\"Vehicle\" abstract=\"true\">{0}" +
  99. " <s:sequence>{0}" +
  100. " <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"licenseNumber\" type=\"s:string\" />{0}" +
  101. " <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"make\" type=\"s:dateTime\" />{0}" +
  102. " <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"age\" type=\"tns:TimeSpan\" />{0}" +
  103. " </s:sequence>{0}" +
  104. " </s:complexType>{0}" +
  105. " <s:complexType name=\"TimeSpan\" />{0}" +
  106. " <s:complexType name=\"Car\">{0}" +
  107. " <s:complexContent mixed=\"false\">{0}" +
  108. " <s:extension base=\"tns:Vehicle\" />{0}" +
  109. " </s:complexContent>{0}" +
  110. " </s:complexType>{0}" +
  111. " </s:schema>{0}" +
  112. " </wsdl:types>{0}" +
  113. " <wsdl:message name=\"EchoStringSoapIn\">{0}" +
  114. " <wsdl:part name=\"parameters\" element=\"tns:EchoString\" />{0}" +
  115. " </wsdl:message>{0}" +
  116. " <wsdl:message name=\"EchoStringSoapOut\">{0}" +
  117. " <wsdl:part name=\"parameters\" element=\"tns:EchoStringResponse\" />{0}" +
  118. " </wsdl:message>{0}" +
  119. " <wsdl:message name=\"VehicleSoapIn\">{0}" +
  120. " <wsdl:part name=\"parameters\" element=\"tns:Vehicle\" />{0}" +
  121. " </wsdl:message>{0}" +
  122. " <wsdl:message name=\"VehicleSoapOut\">{0}" +
  123. " <wsdl:part name=\"parameters\" element=\"tns:VehicleResponse\" />{0}" +
  124. " </wsdl:message>{0}" +
  125. " <wsdl:portType name=\"IncludeTestServicesSoap\">{0}" +
  126. " <wsdl:operation name=\"EchoString\">{0}" +
  127. " <wsdl:input message=\"tns:EchoStringSoapIn\" />{0}" +
  128. " <wsdl:output message=\"tns:EchoStringSoapOut\" />{0}" +
  129. " </wsdl:operation>{0}" +
  130. " <wsdl:operation name=\"Vehicle\">{0}" +
  131. " <wsdl:input message=\"tns:VehicleSoapIn\" />{0}" +
  132. " <wsdl:output message=\"tns:VehicleSoapOut\" />{0}" +
  133. " </wsdl:operation>{0}" +
  134. " </wsdl:portType>{0}" +
  135. " <wsdl:binding name=\"IncludeTestServicesSoap\" type=\"tns:IncludeTestServicesSoap\">{0}" +
  136. " <soap:binding transport=\"http://schemas.xmlsoap.org/soap/http\" />{0}" +
  137. " <wsdl:operation name=\"EchoString\">{0}" +
  138. " <soap:operation soapAction=\"http://tempuri.org/EchoString\" style=\"document\" />{0}" +
  139. " <wsdl:input>{0}" +
  140. " <soap:body use=\"literal\" />{0}" +
  141. " </wsdl:input>{0}" +
  142. " <wsdl:output>{0}" +
  143. " <soap:body use=\"literal\" />{0}" +
  144. " </wsdl:output>{0}" +
  145. " </wsdl:operation>{0}" +
  146. " <wsdl:operation name=\"Vehicle\">{0}" +
  147. " <soap:operation soapAction=\"http://tempuri.org/Vehicle\" style=\"document\" />{0}" +
  148. " <wsdl:input>{0}" +
  149. " <soap:body use=\"literal\" />{0}" +
  150. " </wsdl:input>{0}" +
  151. " <wsdl:output>{0}" +
  152. " <soap:body use=\"literal\" />{0}" +
  153. " </wsdl:output>{0}" +
  154. " </wsdl:operation>{0}" +
  155. " </wsdl:binding>{0}" +
  156. " <wsdl:binding name=\"IncludeTestServicesSoap12\" type=\"tns:IncludeTestServicesSoap\">{0}" +
  157. " <soap12:binding transport=\"http://schemas.xmlsoap.org/soap/http\" />{0}" +
  158. " <wsdl:operation name=\"EchoString\">{0}" +
  159. " <soap12:operation soapAction=\"http://tempuri.org/EchoString\" style=\"document\" />{0}" +
  160. " <wsdl:input>{0}" +
  161. " <soap12:body use=\"literal\" />{0}" +
  162. " </wsdl:input>{0}" +
  163. " <wsdl:output>{0}" +
  164. " <soap12:body use=\"literal\" />{0}" +
  165. " </wsdl:output>{0}" +
  166. " </wsdl:operation>{0}" +
  167. " <wsdl:operation name=\"Vehicle\">{0}" +
  168. " <soap12:operation soapAction=\"http://tempuri.org/Vehicle\" style=\"document\" />{0}" +
  169. " <wsdl:input>{0}" +
  170. " <soap12:body use=\"literal\" />{0}" +
  171. " </wsdl:input>{0}" +
  172. " <wsdl:output>{0}" +
  173. " <soap12:body use=\"literal\" />{0}" +
  174. " </wsdl:output>{0}" +
  175. " </wsdl:operation>{0}" +
  176. " </wsdl:binding>{0}" +
  177. " <wsdl:service name=\"IncludeTestServices\">{0}" +
  178. " <wsdl:port name=\"IncludeTestServicesSoap\" binding=\"tns:IncludeTestServicesSoap\">{0}" +
  179. " <soap:address location=\"http://localhost/IncludeTestServices.asmx\" />{0}" +
  180. " </wsdl:port>{0}" +
  181. " <wsdl:port name=\"IncludeTestServicesSoap12\" binding=\"tns:IncludeTestServicesSoap12\">{0}" +
  182. " <soap12:address location=\"http://localhost/IncludeTestServices.asmx\" />{0}" +
  183. " </wsdl:port>{0}" +
  184. " </wsdl:service>{0}" +
  185. "</wsdl:definitions>", Environment.NewLine), sw.ToString (), "#5");
  186. }
  187. [Test]
  188. [Category ("NotWorking")]
  189. public void ReflectTypeNonDefaultBinding ()
  190. {
  191. // bug #78953
  192. ServiceDescriptionReflector r =
  193. new ServiceDescriptionReflector ();
  194. r.Reflect (typeof (EdaInterface), "urn:foo");
  195. //foreach (ServiceDescription sss in r.ServiceDescriptions) sss.Write (Console.Out);
  196. // It should create two wsdls, one for www.DefaultNamespace.org and
  197. // another for urn:localBinding:local .
  198. Assert.AreEqual (2, r.ServiceDescriptions.Count, "#1");
  199. Assert.IsNotNull (r.ServiceDescriptions ["www.DefaultNamespace.org"], "#1-1");
  200. ServiceDescription sd = r.ServiceDescriptions ["urn:localBinding:local"];
  201. Assert.IsNotNull (sd, "#1-2");
  202. // Soap and Soap12
  203. Assert.AreEqual (2, sd.Bindings.Count, "#2-2.0");
  204. Binding b = sd.Bindings [0];
  205. Assert.AreEqual ("Local", b.Name, "#3");
  206. }
  207. [Test]
  208. public void Bug79087 ()
  209. {
  210. ServiceDescriptionReflector r =
  211. new ServiceDescriptionReflector ();
  212. r.Reflect (typeof (Bug79807Service), "urn:foo");
  213. StringWriter sw = new StringWriter ();
  214. r.ServiceDescriptions [0].Write (sw);
  215. ServiceDescription.Read (new StringReader (sw.ToString ()));
  216. }
  217. [Test]
  218. public void EmptyAction ()
  219. {
  220. ServiceDescriptionReflector r =
  221. new ServiceDescriptionReflector ();
  222. r.Reflect (typeof (EmptyActionService), "urn:foo");
  223. Binding b = r.ServiceDescriptions [0].Bindings ["EmptyActionServiceSoap"];
  224. OperationBinding o = b.Operations [0];
  225. SoapOperationBinding sob = o.Extensions [0] as SoapOperationBinding;
  226. Assert.AreEqual (String.Empty, sob.SoapAction);
  227. }
  228. [Test]
  229. public void Bug332150 ()
  230. {
  231. ServiceDescriptionReflector r =
  232. new ServiceDescriptionReflector ();
  233. r.Reflect (typeof (Bug332150Service), "urn:foo");
  234. StringWriter sw = new StringWriter ();
  235. r.ServiceDescriptions [0].Write (sw);
  236. ServiceDescription.Read (new StringReader (sw.ToString ()));
  237. }
  238. [Test]
  239. public void Bug345448 ()
  240. {
  241. ServiceDescriptionReflector r =
  242. new ServiceDescriptionReflector ();
  243. r.Reflect (typeof (Bug345448Service), "urn:foo");
  244. ServiceDescription sd = r.ServiceDescriptions [0];
  245. Assert.AreEqual("Bug345448ServiceSoap", sd.Bindings [0].Name, "sd #1");
  246. Assert.AreEqual("Bug345448ServiceSoap12", sd.Bindings [1].Name, "sd #2");
  247. }
  248. [Test]
  249. public void Bug345449 ()
  250. {
  251. ServiceDescriptionReflector r =
  252. new ServiceDescriptionReflector ();
  253. r.Reflect (typeof (Bug345448Service), "urn:foo");
  254. ServiceDescription sd = r.ServiceDescriptions [0];
  255. Assert.AreEqual("Bug345448ServiceSoap", sd.Services [0].Ports [0].Name, "sd #3");
  256. Assert.AreEqual("Bug345448ServiceSoap12", sd.Services [0].Ports [1].Name, "sd #4");
  257. }
  258. [Test]
  259. public void Bug360241 ()
  260. {
  261. // Make sure the map for service client is properly created
  262. new Bug360241SoapHttpClientProtocol ();
  263. }
  264. public class IncludeTestServices : WebService
  265. {
  266. [WebMethod ()]
  267. [return: XmlElement ("MyTime", DataType = "time")]
  268. public DateTime EchoString ([XmlElement (DataType = "string")] string strval)
  269. {
  270. return DateTime.Now;
  271. }
  272. [WebMethod ()]
  273. [XmlInclude (typeof (Car))]
  274. public Vehicle Vehicle (string licenseNumber)
  275. {
  276. if (licenseNumber == "0") {
  277. Vehicle v = new Car ();
  278. v.licenseNumber = licenseNumber;
  279. return v;
  280. } else {
  281. return null;
  282. }
  283. }
  284. }
  285. [XmlRoot ("NewVehicle")]
  286. public abstract class Vehicle
  287. {
  288. public string licenseNumber;
  289. public DateTime make;
  290. public TimeSpan age;
  291. }
  292. public class Car : Vehicle
  293. {
  294. }
  295. public class NullableContainer
  296. {
  297. [WebMethod (Description="Test nullables")]
  298. public int? GetNull ()
  299. {
  300. return null;
  301. }
  302. }
  303. // bug #78953
  304. [WebServiceAttribute (Namespace = "www.DefaultNamespace.org")]
  305. [WebServiceBindingAttribute (Name = "Local", Namespace = "urn:localBinding:local")]
  306. public class EdaInterface : WebService
  307. {
  308. [WebMethod]
  309. public void Test ()
  310. {
  311. }
  312. [WebMethod]
  313. public void Test2 ()
  314. {
  315. }
  316. [WebMethod]
  317. [SoapDocumentMethodAttribute ("urn:localBinding:local:LocalBindingMethod",
  318. RequestNamespace = "urn:localBinding:local",
  319. Binding = "Local",
  320. Use = SoapBindingUse.Literal,
  321. ParameterStyle = SoapParameterStyle.Bare)]
  322. public void BindingMethod ()
  323. {
  324. }
  325. }
  326. // bug #79807
  327. public class Bug79807Item
  328. {
  329. public string stringOne;
  330. public string stringTwo;
  331. }
  332. public class Bug79807AnotherItem
  333. {
  334. public string stringOne;
  335. public string stringTwo;
  336. }
  337. [WebService]
  338. [SoapRpcService]
  339. public class Bug79807Service : WebService
  340. {
  341. [WebMethod]
  342. public Bug79807Item [] Method1 (int count)
  343. {
  344. Bug79807Item [] arr = new Bug79807Item [count];
  345. for (int i = 0;i < count;i++) {
  346. arr [i].stringOne = "one";
  347. arr [i].stringTwo = "two";
  348. }
  349. return arr;
  350. }
  351. [WebMethod]
  352. public Bug79807AnotherItem [] Method2 (int count)
  353. {
  354. Bug79807AnotherItem [] arr = new Bug79807AnotherItem [count];
  355. for (int i = 0;i < count;i++) {
  356. arr [i].stringOne = "one";
  357. arr [i].stringTwo = "two";
  358. }
  359. return arr;
  360. }
  361. }
  362. [WebService (Namespace = "http://tempuri.org/")]
  363. public class EmptyActionService : WebService
  364. {
  365. [WebMethod]
  366. [SoapDocumentMethod ("")]
  367. public string HelloWorld () {
  368. return "Hello World";
  369. }
  370. }
  371. [WebService (Namespace = "http://tempuri.org/")]
  372. [WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)]
  373. public abstract class Bug332150SecureWebService : WebService
  374. {
  375. public Bug332150SecureWebService ()
  376. {
  377. }
  378. [WebMethod]
  379. public bool Login (string userName, string password)
  380. {
  381. return true;
  382. }
  383. }
  384. [WebService (Namespace = "http://tempuri.org/")]
  385. [WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)]
  386. public class Bug332150Service : Bug332150SecureWebService
  387. {
  388. public Bug332150Service ()
  389. {
  390. }
  391. [WebMethod]
  392. public string HelloWorld ()
  393. {
  394. return "Hello World";
  395. }
  396. }
  397. [WebService (Namespace = "http://tempuri.org/")]
  398. [WebServiceBindingAttribute (Name = "AnotherBinding", Namespace = "http://tempuri.org/")]
  399. public class Bug345448Service : WebService
  400. {
  401. [WebMethod]
  402. //[SoapDocumentMethodAttribute (Binding="AnotherBinding")]
  403. public string HelloWorld ()
  404. {
  405. return "Hello World";
  406. }
  407. }
  408. [WebServiceBindingAttribute (Name = "AnotherBinding", Namespace = "http://tempuri.org/")]
  409. public class Bug360241SoapHttpClientProtocol : SoapHttpClientProtocol
  410. {
  411. }
  412. }
  413. }
  414. #endif