ServiceDebugBehaviorTest.cs 9.0 KB

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