File: TestFixtureBase.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (218 lines) | stat: -rw-r--r-- 7,007 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#if !MOBILE && !XAMMAC_4_5
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using NUnit.Framework;
using System.Reflection;
using System.Threading;
using System.Configuration;
using System.IO;
using System.Net;
using MonoTests.stand_alone.WebHarness;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;

using MonoTests.Helpers;

namespace MonoTests.Features
{
	public class Configuration
	{
		static Configuration() {
			var port = NetworkHelpers.FindFreePort ();
			onlyServers = Boolean.Parse (ConfigurationManager.AppSettings ["onlyServers"]  ?? "false");
			onlyClients = Boolean.Parse (ConfigurationManager.AppSettings ["onlyClients"]  ?? "false");
			endpointBase = ConfigurationManager.AppSettings ["endpointBase"] ?? $"http://localhost:{port}/";
			if (!endpointBase.EndsWith ("/"))
				endpointBase = endpointBase + '/';
			logMessages = Boolean.Parse (ConfigurationManager.AppSettings ["logMessages"] ?? "false");
		}
		public static bool onlyServers;
		public static bool onlyClients;
		public static string endpointBase;
		public static bool logMessages;
		public static bool IsLocal { get { return !onlyServers && !onlyClients; } }
	}

	class LoggerMessageInspector : IDispatchMessageInspector
	{
		#region IDispatchMessageInspector Members

		public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
			Console.WriteLine ("****Begin message received by host:");
			Console.WriteLine (request);
			Console.WriteLine ("****End message received by host:");
			return new object ();
		}

		public void BeforeSendReply (ref Message reply, object correlationState) {
			Console.WriteLine ("****Begin message reply from host:");
			Console.WriteLine (reply);
			Console.WriteLine ("****End message reply from host:");
		}

		#endregion
	}
	class LoggerBehavior : IServiceBehavior
	{

		#region IServiceBehavior Members

		public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {

		}

		public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
			ChannelDispatcher dispatcher = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
			dispatcher.Endpoints [0].DispatchRuntime.MessageInspectors.Add (new LoggerMessageInspector ());
		}

		public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {

		}

		#endregion
	}

	public abstract class TestFixtureBase<TClient, TServer, IServer> where TClient : new() where TServer: new()
	{
		ServiceHost _hostBase;
		ChannelFactory<IServer> factory;

		protected TestFixtureBase () { }		

		[TearDown]
		public void TearDown ()
		{
			if (_hostBase != null)
				_hostBase.Close ();
			if (factory != null)
				factory.Close ();
		}

		[SetUp]
		public virtual void Run (){
			bool runServer = true;
			bool runClient = true;
			if (Configuration.onlyClients)
				runServer = false;
			if (Configuration.onlyServers)
				runClient = false;
			Run (runServer, runClient);			
		}

		public void CheckWsdlImpl () {
			string goldWsdl;
			try {
				Assembly _assembly = Assembly.GetExecutingAssembly ();
				StreamReader _stream = new StreamReader (_assembly.GetManifestResourceStream ("MonoTests.System.ServiceModel.Test.FeatureBased.Features.Contracts." + typeof (TServer).Name + ".xml"));
				goldWsdl = _stream.ReadToEnd ();
			}
			catch {
				Console.WriteLine ("Couldn't test WSDL of server " + typeof (TServer).Name + " because gold wsdl is not embedded in test !");
				return;
			}
			string currentWsdl = "";

			HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create (getMexEndpoint () + "?wsdl");
			// Obtain a 'Stream' object associated with the response object.
			WebResponse response = myReq.GetResponse ();
			Stream ReceiveStream = response.GetResponseStream ();

			Encoding encode = global::System.Text.Encoding.GetEncoding ("utf-8");

			// Pipe the stream to a higher level stream reader with the required encoding format. 
			StreamReader readStream = new StreamReader (ReceiveStream, encode);
			Console.WriteLine ("\nResponse stream received");
			int maxLen = 10 * 1024;
			Char [] read = new Char [maxLen];

			// Read 256 charcters at a time.    
			int count = readStream.Read (read, 0, maxLen);
			while (count > 0) {
				currentWsdl = currentWsdl + new String (read, 0, count);
				count = readStream.Read (read, 0, 256);
			}
			readStream.Close ();
			response.Close ();

			XmlComparer comparer = new XmlComparer (XmlComparer.Flags.IgnoreAttribOrder, true);
			Assert.IsTrue (comparer.AreEqual (goldWsdl, currentWsdl), "Service WSDL does not match gold WSDL");

		}

		protected void Run (bool runServer, bool runClient) {

			if (runServer) {
				_hostBase = InitializeServiceHost ();
				_hostBase.Open ();
			}

		}

        string getEndpoint()
        {
			return Configuration.endpointBase + typeof(TServer).Name;
        }

		public string getMexEndpoint () 
		{
			return getEndpoint () + "_wsdl"; // should be getEndpoint() but currently implementation is buggy
		}

		TClient _client;
		protected virtual TClient InitializeClient () {
			//return new TClient(new BasicHttpBinding(), new EndpointAddress( getEndpoint) );
			Type [] paramsTypes = new Type [] { typeof (Binding), typeof (EndpointAddress) };
			object [] parameters = new object [] { new BasicHttpBinding (), new EndpointAddress (getEndpoint())};

			ConstructorInfo info = typeof (TClient).GetConstructor (paramsTypes);
			return (TClient) info.Invoke (parameters);
		}

		public TClient ClientProxy {
			get {
				if (_client == null)
					_client = InitializeClient ();			
				return _client;
			}			
		}

		public IServer Client {
			get {
				factory = new ChannelFactory<IServer> (new BasicHttpBinding (), new EndpointAddress (getEndpoint ()));
				return factory.CreateChannel ();
			}
		}

		protected virtual ServiceHost InitializeServiceHost () {
            ServiceHost host = new ServiceHost(typeof(TServer));
            host.AddServiceEndpoint(typeof(IServer), new BasicHttpBinding(), getEndpoint());
			ServiceMetadataBehavior smb = new ServiceMetadataBehavior ();
			smb.HttpGetEnabled = true;
			smb.HttpGetUrl = new Uri (getMexEndpoint ());
			host.Description.Behaviors.Add (smb);
			host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentCalls = 1, MaxConcurrentSessions = 1 });
			if (Configuration.logMessages)
				host.Description.Behaviors.Add (new LoggerBehavior ());
            return host;
		}


		protected ServiceHost Host {
			get {
				return _hostBase;
			}
		}

		[TearDown]
		protected virtual void Close () {
			if (!Configuration.onlyClients && !Configuration.onlyServers &&  Host.State == CommunicationState.Opened)
				Host.Close ();
		}
	}
}
#endif