Merge pull request #1860 from saper/tz-fix
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Description / MetadataResolverTest.cs
1 //
2 // MetadataResolverTest.cs
3 //
4 // Author:
5 //      Ankit Jain <JAnkit@novell.com>
6 //
7 // Copyright (C) 2006 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
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Linq;
33 using System.Net;
34 using System.Text;
35 using System.Runtime.Serialization;
36 using System.ServiceModel.Description;
37 using System.ServiceModel;
38
39 using NUnit.Framework;
40
41 using MonoTests.Helpers;
42
43 namespace MonoTests.System.ServiceModel.Description
44 {
45         [TestFixture]
46         public class MetadataResolverTest
47         {
48                 string url;
49                 //string url = "http://192.168.0.1:8080/echo/mex";
50
51                 static HttpListener listener;
52                 IAsyncResult current_request;
53                 int remaining, port;
54
55                 static readonly string mex = File.ReadAllText ("Test/System.ServiceModel.Description/dump.xml");
56
57                 [SetUp]
58                 public void StartupServer ()
59                 {
60                         if (listener != null)
61                                 listener.Stop ();
62                         listener = new HttpListener ();
63                         port = NetworkHelpers.FindFreePort ();
64                         url = "http://localhost:" + port + "/echo/mex";
65                         listener.Prefixes.Add ("http://*:" + port + "/echo/");
66                         listener.Start ();
67                         current_request = listener.BeginGetContext (OnReceivedRequest, null);
68                         remaining = 1;
69                 }
70                 
71                 void OnReceivedRequest (IAsyncResult result)
72                 {
73                         try {
74                                 var ctx = listener.EndGetContext (result);
75                                 current_request = null;
76                                 ctx.Response.ContentType = "application/soap+xml";
77                                 ctx.Response.ContentLength64 = mex.Length;
78                                 using (var sw = new StreamWriter (ctx.Response.OutputStream))
79                                         sw.Write (mex);
80                                 ctx.Response.Close ();
81                                 if (--remaining > 0)
82                                         current_request = listener.BeginGetContext (OnReceivedRequest, null);
83                         } catch (Exception ex) {
84                                 // ignore server errors in this test.
85                         }
86                 }
87                 
88                 [TearDown]
89                 public void ShutdownServer ()
90                 {
91                         listener.Stop ();
92                         listener = null;
93                 }
94
95                 [Test]
96                 [Category ("NotWorking")]
97                 public void ResolveNoEndpoint ()
98                 {
99                         ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
100                                 typeof (NonExistantContract),
101                                 new EndpointAddress (url));
102
103                         Assert.IsNotNull (endpoints);
104                         Assert.AreEqual (0, endpoints.Count);
105                 }
106
107                 [Test]
108                 [Category ("NotWorking")]
109                 public void Resolve1 ()
110                 {
111                         ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
112                                 typeof (IEchoService), new EndpointAddress (url));
113
114                         CheckIEchoServiceEndpoint (endpoints);
115                 }
116
117                 [Test]
118                 [Category ("NotWorking")]
119                 public void Resolve2 ()
120                 {
121                         ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
122                                 typeof (IEchoService),
123                                 new Uri (url),
124                                 MetadataExchangeClientMode.MetadataExchange);
125
126                         CheckIEchoServiceEndpoint (endpoints);
127                 }
128
129                 [Test]
130                 [Category ("NotWorking")]
131                 public void Resolve3 ()
132                 {
133                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
134                         List<ContractDescription> contracts = new List<ContractDescription> ();
135                         contracts.Add (contract);
136
137                         ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
138                                 contracts,
139                                 new Uri (url),
140                                 MetadataExchangeClientMode.MetadataExchange);
141
142                         CheckIEchoServiceEndpoint (endpoints);
143                 }
144
145                 [Test]
146                 [Category ("NotWorking")]
147                 public void Resolve4 ()
148                 {
149                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
150                         List<ContractDescription> contracts = new List<ContractDescription> ();
151                         contracts.Add (contract);
152                         contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
153
154                         ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
155                                 contracts,
156                                 new Uri (url),
157                                 MetadataExchangeClientMode.MetadataExchange);
158
159                         CheckIEchoServiceEndpoint (endpoints);
160                 }
161
162                 [Test]
163                 [Category ("NotWorking")]
164                 public void Resolve5 ()
165                 {
166                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
167                         List<ContractDescription> contracts = new List<ContractDescription> ();
168                         contracts.Add (contract);
169                         contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
170
171                         //FIXME: What is the 'client' param used for?
172                         //TODO: Write test cases for the related overloads of Resolve
173                         MetadataResolver.Resolve (
174                                 contracts,
175                                 new EndpointAddress (url),
176                                 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
177                 }
178
179                 [Test]
180                 [Category ("NotWorking")]
181                 public void Resolve6 ()
182                 {
183                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
184                         List<ContractDescription> contracts = new List<ContractDescription> ();
185                         contracts.Add (contract);
186                         contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
187
188                         //FIXME: What is the 'client' param used for?
189                         //TODO: Write test cases for the related overloads of Resolve
190                         MetadataResolver.Resolve (
191                                 contracts,
192                                 new Uri (url),
193                                 MetadataExchangeClientMode.MetadataExchange,
194                                 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
195                 }
196
197
198                 //Negative tests
199
200                 [Test]
201                 [ExpectedException (typeof (ArgumentNullException))]
202                 public void ErrResolve1 ()
203                 {
204                         MetadataResolver.Resolve (
205                                 typeof (IEchoService),
206                                 null,
207                                 MetadataExchangeClientMode.MetadataExchange);
208                 }
209
210                 [Test]
211                 [ExpectedException (typeof (InvalidOperationException))]
212                 [Ignore ("does not fail on .NET either")]
213                 public void ErrResolve2 ()
214                 {
215                         //Mex cannot be fetched with HttpGet from the given url
216                         MetadataResolver.Resolve (
217                                 typeof (IEchoService),
218                                 new Uri (url),
219                                 MetadataExchangeClientMode.HttpGet);
220                 }
221
222                 [Test]
223                 [ExpectedException (typeof (ArgumentNullException))]
224                 public void ErrResolve3 ()
225                 {
226                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
227                         List<ContractDescription> contracts = new List<ContractDescription> ();
228                         contracts.Add (contract);
229                         contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
230
231                         MetadataResolver.Resolve (contracts, new EndpointAddress (url),
232                                 (MetadataExchangeClient) null);
233                 }
234
235                 [Test]
236                 [ExpectedException (typeof (InvalidOperationException))]
237                 public void ErrResolve4 ()
238                 {
239                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
240                         List<ContractDescription> contracts = new List<ContractDescription> ();
241                         contracts.Add (contract);
242                         contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
243
244                         //FIXME: What is the 'client' param used for?
245                         //TODO: Write test cases for the related overloads of Resolve
246                         MetadataResolver.Resolve (
247                                 contracts,
248                                 new EndpointAddress ("http://localhost"),
249                                 new MetadataExchangeClient (new EndpointAddress (url)));
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (InvalidOperationException))]
254                 [Ignore ("does not fail on .NET either")]
255                 public void ErrResolve5 ()
256                 {
257                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
258                         List<ContractDescription> contracts = new List<ContractDescription> ();
259                         contracts.Add (contract);
260                         contracts.Add (ContractDescription.GetContract (typeof (NonExistantContract)));
261
262                         //FIXME: What is the 'client' param used for?
263                         //TODO: Write test cases for the related overloads of Resolve
264                         MetadataResolver.Resolve (
265                                 contracts,
266                                 new Uri (url),
267                                 MetadataExchangeClientMode.HttpGet,
268                                 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
269                 }
270
271                 [Test]
272                 [ExpectedException (typeof (ArgumentException))]
273                 public void ErrResolve6 ()
274                 {
275                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
276                         List<ContractDescription> contracts = new List<ContractDescription> ();
277
278                         //FIXME: What is the 'client' param used for?
279                         //TODO: Write test cases for the related overloads of Resolve
280                         MetadataResolver.Resolve (
281                                 contracts,
282                                 new Uri (url),
283                                 MetadataExchangeClientMode.HttpGet,
284                                 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
285                 }
286
287                 [Test]
288                 [ExpectedException (typeof (ArgumentNullException))]
289                 public void ErrResolve7 ()
290                 {
291                         MetadataResolver.Resolve (
292                                 null,
293                                 new Uri (url),
294                                 MetadataExchangeClientMode.HttpGet,
295                                 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
296                 }
297
298                 [Test]
299                 [ExpectedException (typeof (ArgumentNullException))]
300                 public void ErrResolve8 ()
301                 {
302                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
303                         List<ContractDescription> contracts = new List<ContractDescription> ();
304                         contracts.Add (contract);
305
306                         MetadataResolver.Resolve (contracts, null);
307                 }
308
309                 /* Test for bad endpoint address */
310                 [Test]
311                 [ExpectedException (typeof (InvalidOperationException))]
312                 public void ErrResolve9 ()
313                 {
314                         ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
315                         List<ContractDescription> contracts = new List<ContractDescription> ();
316                         contracts.Add (contract);
317
318                         MetadataResolver.Resolve (contracts, new EndpointAddress ("http://localhost"));
319                 }
320
321                 private void CheckIEchoServiceEndpoint (ServiceEndpointCollection endpoints)
322                 {
323                         Assert.IsNotNull (endpoints);
324                         Assert.AreEqual (1, endpoints.Count);
325
326                         ServiceEndpoint ep = endpoints [0];
327
328                         //URI Dependent
329                         //Assert.AreEqual ("http://localhost:8080/echo/svc", ep.Address.Uri.AbsoluteUri, "#R1");
330                         Assert.AreEqual ("IEchoService", ep.Contract.Name, "#R3");
331                         Assert.AreEqual ("http://myns/echo", ep.Contract.Namespace, "#R4");
332                         Assert.AreEqual ("BasicHttpBinding_IEchoService", ep.Name, "#R5");
333
334                         Assert.AreEqual (typeof (BasicHttpBinding), ep.Binding.GetType (), "#R2");
335                 }
336
337                 [Test]
338                 [ExpectedException (typeof (InvalidOperationException))]
339                 public void ResolveNonContract ()
340                 {
341                         MetadataResolver.Resolve (
342                                 typeof (Int32), new EndpointAddress (url));
343                 }
344
345                 [Test]
346                 [ExpectedException (typeof (InvalidOperationException))]
347                 public void ResolveBadUri ()
348                 {
349                         MetadataResolver.Resolve (
350                                 typeof (IEchoService), new EndpointAddress ("http://localhost"));
351                 }
352
353                 [DataContract]
354                 public class dc
355                 {
356                         [DataMember]
357                         string field;
358                 }
359
360                 [ServiceContract (Namespace = "http://myns/echo")]
361                 public interface IEchoService
362                 {
363
364                         [OperationContract]
365                         string Echo (string msg, int num, dc d);
366
367                         [OperationContract]
368                         string DoubleIt (int it, string prefix);
369
370                 }
371
372                 [ServiceContract]
373                 public class NonExistantContract
374                 {
375                 }
376         }
377 }