ServiceDebugBehaviorTest.cs 8.7 KB

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