ServiceDebugBehaviorTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // ServiceDebugBehaviorTest.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.Net.Sockets;
  31. using System.Text;
  32. using NUnit.Framework;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Description;
  35. using System.ServiceModel.Dispatcher;
  36. using System.ServiceModel.Channels;
  37. namespace MonoTests.System.ServiceModel.Description
  38. {
  39. [TestFixture]
  40. public class ServiceDebugBehaviorTest
  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. Uri CreateUri (string uriString)
  55. {
  56. var uri = new Uri (uriString);
  57. var l = new TcpListener (uri.Port);
  58. l.Start ();
  59. l.Stop ();
  60. return uri;
  61. }
  62. [Test]
  63. public void InitializeRuntime1 () {
  64. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  65. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "e1");
  66. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  67. host.Open ();
  68. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  69. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  70. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  71. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  72. Assert.AreEqual (host, cd.Host, "Host");
  73. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  74. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  75. EndpointDispatcher ed = cd.Endpoints [0];
  76. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  77. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  78. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  79. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  80. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  81. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  82. EndpointAddress ea = ed.EndpointAddress;
  83. // TODO
  84. DispatchRuntime dr = ed.DispatchRuntime;
  85. // TODO
  86. host.Close ();
  87. }
  88. }
  89. [Test]
  90. public void InitializeRuntime2 () {
  91. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  92. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  93. host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
  94. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  95. host.Open ();
  96. Assert.AreEqual (1, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  97. host.Close ();
  98. }
  99. }
  100. [Test]
  101. public void InitializeRuntime3 () {
  102. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  103. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  104. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled = false;
  105. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  106. host.Open ();
  107. Assert.AreEqual (1, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  108. host.Close ();
  109. }
  110. }
  111. [Test]
  112. public void InitializeRuntime4 () {
  113. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  114. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  115. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:37564/help");
  116. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  117. host.Open ();
  118. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  119. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  120. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  121. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  122. Assert.AreEqual (host, cd.Host, "Host");
  123. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  124. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  125. Assert.AreEqual (MessageVersion.None, cd.MessageVersion, "MessageVersion");
  126. EndpointDispatcher ed = cd.Endpoints [0];
  127. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  128. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  129. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  130. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  131. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  132. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  133. EndpointAddress ea = ed.EndpointAddress;
  134. Assert.AreEqual (new Uri ("http://localhost:37564/help"), ea.Uri, "Uri");
  135. DispatchRuntime dr = ed.DispatchRuntime;
  136. Assert.AreEqual (1, dr.Operations.Count, "Operations.Count");
  137. DispatchOperation dispOp = dr.Operations [0];
  138. Assert.AreEqual ("*", dispOp.Action, "Operation.Action");
  139. Assert.AreEqual ("*", dispOp.ReplyAction, "Operation.ReplyAction");
  140. Assert.AreEqual ("Get", dispOp.Name, "Operation.Name");
  141. //Assert.IsNotNull (dispOp.Invoker, "Operation.Invoker");
  142. host.Close ();
  143. }
  144. }
  145. [Test]
  146. public void ServiceMetadataExtension1 () {
  147. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  148. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  149. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:37564/help");
  150. host.Open ();
  151. Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
  152. Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
  153. host.Close ();
  154. }
  155. }
  156. [Test]
  157. public void ServiceMetadataExtension2 () {
  158. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  159. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  160. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = CreateUri ("http://localhost:37564/help");
  161. ServiceMetadataExtension extension = new ServiceMetadataExtension ();
  162. host.Extensions.Add (extension);
  163. host.Open ();
  164. Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
  165. Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
  166. Assert.AreEqual (extension, host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #3");
  167. host.Close ();
  168. }
  169. }
  170. [Test]
  171. public void Defaults () {
  172. ServiceDebugBehavior behavior = new ServiceDebugBehavior ();
  173. Assert.AreEqual (true, behavior.HttpHelpPageEnabled, "HttpHelpPageEnabled");
  174. Assert.IsNull (behavior.HttpHelpPageUrl, "HttpHelpPageUrl");
  175. Assert.AreEqual (true, behavior.HttpsHelpPageEnabled, "HttpsHelpPageEnabled");
  176. Assert.IsNull (behavior.HttpsHelpPageUrl, "HttpsHelpPageUrl");
  177. Assert.AreEqual (false, behavior.IncludeExceptionDetailInFaults, "IncludeExceptionDetailInFaults");
  178. }
  179. }
  180. }