MetadataResolverTest.cs 11 KB

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