2
0

ServiceMetadataBehaviorTest.cs 12 KB

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