57e03d8e7b4fa9e487ea5c0c18e84c479ded4e0d
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / ServiceHostTest.cs
1 //
2 // ServiceHostTest.cs
3 //
4 // Author:
5 //      Ankit Jain  <jankit@novell.com>
6 //      Atsushi Enomoto  <atsushi@ximian.com>
7 //
8 // Copyright (C) 2005-2006 Novell, Inc.  http://www.novell.com
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Collections.Generic;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.Dispatcher;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.ServiceModel
38 {
39         [TestFixture]
40         public class ServiceHostTest
41         {
42                 class MyHost : ServiceHost
43                 {
44                         public MyHost (Type type, Uri uri)
45                                 : base (type, uri)
46                         {
47                         }
48
49                         public IDictionary<string,ContractDescription> ExposedContracts {
50                                 get { return ImplementedContracts; }
51                         }
52                 }
53
54                 [Test]
55                 public void Ctor ()
56                 {
57                         MyHost host = new MyHost (typeof (Foo), new Uri ("http://localhost"));
58                         Assert.IsNotNull (host.Description, "#1");
59                         Assert.AreEqual (typeof (Foo), host.Description.ServiceType, "#1-2");
60                         Assert.IsNotNull (host.BaseAddresses, "#2");
61                         Assert.AreEqual (1, host.BaseAddresses.Count, "#3");
62
63                         Assert.IsNotNull (host.ChannelDispatchers, "#4");
64                         Assert.AreEqual (0, host.ChannelDispatchers.Count, "#5");
65                         Assert.IsNotNull (host.Authorization, "#6");
66                         Assert.IsNotNull (host.ExposedContracts, "#7");
67                         // Foo is already in the contracts.
68                         Assert.AreEqual (1, host.ExposedContracts.Count, "#8");
69                         // this loop iterates only once.
70                         foreach (KeyValuePair<string,ContractDescription> e in host.ExposedContracts) {
71                                 // hmm... so, seems like the key is just the full name of the contract type.
72                                 Assert.AreEqual ("MonoTests.System.ServiceModel.ServiceHostTest+Foo", e.Key, "#9");
73                                 ContractDescription cd = e.Value;
74                                 Assert.AreEqual ("Foo", cd.Name, "#10");
75                                 Assert.AreEqual ("http://tempuri.org/", cd.Namespace, "#11");
76                         }
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (ArgumentNullException))]
81                 public void CtorNull ()
82                 {
83                         new ServiceHost (typeof (Foo), null);
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentException))]
88                 public void CtorServiceTypeNotClass ()
89                 {
90                         new ServiceHost (typeof (IBar), new Uri ("http://localhost"));
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (ArgumentException))]
95                 public void CtorRelativeBaseAddress ()
96                 {
97                         new ServiceHost (typeof (Foo), new Uri ("test", UriKind.Relative));
98                 }
99                 
100                 [Test]
101                 [ExpectedException (typeof (ArgumentException))]
102                 public void CtorMultipleAddressPerScheme ()
103                 {
104                         new ServiceHost ( typeof (Foo), 
105                                         new Uri ("http://localhost", UriKind.Absolute),
106                                         new Uri ("http://someotherhost", UriKind.Absolute));
107                 }
108
109                 [Test]
110                 public void AddServiceEndpoint ()
111                 {
112                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
113                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
114                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "svc");
115
116                         Assert.IsNotNull (host.Description, "#6");
117                         Assert.IsNotNull (host.Description.Endpoints, "#7");
118                         Assert.AreEqual (host.Description.Endpoints.Count, 2, "#8");
119                         Assert.AreEqual ("http://localhost/echo/rel", host.Description.Endpoints [0].Address.Uri.AbsoluteUri,  "#9");
120                 }
121
122                 [Test]
123                 [ExpectedException (typeof (InvalidOperationException))]
124                 public void AddServiceEndpoint1 ()
125                 {
126                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("ftp://localhost/echo"));
127                         // ftp does not match BasicHttpBinding
128                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (InvalidOperationException))]
133                 public void AddServiceEndpoint2 ()
134                 {
135                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
136                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
137                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel"); // duplicate URI
138
139                         host.Open ();
140                         host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
141                 }
142
143                 [Test]
144                 [ExpectedException (typeof (InvalidOperationException))]
145                 public void AddServiceEndpoint2_2 ()
146                 {
147                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
148                         // same as above, but through Endpoints.Add()
149                         host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (Foo)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo/rel")));
150                         host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (Foo)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo/rel")));
151
152                         host.Open ();
153                         host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
154                 }
155
156                 [Test]
157                 [ExpectedException (typeof (InvalidOperationException))]
158                 public void AddServiceEndpoint2_3 ()
159                 {
160                         ServiceHost host = new ServiceHost (typeof (HogeFuga), new Uri ("http://localhost/echo"));
161                         host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (IHoge)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo")));
162                         host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (IFuga)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo")));
163
164                         // Different contracts unlike previous two cases.
165                         // If two or more endpoints are bound to the same listen
166                         // URI, then they must share the same instance.
167
168                         host.Open ();
169                         host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
170                 }
171
172                 [Test]
173                 public void AddServiceEndpoint2_4 ()
174                 {
175                         ServiceHost host = new ServiceHost (typeof (HogeFuga), new Uri ("http://localhost:37564"));
176                         var binding = new BasicHttpBinding ();
177                         host.AddServiceEndpoint (typeof (IHoge), binding, new Uri ("http://localhost:37564"));
178                         host.AddServiceEndpoint (typeof (IFuga), binding, new Uri ("http://localhost:37564"));
179
180                         // Use the same binding, results in one ChannelDispatcher (actually two, for metadata/debug behavior).
181                         host.Open ();
182                         try {
183                                 Assert.AreEqual (2, host.ChannelDispatchers.Count, "#1");
184                                 foreach (ChannelDispatcher cd in host.ChannelDispatchers) {
185                                         if (cd.BindingName != binding.Name)
186                                                 continue; // mex
187                                         Assert.AreEqual (2, cd.Endpoints.Count, "#2");
188                                 }
189                         } finally {
190                                 host.Close ();
191                         }
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (InvalidOperationException))]
196                 public void AddServiceEndpoint3 ()
197                 {
198                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
199                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
200                         host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "http://localhost/echo/rel"); // duplicate URI when resolved
201
202                         host.Open ();
203                         host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
204                 }
205
206                 [Test]
207                 public void Open ()
208                 {
209                         ServiceHost host = new ServiceHost (typeof (ZeroOperationsImpl));
210                         host.AddServiceEndpoint (typeof (IHaveZeroOperarationsContract), new BasicHttpBinding (), "http://localhost/echo");
211
212                         try {
213                                 host.Open ();
214                                 Assert.Fail ("InvalidOperationException expected");
215                         } 
216                         catch (InvalidOperationException e) {
217                                 //"ContractDescription 'IHaveZeroOperarationsContract' has zero operations; a contract must have at least one operation."
218                                 StringAssert.Contains ("IHaveZeroOperarationsContract", e.Message);
219                         }
220                         finally {
221                                 if (host.State == CommunicationState.Opened)
222                                         host.Close (); // It is to make sure to close unexpectedly opened host if the test fail.
223                         }
224                 }
225
226                 [Test]
227                 public void AddServiceEndpoint4 ()
228                 {
229                         ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
230                         host.AddServiceEndpoint ("MonoTests.System.ServiceModel.ServiceHostTest+IBaz", new BasicHttpBinding (), "rel");
231                 }
232
233                 [Test]
234                 [ExpectedException (typeof (InvalidOperationException))]
235                 public void AddServiceEndpoint5 ()
236                 {
237                         ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
238
239                         // Full type name is expected here (see AddServiceEndpoint4).
240                         host.AddServiceEndpoint ("IBaz", new BasicHttpBinding (), "rel");
241                 }
242
243                 [Test]
244                 [ExpectedException (typeof (InvalidOperationException))]
245                 public void AddServiceEndpoint6 ()
246                 {
247                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
248                         host.AddServiceEndpoint ("ISuchTypeDoesNotExist", new BasicHttpBinding (), "rel");
249                 }
250
251                 [Test]
252                 public void AddServiceEndpoint7 ()
253                 {
254                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
255                         var a = host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "a");
256                         Console.WriteLine (a.Address);
257                         Assert.AreEqual ("http", a.Address.Uri.Scheme, "#1");
258                         Assert.AreEqual ("http://localhost/echo/a", a.Address.Uri.AbsoluteUri, "#2");
259
260                         var b = host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "/b");
261                         Console.WriteLine (b.Address);
262                         Assert.AreEqual ("http", b.Address.Uri.Scheme, "#3");
263                         Assert.AreEqual ("http://localhost/echo/b", b.Address.Uri.AbsoluteUri, "#4");
264                 }
265                 
266                 [Test]
267                 [ExpectedException (typeof (InvalidOperationException))]
268                 public void AddServiceEndpointMexWithNoImpl ()
269                 {
270                         using (ServiceHost h = new ServiceHost (typeof (Foo), new Uri ("http://localhost:8080"))) {
271                                 // it expects ServiceMetadataBehavior
272                                 h.AddServiceEndpoint (ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
273                         }
274                 }
275
276                 [Test]
277                 public void AddServiceEndpointMetadataExchange ()
278                 {
279                         // MyMetadataExchange implements IMetadataExchange
280                         ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
281                         host.AddServiceEndpoint ("IMetadataExchange",
282                                                  new BasicHttpBinding (),
283                                                  "http://localhost:8080/");
284                 }
285
286                 [Test]
287                 [ExpectedException (typeof (InvalidOperationException))]
288                 public void AddServiceEndpointMetadataExchangeFullNameFails ()
289                 {
290                         ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
291                         host.AddServiceEndpoint ("System.ServiceModel.Description.IMetadataExchange",
292                                                  new BasicHttpBinding (),
293                                                  "http://localhost:8080");
294                 }
295
296                 [Test]
297                 public void InstanceWithNonSingletonMode ()
298                 {
299                         ServiceHost host = new ServiceHost (
300                                 new NonSingletonService ());
301                         Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "premise1");
302                         host.AddServiceEndpoint (
303                                 typeof (NonSingletonService),
304                                 new BasicHttpBinding (),
305                                 new Uri ("http://localhost:37564/s1"));
306
307                         // in case Open() didn't fail, we need to close the host.
308                         // And even if Close() caused the expected exception,
309                         // the test should still fail.
310                         try {
311                                 host.Open ();
312                                 try {
313                                         if (host.State == CommunicationState.Opened)
314                                                 host.Close ();
315                                 } catch (InvalidOperationException) {
316                                 }
317                                 Assert.Fail ("InstanceContextMode was not checked");
318                         } catch (InvalidOperationException) {
319                         }
320                 }
321
322
323                 [Test]
324                 public void InstanceWithSingletonMode ()
325                 {
326                         SingletonService instance = new SingletonService ();
327                         ServiceHost host = new ServiceHost (instance);
328                         Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "#1");
329                         host.AddServiceEndpoint (
330                                 typeof (SingletonService),
331                                 new BasicHttpBinding (),
332                                 new Uri ("http://localhost:37564/s2"));
333
334                         // in case Open() didn't fail, we need to close the host.
335                         // And even if Close() caused the expected exception,
336                         // the test should still fail.
337                         try {
338                                 host.Open ();
339                                 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [0];
340                                 DispatchRuntime dr = cd.Endpoints [0].DispatchRuntime;
341                                 Assert.IsNotNull (dr.InstanceContextProvider, "#2");
342                                 InstanceContext ctx = dr.InstanceContextProvider.GetExistingInstanceContext (null, null);
343                                 Assert.IsNotNull (ctx, "#3");
344                                 Assert.AreEqual (instance, ctx.GetServiceInstance (), "#4");
345                         } finally {
346                                 if (host.State == CommunicationState.Opened)
347                                         host.Close ();
348                         }
349                 }
350
351                 [ServiceContract]
352                 interface IBar
353                 {
354                 }
355
356                 [ServiceContract]
357                 class Foo
358                 {
359                         [OperationContract]
360                         public void SayWhat () { }
361                 }
362
363                 [ServiceContract]
364                 interface IBaz
365                 {
366                         [OperationContract]
367                         string Echo (string source);
368                 }
369                 
370                 [ServiceContract]
371                 interface IHoge
372                 {
373                         [OperationContract]
374                         void DoX ();
375                 }
376
377                 [ServiceContract]
378                 interface IFuga
379                 {
380                         [OperationContract]
381                         void DoY ();
382                 }
383
384                 [ServiceContract]
385                 interface IHaveZeroOperarationsContract
386                 {
387                         string Echo (string source);
388                 }
389
390                 class ZeroOperationsImpl : IHaveZeroOperarationsContract
391                 {
392                         public string Echo(string source)
393                         {
394                                 return null;
395                         }
396                 }
397
398                 class HogeFuga : IHoge, IFuga
399                 {
400                         public void DoX () {}
401                         public void DoY () {}
402                 }
403
404                 class Baz : IBaz
405                 {
406                         public string Echo (string source)
407                         {
408                                 return source;
409                         }
410                 }
411
412                 class MyMetadataExchange : IMetadataExchange
413                 {
414                         public Message Get (Message req)
415                         {
416                                 throw new NotImplementedException ();
417                         }
418
419                         public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
420                         {
421                                 throw new NotImplementedException ();
422                         }
423
424                         public Message EndGet (IAsyncResult result)
425                         {
426                                 throw new NotImplementedException ();
427                         }
428                 }
429
430                 [ServiceContract]
431                 public class NonSingletonService
432                 {
433                         [OperationContract]
434                         public void Process (string input)
435                         {
436                         }
437                 }
438
439                 [ServiceContract]
440                 [ServiceBehavior (InstanceContextMode = InstanceContextMode.Single)]
441                 public class SingletonService
442                 {
443                         [OperationContract]
444                         public void Process (string input)
445                         {
446                         }
447                 }
448         }
449 }