ServiceDebugBehaviorTest.cs 9.1 KB

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