TestFixtureBase.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.ServiceModel.Description;
  7. using NUnit.Framework;
  8. using System.Reflection;
  9. using System.Threading;
  10. using System.Configuration;
  11. using System.IO;
  12. using System.Net;
  13. using MonoTests.stand_alone.WebHarness;
  14. using System.ServiceModel.Dispatcher;
  15. using System.Collections.ObjectModel;
  16. namespace MonoTests.Features
  17. {
  18. public class Configuration
  19. {
  20. static Configuration() {
  21. onlyServers = Boolean.Parse (ConfigurationManager.AppSettings ["onlyServers"] ?? "false");
  22. onlyClients = Boolean.Parse (ConfigurationManager.AppSettings ["onlyClients"] ?? "false");
  23. endpointBase = ConfigurationManager.AppSettings ["endpointBase"] ?? "http://localhost:9999/";
  24. if (!endpointBase.EndsWith ("/"))
  25. endpointBase = endpointBase + '/';
  26. logMessages = Boolean.Parse (ConfigurationManager.AppSettings ["logMessages"] ?? "false");
  27. }
  28. public static bool onlyServers;
  29. public static bool onlyClients;
  30. public static string endpointBase;
  31. public static bool logMessages;
  32. public static bool IsLocal { get { return !onlyServers && !onlyClients; } }
  33. }
  34. class LoggerMessageInspector : IDispatchMessageInspector
  35. {
  36. #region IDispatchMessageInspector Members
  37. public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
  38. Console.WriteLine ("****Begin message received by host:");
  39. Console.WriteLine (request);
  40. Console.WriteLine ("****End message received by host:");
  41. return new object ();
  42. }
  43. public void BeforeSendReply (ref Message reply, object correlationState) {
  44. Console.WriteLine ("****Begin message reply from host:");
  45. Console.WriteLine (reply);
  46. Console.WriteLine ("****End message reply from host:");
  47. }
  48. #endregion
  49. }
  50. class LoggerBehavior : IServiceBehavior
  51. {
  52. #region IServiceBehavior Members
  53. public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
  54. }
  55. public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  56. ChannelDispatcher dispatcher = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
  57. dispatcher.Endpoints [0].DispatchRuntime.MessageInspectors.Add (new LoggerMessageInspector ());
  58. }
  59. public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  60. }
  61. #endregion
  62. }
  63. public abstract class TestFixtureBase<TClient, TServer, IServer> where TClient : new() where TServer: new()
  64. {
  65. ServiceHost _hostBase;
  66. ChannelFactory<IServer> factory;
  67. protected TestFixtureBase () { }
  68. [TearDown]
  69. public void TearDown ()
  70. {
  71. if (_hostBase != null)
  72. _hostBase.Close ();
  73. if (factory != null)
  74. factory.Close ();
  75. }
  76. [SetUp]
  77. public virtual void Run (){
  78. bool runServer = true;
  79. bool runClient = true;
  80. if (Configuration.onlyClients)
  81. runServer = false;
  82. if (Configuration.onlyServers)
  83. runClient = false;
  84. Run (runServer, runClient);
  85. }
  86. public void CheckWsdlImpl () {
  87. string goldWsdl;
  88. try {
  89. Assembly _assembly = Assembly.GetExecutingAssembly ();
  90. StreamReader _stream = new StreamReader (_assembly.GetManifestResourceStream ("MonoTests.System.ServiceModel.Test.FeatureBased.Features.Contracts." + typeof (TServer).Name + ".xml"));
  91. goldWsdl = _stream.ReadToEnd ();
  92. }
  93. catch {
  94. Console.WriteLine ("Couldn't test WSDL of server " + typeof (TServer).Name + " because gold wsdl is not embedded in test !");
  95. return;
  96. }
  97. string currentWsdl = "";
  98. HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create (getMexEndpoint () + "?wsdl");
  99. // Obtain a 'Stream' object associated with the response object.
  100. WebResponse response = myReq.GetResponse ();
  101. Stream ReceiveStream = response.GetResponseStream ();
  102. Encoding encode = global::System.Text.Encoding.GetEncoding ("utf-8");
  103. // Pipe the stream to a higher level stream reader with the required encoding format.
  104. StreamReader readStream = new StreamReader (ReceiveStream, encode);
  105. Console.WriteLine ("\nResponse stream received");
  106. int maxLen = 10 * 1024;
  107. Char [] read = new Char [maxLen];
  108. // Read 256 charcters at a time.
  109. int count = readStream.Read (read, 0, maxLen);
  110. while (count > 0) {
  111. currentWsdl = currentWsdl + new String (read, 0, count);
  112. count = readStream.Read (read, 0, 256);
  113. }
  114. readStream.Close ();
  115. response.Close ();
  116. XmlComparer comparer = new XmlComparer (XmlComparer.Flags.IgnoreAttribOrder, true);
  117. Assert.IsTrue (comparer.AreEqual (goldWsdl, currentWsdl), "Service WSDL does not match gold WSDL");
  118. }
  119. protected void Run (bool runServer, bool runClient) {
  120. if (runServer) {
  121. _hostBase = InitializeServiceHost ();
  122. _hostBase.Open ();
  123. }
  124. }
  125. string getEndpoint()
  126. {
  127. return Configuration.endpointBase + typeof(TServer).Name;
  128. }
  129. public string getMexEndpoint ()
  130. {
  131. return getEndpoint () + "_wsdl"; // should be getEndpoint() but currently implementation is buggy
  132. }
  133. TClient _client;
  134. protected virtual TClient InitializeClient () {
  135. //return new TClient(new BasicHttpBinding(), new EndpointAddress( getEndpoint) );
  136. Type [] paramsTypes = new Type [] { typeof (Binding), typeof (EndpointAddress) };
  137. object [] parameters = new object [] { new BasicHttpBinding (), new EndpointAddress (getEndpoint())};
  138. ConstructorInfo info = typeof (TClient).GetConstructor (paramsTypes);
  139. return (TClient) info.Invoke (parameters);
  140. }
  141. public TClient ClientProxy {
  142. get {
  143. if (_client == null)
  144. _client = InitializeClient ();
  145. return _client;
  146. }
  147. }
  148. public IServer Client {
  149. get {
  150. factory = new ChannelFactory<IServer> (new BasicHttpBinding (), new EndpointAddress (getEndpoint ()));
  151. return factory.CreateChannel ();
  152. }
  153. }
  154. protected virtual ServiceHost InitializeServiceHost () {
  155. ServiceHost host = new ServiceHost(typeof(TServer));
  156. host.AddServiceEndpoint(typeof(IServer), new BasicHttpBinding(), getEndpoint());
  157. ServiceMetadataBehavior smb = new ServiceMetadataBehavior ();
  158. smb.HttpGetEnabled = true;
  159. smb.HttpGetUrl = new Uri (getMexEndpoint ());
  160. host.Description.Behaviors.Add (smb);
  161. host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentCalls = 1, MaxConcurrentSessions = 1 });
  162. if (Configuration.logMessages)
  163. host.Description.Behaviors.Add (new LoggerBehavior ());
  164. return host;
  165. }
  166. protected ServiceHost Host {
  167. get {
  168. return _hostBase;
  169. }
  170. }
  171. [TearDown]
  172. protected virtual void Close () {
  173. if (!Configuration.onlyClients && !Configuration.onlyServers && Host.State == CommunicationState.Opened)
  174. Host.Close ();
  175. }
  176. }
  177. }