ServiceDebugBehaviorTest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. try {
  68. host.Open ();
  69. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  70. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  71. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  72. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  73. Assert.AreEqual (host, cd.Host, "Host");
  74. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  75. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  76. EndpointDispatcher ed = cd.Endpoints [0];
  77. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  78. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  79. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  80. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  81. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  82. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  83. EndpointAddress ea = ed.EndpointAddress;
  84. // TODO
  85. DispatchRuntime dr = ed.DispatchRuntime;
  86. // TODO
  87. } finally {
  88. host.Close ();
  89. }
  90. }
  91. }
  92. [Test]
  93. public void InitializeRuntime2 () {
  94. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  95. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  96. host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
  97. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  98. try {
  99. host.Open ();
  100. Assert.AreEqual (1, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  101. } finally {
  102. host.Close ();
  103. }
  104. }
  105. }
  106. [Test]
  107. public void InitializeRuntime3 () {
  108. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  109. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  110. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled = false;
  111. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  112. try {
  113. host.Open ();
  114. Assert.AreEqual (1, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  115. } finally {
  116. host.Close ();
  117. }
  118. }
  119. }
  120. [Test]
  121. public void InitializeRuntime4 () {
  122. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  123. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  124. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:37564/help");
  125. Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
  126. try {
  127. host.Open ();
  128. Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
  129. ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
  130. Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
  131. Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
  132. Assert.AreEqual (host, cd.Host, "Host");
  133. //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
  134. //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
  135. Assert.AreEqual (MessageVersion.None, cd.MessageVersion, "MessageVersion");
  136. EndpointDispatcher ed = cd.Endpoints [0];
  137. Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
  138. Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
  139. Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
  140. Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
  141. Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
  142. Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
  143. EndpointAddress ea = ed.EndpointAddress;
  144. Assert.AreEqual (new Uri ("http://localhost:37564/help"), ea.Uri, "Uri");
  145. DispatchRuntime dr = ed.DispatchRuntime;
  146. Assert.AreEqual (1, dr.Operations.Count, "Operations.Count");
  147. DispatchOperation dispOp = dr.Operations [0];
  148. Assert.AreEqual ("*", dispOp.Action, "Operation.Action");
  149. Assert.AreEqual ("*", dispOp.ReplyAction, "Operation.ReplyAction");
  150. Assert.AreEqual ("Get", dispOp.Name, "Operation.Name");
  151. //Assert.IsNotNull (dispOp.Invoker, "Operation.Invoker");
  152. } finally {
  153. host.Close ();
  154. }
  155. }
  156. }
  157. [Test]
  158. public void ServiceMetadataExtension1 () {
  159. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  160. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  161. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:37564/help");
  162. try {
  163. host.Open ();
  164. Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
  165. Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
  166. } finally {
  167. host.Close ();
  168. }
  169. }
  170. }
  171. [Test]
  172. public void ServiceMetadataExtension2 () {
  173. using (ServiceHost host = new ServiceHost (typeof (MyService), CreateUri ("http://localhost:37564"))) {
  174. host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
  175. host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = CreateUri ("http://localhost:37564/help");
  176. ServiceMetadataExtension extension = new ServiceMetadataExtension ();
  177. host.Extensions.Add (extension);
  178. try {
  179. host.Open ();
  180. Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
  181. Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
  182. Assert.AreEqual (extension, host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #3");
  183. } finally {
  184. host.Close ();
  185. }
  186. }
  187. }
  188. [Test]
  189. public void Defaults () {
  190. ServiceDebugBehavior behavior = new ServiceDebugBehavior ();
  191. Assert.AreEqual (true, behavior.HttpHelpPageEnabled, "HttpHelpPageEnabled");
  192. Assert.IsNull (behavior.HttpHelpPageUrl, "HttpHelpPageUrl");
  193. Assert.AreEqual (true, behavior.HttpsHelpPageEnabled, "HttpsHelpPageEnabled");
  194. Assert.IsNull (behavior.HttpsHelpPageUrl, "HttpsHelpPageUrl");
  195. Assert.AreEqual (false, behavior.IncludeExceptionDetailInFaults, "IncludeExceptionDetailInFaults");
  196. }
  197. }
  198. }