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