[bcl] Remove NET_4_0 defines from class libs
[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 using MonoTests.Helpers;
40
41 namespace MonoTests.System.ServiceModel
42 {
43         [TestFixture]
44         public class ClientBaseTest
45         {
46 /*
47                 [Test]
48                 [ExpectedException (typeof (InvalidOperationException))]
49                 public void GenericTypeArgumentIsServiceContract ()
50                 {
51                         new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
52                 }
53 */
54
55 /*
56                 public class MyClientBase<T> : ClientBase<T>
57                 {
58                         public MyClientBase (Binding binding, EndpointAddress address)
59                                 : base (binding, address)
60                         {
61                         }
62                 }
63
64                 public class MyClientBase1 : ClientBase<TestService>
65                 {
66                         public MyClientBase1 (Binding binding, EndpointAddress address)
67                                 : base (binding, address)
68                         {
69                         }
70                 }
71
72                 [Test]
73                 public void ClassTypeArg ()
74                 {
75                         new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
76                 }
77 */
78
79                 [ServiceContract]
80                 public interface ITestService
81                 {
82                         [OperationContract]
83                         string Foo (string arg);
84                 }
85
86                 public class TestService : ITestService
87                 {
88                         public string Foo (string arg)
89                         {
90                                 return arg;
91                         }
92                 }
93
94                 [ServiceContract]
95                 public interface ITestService2
96                 {
97                         [OperationContract]
98                         void Bar (string arg);
99                 }
100
101                 public class TestService2 : ITestService2
102                 {
103                         public void Bar (string arg)
104                         {
105                         }
106                 }
107
108                 [Test]
109                 [Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
110                 public void Foo ()
111                 {
112                         Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
113                         Assert.IsTrue (t.IsGenericParameter);
114                         Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
115                 }
116
117                 class MyChannelFactory<T> : ChannelFactory<T>
118                 {
119                         public MyChannelFactory (Binding b, EndpointAddress e)
120                                 : base (b, e)
121                         {
122                         }
123
124                         public IChannelFactory GimmeFactory ()
125                         {
126                                 return CreateFactory ();
127                         }
128                 }
129
130                 #region UseCase1
131
132                 ServiceHost host;
133
134                 [Test]
135                 [ExpectedException (typeof (ArgumentNullException))]
136                 public void ClientBaseCtorArgsTest1 ()
137                 {
138                         new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof (ArgumentNullException))]
143                 public void ClientBaseCtorArgsTest2 ()
144                 {
145                         new CtorUseCase1 (null, new EndpointAddress ("http://test"));
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentNullException))]
150                 public void ClientBaseCtorArgsTest3 ()
151                 {
152                         new CtorUseCase1 (null, "http://test");
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (ArgumentNullException))]
157                 public void ClientBaseCtorArgsTest4 ()
158                 {
159                         new CtorUseCase1 ("CtorUseCase1_1", null);
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (ArgumentNullException))]
164                 public void ClientBaseCtorArgsTest5 ()
165                 {
166                         new CtorUseCase1 (new BasicHttpBinding (), null);
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (InvalidOperationException))]
171                 public void ClientBaseCtorArgsTest6 ()
172                 {
173                         new CtorUseCase1 ("CtorUseCase1_Incorrect");
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (InvalidOperationException))]
178                 public void ClientBaseCtorArgsTest7 ()
179                 {
180                         new CtorUseCase3 ();
181                 }
182
183                 [Test]
184                 [ExpectedException (typeof (InvalidOperationException))]
185                 public void ClientBaseCtorConfigAmbiguityTest ()
186                 {
187                         new CtorUseCase2 ();
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (InvalidOperationException))]
192                 public void ClientBaseCtorConfigAmbiguityTest2 ()
193                 {
194                         new CtorUseCase2 ("*");
195                 }
196
197                 [Test]
198                 [Ignore ("fails under .NET; I never bothered to fix the test")]
199                 public void ClientBaseConfigEmptyCtor ()
200                 {
201                         new CtorUseCase1 ();
202                 }
203
204                 [Test]
205                 [Ignore ("fails under .NET; I never bothered to fix the test")]
206                 public void ClientBaseConfigCtor ()
207                 {
208                         new CtorUseCase1 ("CtorUseCase1_1");
209                 }
210
211                 [Test]
212                 [Ignore ("fails under .NET; I never bothered to fix the test")]
213                 public void ClientBaseConfigCtorUsingDefault ()
214                 {
215                         new CtorUseCase1 ("*");
216                 }
217
218                 [Test]
219                 [Ignore ("With Orcas it does not work fine")]
220                 public void UseCase1Test ()
221                 {
222                         int port = NetworkHelpers.FindFreePort ();
223                         // almost equivalent to samples/clientbase/samplesvc.cs
224                         using (host = new ServiceHost (typeof (UseCase1))) {
225                                 Binding binding = new BasicHttpBinding ();
226                                 binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
227                                 host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:" + port));
228
229                                 host.Open ();
230                                 // almost equivalent to samples/clientbase/samplecli.cs
231                                 using (UseCase1Proxy proxy = new UseCase1Proxy (
232                                         new BasicHttpBinding (),
233                                         new EndpointAddress ("http://localhost:" + port))) {
234                                         proxy.Open ();
235                                         Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
236                                 }
237                         }
238                         EnsurePortNonBlocking (port);
239                 }
240
241                 void EnsurePortNonBlocking (int port)
242                 {
243                         TcpListener l = new TcpListener (port);
244                         l.ExclusiveAddressUse = true;
245                         l.Start ();
246                         l.Stop ();
247                 }
248
249                 [ServiceContract]
250                 public interface IUseCase1
251                 {
252                         [OperationContract]
253                         string Echo (string msg);
254                 }
255
256                 public class UseCase1 : IUseCase1
257                 {
258                         public string Echo (string msg)
259                         {
260                                 return msg + msg;
261                         }
262                 }
263
264                 public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
265                 {
266                         public UseCase1Proxy (Binding binding, EndpointAddress address)
267                                 : base (binding, address)
268                         {
269                         }
270
271                         public string Echo (string msg)
272                         {
273                                 return Channel.Echo (msg);
274                         }
275                 }
276
277                 public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
278                 {
279                         public CtorUseCase1 ()
280                                 : base ()
281                         {
282                         }
283
284                         public CtorUseCase1 (string configName)
285                                 : base (configName)
286                         {
287                         }
288
289                         public CtorUseCase1 (string configName, string address)
290                                 : base (configName, address)
291                         {
292                         }
293
294                         public CtorUseCase1 (Binding binding, EndpointAddress address)
295                                 : base (binding, address)
296                         {
297                         }
298
299                         public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
300                                 : base (i, binding, address)
301                         {
302                         }
303
304                         public string Echo (string msg)
305                         {
306                                 return Channel.Echo (msg);
307                         }
308                 }
309
310                 public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
311                 {
312                         public CtorUseCase2 ()
313                                 : base ()
314                         {
315                         }
316
317                         public CtorUseCase2 (string configName)
318                                 : base (configName)
319                         {
320                         }
321
322                         public CtorUseCase2 (string configName, string address)
323                                 : base (configName, address)
324                         {
325                         }
326
327                         public CtorUseCase2 (Binding binding, EndpointAddress address)
328                                 : base (binding, address)
329                         {
330                         }
331
332                         public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
333                                 : base (i, binding, address)
334                         {
335                         }
336
337                         public string Echo (string msg)
338                         {
339                                 return Channel.Echo (msg);
340                         }
341                 }
342
343                 public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
344                 {
345                         public string Echo (string msg)
346                         {
347                                 return Channel.Echo (msg);
348                         }
349                 }
350
351                 #endregion
352
353                 // For contract that directly receives and sends Message instances.
354                 #region UseCase2
355                 [Test]
356                 [Ignore ("With Orcas it does not work fine")]
357                 public void UseCase2Test ()
358                 {
359                         // almost equivalent to samples/clientbase/samplesvc2.cs
360                         ServiceHost host = new ServiceHost (typeof (UseCase2));
361                         Binding binding = new BasicHttpBinding ();
362                         binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
363                         int port = NetworkHelpers.FindFreePort ();
364                         host.AddServiceEndpoint (typeof (IUseCase2).FullName,
365                         binding, new Uri ("http://localhost:" + port));
366
367                         try {
368                                 host.Open ();
369                                 // almost equivalent to samples/clientbase/samplecli2.cs
370                                 Binding b = new BasicHttpBinding ();
371                                 b.SendTimeout = TimeSpan.FromSeconds (15);
372                                 b.ReceiveTimeout = TimeSpan.FromSeconds (15);
373                                 UseCase2Proxy proxy = new UseCase2Proxy (
374                                         b,
375                                         new EndpointAddress ("http://localhost:" + port + "/"));
376                                 proxy.Open ();
377                                 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
378                                 Message res = proxy.Echo (req);
379                                 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
380                                         res.WriteMessage (w);
381                                 }
382                         } finally {
383                                 if (host.State == CommunicationState.Opened)
384                                         host.Close ();
385                                 EnsurePortNonBlocking (port);
386                         }
387                 }
388
389                 [ServiceContract]
390                 public interface IUseCase2
391                 {
392                         [OperationContract]
393                         Message Echo (Message request);
394                 }
395
396                 class UseCase2 : IUseCase2
397                 {
398                         public Message Echo (Message request)
399                         {
400                                 Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
401                                 msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
402                                 //msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
403                                 return msg;
404                         }
405                 }
406
407                 public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
408                 {
409                         public UseCase2Proxy (Binding binding, EndpointAddress address)
410                                 : base (binding, address)
411                         {
412                         }
413
414                         public Message Echo (Message request)
415                         {
416                                 return Channel.Echo (request);
417                         }
418                 }
419
420                 #endregion
421
422                 [Test]
423                 [Ignore ("With Orcas it does not work fine")]
424                 public void UseCase3 ()
425                 {
426                         // almost equivalent to samples/clientbase/samplesvc3.cs
427                         ServiceHost host = new ServiceHost (typeof (MetadataExchange));
428                         host.Description.Behaviors.Find<ServiceDebugBehavior> ()
429                                 .IncludeExceptionDetailInFaults = true;
430                         Binding bs = new BasicHttpBinding ();
431                         bs.SendTimeout = TimeSpan.FromSeconds (5);
432                         bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
433                         int port = NetworkHelpers.FindFreePort ();
434                         // magic name that does not require fully qualified name ...
435                         host.AddServiceEndpoint ("IMetadataExchange",
436                                 bs, new Uri ("http://localhost:" + port));
437                         try {
438                                 host.Open ();
439                                 // almost equivalent to samples/clientbase/samplecli3.cs
440                                 Binding bc = new BasicHttpBinding ();
441                                 bc.SendTimeout = TimeSpan.FromSeconds (5);
442                                 bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
443                                 MetadataExchangeProxy proxy = new MetadataExchangeProxy (
444                                         bc,
445                                         new EndpointAddress ("http://localhost:" + port + "/"));
446                                 proxy.Open ();
447
448                                 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
449                                 Message res = proxy.Get (req);
450                                 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
451                                         res.WriteMessage (w);
452                                 }
453                         } finally {
454                                 if (host.State == CommunicationState.Opened)
455                                         host.Close ();
456                                 EnsurePortNonBlocking (port);
457                         }
458                 }
459
460                 class MetadataExchange : IMetadataExchange
461                 {
462                         public Message Get (Message request)
463                         {
464                                 XmlDocument doc = new XmlDocument ();
465                                 doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
466                                 return Message.CreateMessage (request.Version,
467                                 "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
468                                 new XmlNodeReader (doc));
469                         }
470
471                         public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
472                         {
473                                 throw new NotImplementedException ();
474                         }
475
476                         public Message EndGet (IAsyncResult result)
477                         {
478                                 throw new NotImplementedException ();
479                         }
480                 }
481
482                 [Test]
483                 public void ConstructorServiceEndpoint ()
484                 {
485                         // It is okay to pass ServiceEndpoint that does not have Binding or EndpointAddress.
486                         new MyClient (new ServiceEndpoint (ContractDescription.GetContract (typeof (IMetadataExchange)), null, null));
487                         try {
488                                 new MyClient ((Binding) null, (EndpointAddress) null);
489                                 Assert.Fail ("ArgumentNullException is expected");
490                         } catch (ArgumentNullException) {
491                         }
492                 }
493
494                 class MyClient : ClientBase<IMetadataExchange>
495                 {
496                         public MyClient (ServiceEndpoint endpoint)
497                                 : base (endpoint)
498                         {
499                         }
500                         
501                         public MyClient (Binding binding, EndpointAddress address)
502                                 : base (binding, address)
503                         {
504                         }
505                 }
506         }
507 }