move to from olive to mcs
[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                 [Ignore ("AddServiceEndpoint part does not work")]
111                 public void AddServiceEndpoint ()
112                 {
113                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
114                         host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
115                         host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "svc");
116
117                         Assert.IsNotNull (host.Description, "#6");
118                         Assert.IsNotNull (host.Description.Endpoints, "#7");
119                         Assert.AreEqual (host.Description.Endpoints.Count, 2, "#8");
120                         Assert.AreEqual ("http://localhost/echo/rel", host.Description.Endpoints [0].Address.Uri.AbsoluteUri,  "#9");
121                 }
122
123                 [Test]
124                 [ExpectedException (typeof (InvalidOperationException))]
125                 public void AddServiceEndpoint1 ()
126                 {
127                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("ftp://localhost/echo"));
128                         // ftp does not match BasicHttpBinding
129                         host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (InvalidOperationException))]
134                 public void AddServiceEndpoint2 ()
135                 {
136                         // IBar is not part of the contract
137                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
138                         host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
139                         //host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
140                 }
141
142                 [Test]
143                 [ExpectedException (typeof (InvalidOperationException))]
144                 public void AddServiceEndpoint3 ()
145                 {
146                         // IBar is not part of the contract
147                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
148                         host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
149                         // host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "http://localhost/echo/rel");
150                 }
151
152                 [Test]
153                 public void AddServiceEndpoint4 ()
154                 {
155                         ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
156                         host.AddServiceEndpoint ("MonoTests.System.ServiceModel.ServiceHostTest+IBaz", new BasicHttpBinding (), "rel");
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (InvalidOperationException))]
161                 public void AddServiceEndpoint5 ()
162                 {
163                         ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
164                         // Full type name is expected here.
165                         host.AddServiceEndpoint ("IBaz", new BasicHttpBinding (), "rel");
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (InvalidOperationException))]
170                 public void AddServiceEndpoint6 ()
171                 {
172                         ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
173                         host.AddServiceEndpoint ("ISuchTypeDoesNotExist", new BasicHttpBinding (), "rel");
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (InvalidOperationException))]
178                 public void AddServiceEndpointMex ()
179                 {
180                         using (ServiceHost h = new ServiceHost (typeof (Foo), new Uri ("http://localhost:8080"))) {
181                                 // it expects ServiceMetadataBehavior
182                                 h.AddServiceEndpoint (ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
183                         }
184                 }
185
186                 [Test]
187                 public void AddServiceEndpointMetadataExchange ()
188                 {
189                         ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
190                         host.AddServiceEndpoint ("IMetadataExchange",
191                                                  new BasicHttpBinding (),
192                                                  "http://localhost:8080");
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (InvalidOperationException))]
197                 public void AddServiceEndpointMetadataExchangeFullNameFails ()
198                 {
199                         ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
200                         host.AddServiceEndpoint ("System.ServiceModel.Description.IMetadataExchange",
201                                                  new BasicHttpBinding (),
202                                                  "http://localhost:8080");
203                 }
204
205                 [Test]
206                 public void InstanceWithNonSingletonMode ()
207                 {
208                         ServiceHost host = new ServiceHost (
209                                 new NonSingletonService ());
210                         Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "premise1");
211                         host.AddServiceEndpoint (
212                                 typeof (NonSingletonService),
213                                 new BasicHttpBinding (),
214                                 new Uri ("http://localhost:37564/s1"));
215
216                         // in case Open() didn't fail, we need to close the host.
217                         // And even if Close() caused the expected exception,
218                         // the test should still fail.
219                         try {
220                                 host.Open ();
221                                 try {
222                                         if (host.State == CommunicationState.Opened)
223                                                 host.Close ();
224                                 } catch (InvalidOperationException) {
225                                 }
226                                 Assert.Fail ("InstanceContextMode was not checked");
227                         } catch (InvalidOperationException) {
228                         }
229                 }
230
231
232                 [Test]
233                 public void InstanceWithSingletonMode ()
234                 {
235                         SingletonService instance = new SingletonService ();
236                         ServiceHost host = new ServiceHost (instance);
237                         Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "#1");
238                         host.AddServiceEndpoint (
239                                 typeof (SingletonService),
240                                 new BasicHttpBinding (),
241                                 new Uri ("http://localhost:37564/s2"));
242
243                         // in case Open() didn't fail, we need to close the host.
244                         // And even if Close() caused the expected exception,
245                         // the test should still fail.
246                         try {
247                                 host.Open ();
248                                 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [0];
249                                 DispatchRuntime dr = cd.Endpoints [0].DispatchRuntime;
250                                 Assert.IsNotNull (dr.InstanceContextProvider, "#2");
251                                 InstanceContext ctx = dr.InstanceContextProvider.GetExistingInstanceContext (null, null);
252                                 Assert.IsNotNull (ctx, "#3");
253                                 Assert.AreEqual (instance, ctx.GetServiceInstance (), "#4");
254                         } finally {
255                                 if (host.State == CommunicationState.Opened)
256                                         host.Close ();
257                         }
258                 }
259
260                 [ServiceContract]
261                 interface IBar
262                 {
263                 }
264
265                 [ServiceContract]
266                 class Foo
267                 {
268                         [OperationContract]
269                         public void SayWhat () { }
270                 }
271
272                 [ServiceContract]
273                 interface IBaz
274                 {
275                         [OperationContract]
276                         string Echo (string source);
277                 }
278
279                 class Baz : IBaz
280                 {
281                         public string Echo (string source)
282                         {
283                                 return source;
284                         }
285                 }
286
287                 class MyMetadataExchange : IMetadataExchange
288                 {
289                         public Message Get (Message req)
290                         {
291                                 throw new NotImplementedException ();
292                         }
293
294                         public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
295                         {
296                                 throw new NotImplementedException ();
297                         }
298
299                         public Message EndGet (IAsyncResult result)
300                         {
301                                 throw new NotImplementedException ();
302                         }
303                 }
304
305                 [ServiceContract]
306                 public class NonSingletonService
307                 {
308                         [OperationContract]
309                         public void Process (string input)
310                         {
311                         }
312                 }
313
314                 [ServiceContract]
315                 [ServiceBehavior (InstanceContextMode = InstanceContextMode.Single)]
316                 public class SingletonService
317                 {
318                         [OperationContract]
319                         public void Process (string input)
320                         {
321                         }
322                 }
323         }
324 }