ServiceMetadataBehaviorTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // ServiceMetadataBehaviorTest.cs
  3. //
  4. // Author:
  5. // Igor Zelmanovich <[email protected]>
  6. //
  7. // Copyright (C) 2008 Mainsoft, Inc. http://www.mainsoft.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.Text;
  31. using NUnit.Framework;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Description;
  34. using System.ServiceModel.Dispatcher;
  35. using System.ServiceModel.Channels;
  36. using MonoTests.Helpers;
  37. namespace MonoTests.System.ServiceModel.Description
  38. {
  39. [TestFixture]
  40. public class ServiceMetadataBehaviorTest
  41. {
  42. [ServiceContract]
  43. interface IMyContract
  44. {
  45. [OperationContract]
  46. string GetData ();
  47. }
  48. class MyService : IMyContract
  49. {
  50. public string GetData () {
  51. return "Hello World";
  52. }
  53. }
  54. [Test]
  55. public void InitializeRuntime1 () {
  56. var port = NetworkHelpers.FindFreePort ();
  57. using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:" + port))) {
  58. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "e1");
  59. host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true });
  60. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  61. host.Open ();
  62. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  63. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  64. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  65. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  66. Assert.AreEqual (host, cd.Host, "Host");
  67. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  68. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  69. EndpointDispatcher ed = cd.Endpoints [0];
  70. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  71. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  72. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  73. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  74. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  75. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  76. EndpointAddress ea = ed.EndpointAddress;
  77. // TODO
  78. DispatchRuntime dr = ed.DispatchRuntime;
  79. // TODO
  80. host.Close ();
  81. }
  82. }
  83. [Test]
  84. public void InitializeRuntime2 () {
  85. var port = NetworkHelpers.FindFreePort ();
  86. using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:" + port))) {
  87. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  88. host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:" + port + "/mex_and_help") });
  89. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:" + port + "/mex_and_help");
  90. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  91. host.Open ();
  92. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  93. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  94. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  95. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  96. Assert.AreEqual (host, cd.Host, "Host");
  97. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  98. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  99. EndpointDispatcher ed = cd.Endpoints [0];
  100. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  101. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  102. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  103. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  104. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  105. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  106. host.Close ();
  107. }
  108. }
  109. [Test]
  110. public void InitializeRuntime3 () {
  111. var port = NetworkHelpers.FindFreePort ();
  112. using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:" + port))) {
  113. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  114. host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:" + port + "/mex") });
  115. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:" + port + "/help");
  116. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  117. host.Open ();
  118. Assert.AreEqual (3, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  119. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  120. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  121. EndpointDispatcher ed = cd.Endpoints [0];
  122. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter #1");
  123. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher #1");
  124. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter #1");
  125. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName #1");
  126. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace #1");
  127. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority #1");
  128. EndpointAddress ea = ed.EndpointAddress;
  129. // TODO
  130. DispatchRuntime dr = ed.DispatchRuntime;
  131. // TODO
  132. cd = (ChannelDispatcher) host.ChannelDispatchers [2];
  133. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  134. ed = cd.Endpoints [0];
  135. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter #2");
  136. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher #2");
  137. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter #2");
  138. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName #2");
  139. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace #2");
  140. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority #2");
  141. ea = ed.EndpointAddress;
  142. // TODO
  143. dr = ed.DispatchRuntime;
  144. // TODO
  145. host.Close ();
  146. }
  147. }
  148. [Test]
  149. public void InitializeRuntime4 () {
  150. var port = NetworkHelpers.FindFreePort ();
  151. using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:" + port))) {
  152. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  153. host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:" + port + "/mex") });
  154. host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
  155. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  156. host.Open ();
  157. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  158. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  159. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  160. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  161. Assert.AreEqual (host, cd.Host, "Host");
  162. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  163. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  164. Assert.AreEqual (MessageVersion.None, cd.MessageVersion, "MessageVersion");
  165. EndpointDispatcher ed = cd.Endpoints [0];
  166. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  167. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  168. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  169. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  170. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  171. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  172. EndpointAddress ea = ed.EndpointAddress;
  173. Assert.AreEqual (new Uri ("http://localhost:" + port + "/mex"), ea.Uri, "Uri");
  174. DispatchRuntime dr = ed.DispatchRuntime;
  175. Assert.AreEqual (1, dr.Operations.Count, "Operations.Count");
  176. DispatchOperation dispOp = dr.Operations [0];
  177. Assert.AreEqual ("*", dispOp.Action, "Operation.Action");
  178. Assert.AreEqual ("*", dispOp.ReplyAction, "Operation.ReplyAction");
  179. Assert.AreEqual ("Get", dispOp.Name, "Operation.Name");
  180. //Assert.IsNotNull (dispOp.Invoker, "Operation.Invoker");
  181. host.Close ();
  182. }
  183. }
  184. [Test]
  185. public void ServiceMetadataExtension1 () {
  186. var port = NetworkHelpers.FindFreePort ();
  187. using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:" + port))) {
  188. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  189. host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:" + port + "/mex") });
  190. host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
  191. host.Open ();
  192. Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
  193. Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
  194. host.Close ();
  195. }
  196. }
  197. [Test]
  198. public void ServiceMetadataExtension2 () {
  199. var port = NetworkHelpers.FindFreePort ();
  200. using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:" + port))) {
  201. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  202. host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:" + port + "/mex") });
  203. host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
  204. ServiceMetadataExtension extension = new ServiceMetadataExtension ();
  205. host.Extensions.Add (extension);
  206. host.Open ();
  207. Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
  208. Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
  209. Assert.AreEqual (extension, host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #3");
  210. host.Close ();
  211. }
  212. }
  213. [Test]
  214. public void Defaults () {
  215. ServiceMetadataBehavior behavior = new ServiceMetadataBehavior ();
  216. Assert.IsNull (behavior.ExternalMetadataLocation, "ExternalMetadataLocation");
  217. Assert.AreEqual (false, behavior.HttpGetEnabled, "HttpGetEnabled");
  218. Assert.IsNull (behavior.HttpGetUrl, "HttpGetUrl");
  219. Assert.AreEqual (false, behavior.HttpsGetEnabled, "HttpsGetEnabled");
  220. Assert.IsNull (behavior.HttpsGetUrl, "HttpsGetUrl");
  221. Assert.IsNotNull (behavior.MetadataExporter, "MetadataExporter #1");
  222. Assert.AreEqual (typeof (WsdlExporter), behavior.MetadataExporter.GetType (), "MetadataExporter #2");
  223. Assert.AreEqual ("IMetadataExchange", ServiceMetadataBehavior.MexContractName, "MexContractName");
  224. }
  225. }
  226. }