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