2 // MetadataResolverTest.cs
5 // Ankit Jain <JAnkit@novell.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
35 using System.Runtime.Serialization;
36 using System.ServiceModel.Description;
37 using System.ServiceModel;
39 using NUnit.Framework;
41 using MonoTests.Helpers;
43 namespace MonoTests.System.ServiceModel.Description
46 public class MetadataResolverTest
49 //string url = "http://192.168.0.1:8080/echo/mex";
51 static HttpListener listener;
52 IAsyncResult current_request;
55 static readonly string mex = File.ReadAllText ("Test/System.ServiceModel.Description/dump.xml");
58 public void StartupServer ()
62 listener = new HttpListener ();
63 port = NetworkHelpers.FindFreePort ();
64 url = "http://localhost:" + port + "/echo/mex";
65 listener.Prefixes.Add ("http://*:" + port + "/echo/");
67 current_request = listener.BeginGetContext (OnReceivedRequest, null);
71 void OnReceivedRequest (IAsyncResult result)
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))
80 ctx.Response.Close ();
82 current_request = listener.BeginGetContext (OnReceivedRequest, null);
83 } catch (Exception ex) {
84 // ignore server errors in this test.
89 public void ShutdownServer ()
96 [Category ("NotWorking")]
97 public void ResolveNoEndpoint ()
99 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
100 typeof (NonExistantContract),
101 new EndpointAddress (url));
103 Assert.IsNotNull (endpoints);
104 Assert.AreEqual (0, endpoints.Count);
108 [Category ("NotWorking")]
109 public void Resolve1 ()
111 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
112 typeof (IEchoService), new EndpointAddress (url));
114 CheckIEchoServiceEndpoint (endpoints);
118 [Category ("NotWorking")]
119 public void Resolve2 ()
121 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
122 typeof (IEchoService),
124 MetadataExchangeClientMode.MetadataExchange);
126 CheckIEchoServiceEndpoint (endpoints);
130 [Category ("NotWorking")]
131 public void Resolve3 ()
133 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
134 List<ContractDescription> contracts = new List<ContractDescription> ();
135 contracts.Add (contract);
137 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
140 MetadataExchangeClientMode.MetadataExchange);
142 CheckIEchoServiceEndpoint (endpoints);
146 [Category ("NotWorking")]
147 public void Resolve4 ()
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)));
154 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
157 MetadataExchangeClientMode.MetadataExchange);
159 CheckIEchoServiceEndpoint (endpoints);
163 [Category ("NotWorking")]
164 public void Resolve5 ()
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)));
171 //FIXME: What is the 'client' param used for?
172 //TODO: Write test cases for the related overloads of Resolve
173 MetadataResolver.Resolve (
175 new EndpointAddress (url),
176 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
180 [Category ("NotWorking")]
181 public void Resolve6 ()
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)));
188 //FIXME: What is the 'client' param used for?
189 //TODO: Write test cases for the related overloads of Resolve
190 MetadataResolver.Resolve (
193 MetadataExchangeClientMode.MetadataExchange,
194 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
201 [ExpectedException (typeof (ArgumentNullException))]
202 public void ErrResolve1 ()
204 MetadataResolver.Resolve (
205 typeof (IEchoService),
207 MetadataExchangeClientMode.MetadataExchange);
211 [ExpectedException (typeof (InvalidOperationException))]
212 [Ignore ("does not fail on .NET either")]
213 public void ErrResolve2 ()
215 //Mex cannot be fetched with HttpGet from the given url
216 MetadataResolver.Resolve (
217 typeof (IEchoService),
219 MetadataExchangeClientMode.HttpGet);
223 [ExpectedException (typeof (ArgumentNullException))]
224 public void ErrResolve3 ()
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)));
231 MetadataResolver.Resolve (contracts, new EndpointAddress (url),
232 (MetadataExchangeClient) null);
236 [ExpectedException (typeof (InvalidOperationException))]
237 public void ErrResolve4 ()
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)));
244 //FIXME: What is the 'client' param used for?
245 //TODO: Write test cases for the related overloads of Resolve
246 MetadataResolver.Resolve (
248 new EndpointAddress ("http://localhost"),
249 new MetadataExchangeClient (new EndpointAddress (url)));
253 [ExpectedException (typeof (InvalidOperationException))]
254 [Ignore ("does not fail on .NET either")]
255 public void ErrResolve5 ()
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)));
262 //FIXME: What is the 'client' param used for?
263 //TODO: Write test cases for the related overloads of Resolve
264 MetadataResolver.Resolve (
267 MetadataExchangeClientMode.HttpGet,
268 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
272 [ExpectedException (typeof (ArgumentException))]
273 public void ErrResolve6 ()
275 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
276 List<ContractDescription> contracts = new List<ContractDescription> ();
278 //FIXME: What is the 'client' param used for?
279 //TODO: Write test cases for the related overloads of Resolve
280 MetadataResolver.Resolve (
283 MetadataExchangeClientMode.HttpGet,
284 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
288 [ExpectedException (typeof (ArgumentNullException))]
289 public void ErrResolve7 ()
291 MetadataResolver.Resolve (
294 MetadataExchangeClientMode.HttpGet,
295 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
299 [ExpectedException (typeof (ArgumentNullException))]
300 public void ErrResolve8 ()
302 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
303 List<ContractDescription> contracts = new List<ContractDescription> ();
304 contracts.Add (contract);
306 MetadataResolver.Resolve (contracts, null);
309 /* Test for bad endpoint address */
311 [ExpectedException (typeof (InvalidOperationException))]
312 public void ErrResolve9 ()
314 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
315 List<ContractDescription> contracts = new List<ContractDescription> ();
316 contracts.Add (contract);
318 MetadataResolver.Resolve (contracts, new EndpointAddress ("http://localhost"));
321 private void CheckIEchoServiceEndpoint (ServiceEndpointCollection endpoints)
323 Assert.IsNotNull (endpoints);
324 Assert.AreEqual (1, endpoints.Count);
326 ServiceEndpoint ep = endpoints [0];
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");
334 Assert.AreEqual (typeof (BasicHttpBinding), ep.Binding.GetType (), "#R2");
338 [ExpectedException (typeof (InvalidOperationException))]
339 public void ResolveNonContract ()
341 MetadataResolver.Resolve (
342 typeof (Int32), new EndpointAddress (url));
346 [ExpectedException (typeof (InvalidOperationException))]
347 public void ResolveBadUri ()
349 MetadataResolver.Resolve (
350 typeof (IEchoService), new EndpointAddress ("http://localhost"));
360 [ServiceContract (Namespace = "http://myns/echo")]
361 public interface IEchoService
365 string Echo (string msg, int num, dc d);
368 string DoubleIt (int it, string prefix);
373 public class NonExistantContract