Normalize line endings.
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / ClientBaseTest.cs
1 //
2 // ClientBaseTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.ObjectModel;
30 using System.IO;
31 using System.Net.Sockets;
32 using System.Reflection;
33 using System.ServiceModel;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
36 using System.Xml;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.ServiceModel
40 {
41         [TestFixture]
42         public class ClientBaseTest
43         {
44 /*
45                 [Test]
46                 [ExpectedException (typeof (InvalidOperationException))]
47                 public void GenericTypeArgumentIsServiceContract ()
48                 {
49                         new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
50                 }
51 */
52
53 /*
54                 public class MyClientBase<T> : ClientBase<T>
55                 {
56                         public MyClientBase (Binding binding, EndpointAddress address)
57                                 : base (binding, address)
58                         {
59                         }
60                 }
61
62                 public class MyClientBase1 : ClientBase<TestService>
63                 {
64                         public MyClientBase1 (Binding binding, EndpointAddress address)
65                                 : base (binding, address)
66                         {
67                         }
68                 }
69
70                 [Test]
71                 public void ClassTypeArg ()
72                 {
73                         new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
74                 }
75 */
76
77                 [ServiceContract]
78                 public interface ITestService
79                 {
80                         [OperationContract]
81                         string Foo (string arg);
82                 }
83
84                 public class TestService : ITestService
85                 {
86                         public string Foo (string arg)
87                         {
88                                 return arg;
89                         }
90                 }
91
92                 [ServiceContract]
93                 public interface ITestService2
94                 {
95                         [OperationContract]
96                         void Bar (string arg);
97                 }
98
99                 public class TestService2 : ITestService2
100                 {
101                         public void Bar (string arg)
102                         {
103                         }
104                 }
105
106                 [Test]
107                 [Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
108                 public void Foo ()
109                 {
110                         Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
111                         Assert.IsTrue (t.IsGenericParameter);
112                         Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
113                 }
114
115                 class MyChannelFactory<T> : ChannelFactory<T>
116                 {
117                         public MyChannelFactory (Binding b, EndpointAddress e)
118                                 : base (b, e)
119                         {
120                         }
121
122                         public IChannelFactory GimmeFactory ()
123                         {
124                                 return CreateFactory ();
125                         }
126                 }
127
128                 #region UseCase1
129
130                 ServiceHost host;
131
132                 [Test]
133                 [ExpectedException (typeof (ArgumentNullException))]
134                 public void ClientBaseCtorArgsTest1 ()
135                 {
136                         new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (ArgumentNullException))]
141                 public void ClientBaseCtorArgsTest2 ()
142                 {
143                         new CtorUseCase1 (null, new EndpointAddress ("http://test"));
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentNullException))]
148                 public void ClientBaseCtorArgsTest3 ()
149                 {
150                         new CtorUseCase1 (null, "http://test");
151                 }
152
153                 [Test]
154                 [ExpectedException (typeof (ArgumentNullException))]
155                 public void ClientBaseCtorArgsTest4 ()
156                 {
157                         new CtorUseCase1 ("CtorUseCase1_1", null);
158                 }
159
160                 [Test]
161                 [ExpectedException (typeof (ArgumentNullException))]
162                 public void ClientBaseCtorArgsTest5 ()
163                 {
164                         new CtorUseCase1 (new BasicHttpBinding (), null);
165                 }
166
167                 [Test]
168                 [ExpectedException (typeof (InvalidOperationException))]
169                 public void ClientBaseCtorArgsTest6 ()
170                 {
171                         new CtorUseCase1 ("CtorUseCase1_Incorrect");
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (InvalidOperationException))]
176                 public void ClientBaseCtorArgsTest7 ()
177                 {
178                         new CtorUseCase3 ();
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (InvalidOperationException))]
183                 public void ClientBaseCtorConfigAmbiguityTest ()
184                 {
185                         new CtorUseCase2 ();
186                 }
187
188                 [Test]
189                 [ExpectedException (typeof (InvalidOperationException))]
190                 public void ClientBaseCtorConfigAmbiguityTest2 ()
191                 {
192                         new CtorUseCase2 ("*");
193                 }
194
195                 [Test]
196                 [Ignore ("fails under .NET; I never bothered to fix the test")]
197                 public void ClientBaseConfigEmptyCtor ()
198                 {
199                         new CtorUseCase1 ();
200                 }
201
202                 [Test]
203                 [Ignore ("fails under .NET; I never bothered to fix the test")]
204                 public void ClientBaseConfigCtor ()
205                 {
206                         new CtorUseCase1 ("CtorUseCase1_1");
207                 }
208
209                 [Test]
210                 [Ignore ("fails under .NET; I never bothered to fix the test")]
211                 public void ClientBaseConfigCtorUsingDefault ()
212                 {
213                         new CtorUseCase1 ("*");
214                 }
215
216                 [Test]
217                 [Ignore ("With Orcas it does not work fine")]
218                 public void UseCase1Test ()
219                 {
220                         // almost equivalent to samples/clientbase/samplesvc.cs
221                         using (host = new ServiceHost (typeof (UseCase1))) {
222                                 Binding binding = new BasicHttpBinding ();
223                                 binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
224                                 host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:37564"));
225
226                                 host.Open ();
227                                 // almost equivalent to samples/clientbase/samplecli.cs
228                                 using (UseCase1Proxy proxy = new UseCase1Proxy (
229                                         new BasicHttpBinding (),
230                                         new EndpointAddress ("http://localhost:37564"))) {
231                                         proxy.Open ();
232                                         Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
233                                 }
234                         }
235                         EnsurePortNonBlocking (37564);
236                 }
237
238                 void EnsurePortNonBlocking (int port)
239                 {
240                         TcpListener l = new TcpListener (port);
241                         l.ExclusiveAddressUse = true;
242                         l.Start ();
243                         l.Stop ();
244                 }
245
246                 [ServiceContract]
247                 public interface IUseCase1
248                 {
249                         [OperationContract]
250                         string Echo (string msg);
251                 }
252
253                 public class UseCase1 : IUseCase1
254                 {
255                         public string Echo (string msg)
256                         {
257                                 return msg + msg;
258                         }
259                 }
260
261                 public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
262                 {
263                         public UseCase1Proxy (Binding binding, EndpointAddress address)
264                                 : base (binding, address)
265                         {
266                         }
267
268                         public string Echo (string msg)
269                         {
270                                 return Channel.Echo (msg);
271                         }
272                 }
273
274                 public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
275                 {
276                         public CtorUseCase1 ()
277                                 : base ()
278                         {
279                         }
280
281                         public CtorUseCase1 (string configName)
282                                 : base (configName)
283                         {
284                         }
285
286                         public CtorUseCase1 (string configName, string address)
287                                 : base (configName, address)
288                         {
289                         }
290
291                         public CtorUseCase1 (Binding binding, EndpointAddress address)
292                                 : base (binding, address)
293                         {
294                         }
295
296                         public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
297                                 : base (i, binding, address)
298                         {
299                         }
300
301                         public string Echo (string msg)
302                         {
303                                 return Channel.Echo (msg);
304                         }
305                 }
306
307                 public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
308                 {
309                         public CtorUseCase2 ()
310                                 : base ()
311                         {
312                         }
313
314                         public CtorUseCase2 (string configName)
315                                 : base (configName)
316                         {
317                         }
318
319                         public CtorUseCase2 (string configName, string address)
320                                 : base (configName, address)
321                         {
322                         }
323
324                         public CtorUseCase2 (Binding binding, EndpointAddress address)
325                                 : base (binding, address)
326                         {
327                         }
328
329                         public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
330                                 : base (i, binding, address)
331                         {
332                         }
333
334                         public string Echo (string msg)
335                         {
336                                 return Channel.Echo (msg);
337                         }
338                 }
339
340                 public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
341                 {
342                         public string Echo (string msg)
343                         {
344                                 return Channel.Echo (msg);
345                         }
346                 }
347
348                 #endregion
349
350                 // For contract that directly receives and sends Message instances.
351                 #region UseCase2
352                 [Test]
353                 [Ignore ("With Orcas it does not work fine")]
354                 public void UseCase2Test ()
355                 {
356                         // almost equivalent to samples/clientbase/samplesvc2.cs
357                         ServiceHost host = new ServiceHost (typeof (UseCase2));
358                         Binding binding = new BasicHttpBinding ();
359                         binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
360                         host.AddServiceEndpoint (typeof (IUseCase2).FullName,
361                         binding, new Uri ("http://localhost:37564"));
362
363                         try {
364                                 host.Open ();
365                                 // almost equivalent to samples/clientbase/samplecli2.cs
366                                 Binding b = new BasicHttpBinding ();
367                                 b.SendTimeout = TimeSpan.FromSeconds (15);
368                                 b.ReceiveTimeout = TimeSpan.FromSeconds (15);
369                                 UseCase2Proxy proxy = new UseCase2Proxy (
370                                         b,
371                                         new EndpointAddress ("http://localhost:37564/"));
372                                 proxy.Open ();
373                                 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
374                                 Message res = proxy.Echo (req);
375                                 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
376                                         res.WriteMessage (w);
377                                 }
378                         } finally {
379                                 if (host.State == CommunicationState.Opened)
380                                         host.Close ();
381                                 EnsurePortNonBlocking (37564);
382                         }
383                 }
384
385                 [ServiceContract]
386                 public interface IUseCase2
387                 {
388                         [OperationContract]
389                         Message Echo (Message request);
390                 }
391
392                 class UseCase2 : IUseCase2
393                 {
394                         public Message Echo (Message request)
395                         {
396                                 Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
397                                 msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
398                                 //msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
399                                 return msg;
400                         }
401                 }
402
403                 public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
404                 {
405                         public UseCase2Proxy (Binding binding, EndpointAddress address)
406                                 : base (binding, address)
407                         {
408                         }
409
410                         public Message Echo (Message request)
411                         {
412                                 return Channel.Echo (request);
413                         }
414                 }
415
416                 #endregion
417
418                 [Test]
419                 [Ignore ("With Orcas it does not work fine")]
420                 public void UseCase3 ()
421                 {
422                         // almost equivalent to samples/clientbase/samplesvc3.cs
423                         ServiceHost host = new ServiceHost (typeof (MetadataExchange));
424                         host.Description.Behaviors.Find<ServiceDebugBehavior> ()
425                                 .IncludeExceptionDetailInFaults = true;
426                         Binding bs = new BasicHttpBinding ();
427                         bs.SendTimeout = TimeSpan.FromSeconds (5);
428                         bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
429                         // magic name that does not require fully qualified name ...
430                         host.AddServiceEndpoint ("IMetadataExchange",
431                                 bs, new Uri ("http://localhost:37564"));
432                         try {
433                                 host.Open ();
434                                 // almost equivalent to samples/clientbase/samplecli3.cs
435                                 Binding bc = new BasicHttpBinding ();
436                                 bc.SendTimeout = TimeSpan.FromSeconds (5);
437                                 bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
438                                 MetadataExchangeProxy proxy = new MetadataExchangeProxy (
439                                         bc,
440                                         new EndpointAddress ("http://localhost:37564/"));
441                                 proxy.Open ();
442
443                                 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
444                                 Message res = proxy.Get (req);
445                                 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
446                                         res.WriteMessage (w);
447                                 }
448                         } finally {
449                                 if (host.State == CommunicationState.Opened)
450                                         host.Close ();
451                                 EnsurePortNonBlocking (37564);
452                         }
453                 }
454
455                 class MetadataExchange : IMetadataExchange
456                 {
457                         public Message Get (Message request)
458                         {
459                                 XmlDocument doc = new XmlDocument ();
460                                 doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
461                                 return Message.CreateMessage (request.Version,
462                                 "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
463                                 new XmlNodeReader (doc));
464                         }
465
466                         public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
467                         {
468                                 throw new NotImplementedException ();
469                         }
470
471                         public Message EndGet (IAsyncResult result)
472                         {
473                                 throw new NotImplementedException ();
474                         }
475                 }
476         }
477 }