ServiceDiscoveryBehaviorTest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Author: Atsushi Enomoto <[email protected]>
  3. //
  4. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Collections.ObjectModel;
  28. using System.ServiceModel;
  29. using System.ServiceModel.Channels;
  30. using System.ServiceModel.Description;
  31. using System.ServiceModel.Discovery;
  32. using System.ServiceModel.Dispatcher;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.ServiceModel.Discovery
  35. {
  36. [TestFixture]
  37. public class ServiceDiscoveryBehaviorTest
  38. {
  39. [Test]
  40. public void Use ()
  41. {
  42. var b = new ServiceDiscoveryBehavior ();
  43. b.AnnouncementEndpoints.Add (new UdpAnnouncementEndpoint ());
  44. IServiceBehavior sb = b;
  45. var host = new ServiceHost (new Uri ("http://localhost:37564"));
  46. var bc = new BindingParameterCollection ();
  47. sb.AddBindingParameters (host.Description, host, host.Description.Endpoints, bc);
  48. Assert.AreEqual (0, bc.Count, "#1");
  49. Assert.AreEqual (0, host.Extensions.Count, "#2-1");
  50. sb.Validate (host.Description, host);
  51. // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
  52. Assert.AreEqual (1, host.Extensions.Count, "#2-2");
  53. var dse = host.Extensions.Find<DiscoveryServiceExtension> ();
  54. Assert.IsNotNull (dse, "#2-3");
  55. Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#2-4");
  56. Assert.AreEqual (0, host.ChannelDispatchers.Count, "#3-1");
  57. sb.ApplyDispatchBehavior (host.Description, host);
  58. Assert.AreEqual (0, host.Description.Endpoints.Count, "#3-2");
  59. Assert.AreEqual (2, host.ChannelDispatchers.Count, "#3-3"); // for online and offline announcements
  60. Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-4"); // discovery endpoints are not "published"
  61. int idx = 0;
  62. foreach (var cdisb in host.ChannelDispatchers) {
  63. var cdis = cdisb as ChannelDispatcher;
  64. string head = "#4." + idx + ".";
  65. Assert.IsNull (cdis, head + "dispatcher");
  66. if (cdisb.Listener != null)
  67. Assert.AreEqual ("urn:schemas-microsoft-org:ws:2008:07:discovery", cdisb.Listener.Uri.ToString (), head + "uri");
  68. // else ... WHOA! .NET "OnlineAnnouncementChannelDispatcher" type does not seem to provide the listener.
  69. idx++;
  70. }
  71. }
  72. [Test]
  73. public void Use2 ()
  74. {
  75. var b = new ServiceDiscoveryBehavior ();
  76. b.AnnouncementEndpoints.Add (new UdpAnnouncementEndpoint ());
  77. IServiceBehavior sb = b;
  78. var host = new ServiceHost (typeof (TestService));
  79. var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
  80. var bc = new BindingParameterCollection ();
  81. sb.AddBindingParameters (host.Description, host, host.Description.Endpoints, bc);
  82. Assert.AreEqual (0, bc.Count, "#1");
  83. Assert.AreEqual (0, host.Extensions.Count, "#1-2");
  84. Assert.AreEqual (0, se.Behaviors.Count, "#1-3");
  85. sb.Validate (host.Description, host);
  86. // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
  87. Assert.AreEqual (1, host.Extensions.Count, "#2-2");
  88. var dse = host.Extensions.Find<DiscoveryServiceExtension> ();
  89. Assert.IsNotNull (dse, "#2-3");
  90. Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#2-4");
  91. Assert.AreEqual (1, se.Behaviors.Count, "#2-5"); // Validate() adds endpoint initialization behavior.
  92. Assert.AreEqual (0, host.ChannelDispatchers.Count, "#3-1");
  93. sb.ApplyDispatchBehavior (host.Description, host);
  94. Assert.AreEqual (1, host.Description.Endpoints.Count, "#3-2");
  95. Assert.AreEqual (2, host.ChannelDispatchers.Count, "#3-3"); // for online and offline announcements
  96. Assert.AreEqual (0, dse.PublishedEndpoints.Count, "#3-4"); // discovery endpoints are not "published"
  97. Assert.AreEqual (1, se.Behaviors.Count, "#3-5");
  98. // The IEndpointBehavior from ServiceDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint.
  99. se.Behaviors [0].ApplyDispatchBehavior (se, new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "ITestService", "http://tempuri.org/"));
  100. Assert.AreEqual (2, host.ChannelDispatchers.Count, "#3-6-1"); // for online and offline announcements
  101. Assert.AreEqual (1, dse.PublishedEndpoints.Count, "#3-6-2"); // The endpoint is newly published.
  102. host.Open ();
  103. try {
  104. Assert.AreEqual (3, host.ChannelDispatchers.Count, "#4-1"); // for online and offline announcements
  105. Assert.AreEqual (2, dse.PublishedEndpoints.Count, "#4-2"); // The endpoint is published again. (Not sure if it's worthy of testing.)
  106. } finally {
  107. host.Close ();
  108. }
  109. }
  110. [Test]
  111. public void UseHttpBinding ()
  112. {
  113. var ahost = new ServiceHost (typeof (AnnouncementService));
  114. var aendpoint = new AnnouncementEndpoint (new CustomBinding (new HttpTransportBindingElement ()), new EndpointAddress ("http://localhost:4989"));
  115. var ib = new InspectionBehavior ();
  116. object state = new object ();
  117. ib.RequestReceived += delegate {
  118. InspectionBehavior.State = state;
  119. return null;
  120. };
  121. aendpoint.Behaviors.Add (ib);
  122. ahost.AddServiceEndpoint (aendpoint);
  123. ahost.Open ();
  124. try {
  125. Assert.IsTrue (ib.DispatchBehaviorApplied, "#1");
  126. var b = new ServiceDiscoveryBehavior ();
  127. b.AnnouncementEndpoints.Add (new AnnouncementEndpoint (new CustomBinding (new HttpTransportBindingElement ()), new EndpointAddress ("http://localhost:4989")));
  128. IServiceBehavior sb = b;
  129. var host = new ServiceHost (typeof (TestService));
  130. var se = host.AddServiceEndpoint (typeof (ITestService), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
  131. var bc = new BindingParameterCollection ();
  132. sb.AddBindingParameters (host.Description, host, host.Description.Endpoints, bc);
  133. sb.Validate (host.Description, host);
  134. // ... should "validate" not "apply dispatch behavior" do "add host extension" job? I doubt that.
  135. var dse = host.Extensions.Find<DiscoveryServiceExtension> ();
  136. Assert.IsNotNull (dse, "#2");
  137. sb.ApplyDispatchBehavior (host.Description, host);
  138. // The IEndpointBehavior from ServiceDiscoveryBehavior, when ApplyDispatchBehavior() is invoked, publishes an endpoint.
  139. se.Behaviors [0].ApplyDispatchBehavior (se, new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "ITestService", "http://tempuri.org/"));
  140. host.Open ();
  141. try {
  142. Assert.AreEqual (state, InspectionBehavior.State, "#3");
  143. } finally {
  144. host.Close ();
  145. }
  146. } finally {
  147. ahost.Close ();
  148. }
  149. }
  150. }
  151. }