MetadataResolverTest.cs 11 KB

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