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 namespace MonoTests.System.ServiceModel.Description
44 public class MetadataResolverTest
46 string url = "http://localhost:37564/echo/mex";
47 //string url = "http://192.168.0.1:8080/echo/mex";
49 static HttpListener listener;
50 IAsyncResult current_request;
53 static readonly string mex = File.ReadAllText ("Test/System.ServiceModel.Description/dump.xml");
56 public void StartupServer ()
60 listener = new HttpListener ();
61 listener.Prefixes.Add ("http://*:37564/echo/");
63 current_request = listener.BeginGetContext (OnReceivedRequest, null);
67 void OnReceivedRequest (IAsyncResult result)
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))
76 ctx.Response.Close ();
78 current_request = listener.BeginGetContext (OnReceivedRequest, null);
79 } catch (Exception ex) {
80 // ignore server errors in this test.
85 public void ShutdownServer ()
92 [Category ("NotWorking")]
93 public void ResolveNoEndpoint ()
95 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
96 typeof (NonExistantContract),
97 new EndpointAddress (url));
99 Assert.IsNotNull (endpoints);
100 Assert.AreEqual (0, endpoints.Count);
104 [Category ("NotWorking")]
105 public void Resolve1 ()
107 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
108 typeof (IEchoService), new EndpointAddress (url));
110 CheckIEchoServiceEndpoint (endpoints);
114 [Category ("NotWorking")]
115 public void Resolve2 ()
117 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
118 typeof (IEchoService),
120 MetadataExchangeClientMode.MetadataExchange);
122 CheckIEchoServiceEndpoint (endpoints);
126 [Category ("NotWorking")]
127 public void Resolve3 ()
129 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
130 List<ContractDescription> contracts = new List<ContractDescription> ();
131 contracts.Add (contract);
133 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
136 MetadataExchangeClientMode.MetadataExchange);
138 CheckIEchoServiceEndpoint (endpoints);
142 [Category ("NotWorking")]
143 public void Resolve4 ()
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)));
150 ServiceEndpointCollection endpoints = MetadataResolver.Resolve (
153 MetadataExchangeClientMode.MetadataExchange);
155 CheckIEchoServiceEndpoint (endpoints);
159 [Category ("NotWorking")]
160 public void Resolve5 ()
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)));
167 //FIXME: What is the 'client' param used for?
168 //TODO: Write test cases for the related overloads of Resolve
169 MetadataResolver.Resolve (
171 new EndpointAddress (url),
172 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
176 [Category ("NotWorking")]
177 public void Resolve6 ()
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)));
184 //FIXME: What is the 'client' param used for?
185 //TODO: Write test cases for the related overloads of Resolve
186 MetadataResolver.Resolve (
189 MetadataExchangeClientMode.MetadataExchange,
190 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
197 [ExpectedException (typeof (ArgumentNullException))]
198 public void ErrResolve1 ()
200 MetadataResolver.Resolve (
201 typeof (IEchoService),
203 MetadataExchangeClientMode.MetadataExchange);
207 [ExpectedException (typeof (InvalidOperationException))]
208 [Ignore ("does not fail on .NET either")]
209 public void ErrResolve2 ()
211 //Mex cannot be fetched with HttpGet from the given url
212 MetadataResolver.Resolve (
213 typeof (IEchoService),
215 MetadataExchangeClientMode.HttpGet);
219 [ExpectedException (typeof (ArgumentNullException))]
220 public void ErrResolve3 ()
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)));
227 MetadataResolver.Resolve (contracts, new EndpointAddress (url),
228 (MetadataExchangeClient) null);
232 [ExpectedException (typeof (InvalidOperationException))]
233 public void ErrResolve4 ()
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)));
240 //FIXME: What is the 'client' param used for?
241 //TODO: Write test cases for the related overloads of Resolve
242 MetadataResolver.Resolve (
244 new EndpointAddress ("http://localhost"),
245 new MetadataExchangeClient (new EndpointAddress (url)));
249 [ExpectedException (typeof (InvalidOperationException))]
250 [Ignore ("does not fail on .NET either")]
251 public void ErrResolve5 ()
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)));
258 //FIXME: What is the 'client' param used for?
259 //TODO: Write test cases for the related overloads of Resolve
260 MetadataResolver.Resolve (
263 MetadataExchangeClientMode.HttpGet,
264 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
268 [ExpectedException (typeof (ArgumentException))]
269 public void ErrResolve6 ()
271 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
272 List<ContractDescription> contracts = new List<ContractDescription> ();
274 //FIXME: What is the 'client' param used for?
275 //TODO: Write test cases for the related overloads of Resolve
276 MetadataResolver.Resolve (
279 MetadataExchangeClientMode.HttpGet,
280 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
284 [ExpectedException (typeof (ArgumentNullException))]
285 public void ErrResolve7 ()
287 MetadataResolver.Resolve (
290 MetadataExchangeClientMode.HttpGet,
291 new MetadataExchangeClient (new EndpointAddress ("http://localhost")));
295 [ExpectedException (typeof (ArgumentNullException))]
296 public void ErrResolve8 ()
298 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
299 List<ContractDescription> contracts = new List<ContractDescription> ();
300 contracts.Add (contract);
302 MetadataResolver.Resolve (contracts, null);
305 /* Test for bad endpoint address */
307 [ExpectedException (typeof (InvalidOperationException))]
308 public void ErrResolve9 ()
310 ContractDescription contract = ContractDescription.GetContract (typeof (IEchoService));
311 List<ContractDescription> contracts = new List<ContractDescription> ();
312 contracts.Add (contract);
314 MetadataResolver.Resolve (contracts, new EndpointAddress ("http://localhost"));
317 private void CheckIEchoServiceEndpoint (ServiceEndpointCollection endpoints)
319 Assert.IsNotNull (endpoints);
320 Assert.AreEqual (1, endpoints.Count);
322 ServiceEndpoint ep = endpoints [0];
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");
330 Assert.AreEqual (typeof (BasicHttpBinding), ep.Binding.GetType (), "#R2");
334 [ExpectedException (typeof (InvalidOperationException))]
335 public void ResolveNonContract ()
337 MetadataResolver.Resolve (
338 typeof (Int32), new EndpointAddress (url));
342 [ExpectedException (typeof (InvalidOperationException))]
343 public void ResolveBadUri ()
345 MetadataResolver.Resolve (
346 typeof (IEchoService), new EndpointAddress ("http://localhost"));
356 [ServiceContract (Namespace = "http://myns/echo")]
357 public interface IEchoService
361 string Echo (string msg, int num, dc d);
364 string DoubleIt (int it, string prefix);
369 public class NonExistantContract