Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ServiceModel.Discovery / Test / System.ServiceModel.Discovery / AnnouncementClientTest.cs
1 //
2 // Author: Atsushi Enomoto <atsushi@ximian.com>
3 //
4 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 using System;
26 using System.Collections.Generic;
27 using System.Collections.ObjectModel;
28 using System.Linq;
29 using System.ServiceModel;
30 using System.ServiceModel.Channels;
31 using System.ServiceModel.Description;
32 using System.ServiceModel.Discovery;
33 using System.ServiceModel.Dispatcher;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.ServiceModel.Discovery
37 {
38         [TestFixture]
39         public class AnnouncementClientTest
40         {
41                 [Test]
42                 public void DefaultValues ()
43                 {
44                         // default constructor causes IOE unless there is default configuration...
45                         var ac = new AnnouncementClient (new AnnouncementEndpoint ());
46                         Assert.AreEqual (ac.ChannelFactory.Endpoint, ac.Endpoint, "#1");
47                         Assert.AreEqual ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01", ac.Endpoint.Contract.Namespace, "#2");
48                         Assert.AreEqual (2, ac.Endpoint.Contract.Operations.Count, "#2-2");
49                         Assert.IsTrue (ac.Endpoint.Contract.Operations.Any (od => od.Messages.Any (md => md.Action == "http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/Hello")), "#2-3");
50                         Assert.IsTrue (ac.Endpoint.Contract.Operations.Any (od => od.Messages.Any (md => md.Action == "http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/Bye")), "#2-4");
51                         Assert.IsNull (ac.Endpoint.Binding, "#3");
52                         Assert.IsNull (ac.Endpoint.Address, "#4");
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void AnnonceOnlineOfflineNoEndpointAddress ()
58                 {
59                         var ac = new AnnouncementClient (new AnnouncementEndpoint () { Binding = new BasicHttpBinding () });
60                         var edm = new EndpointDiscoveryMetadata ();
61                         try {
62                                 ac.AnnounceOnline (edm);
63                         } finally {
64                                 ac.Close ();
65                         }
66                 }
67
68                 [Test]
69                 [ExpectedException (typeof (InvalidOperationException))]
70                 public void AnnonceOnlineOfflineNoBinding ()
71                 {
72                         var ac = new AnnouncementClient (new AnnouncementEndpoint () { Address = new EndpointAddress ("http://localhost:37564")});
73                         var edm = new EndpointDiscoveryMetadata ();
74                         ac.AnnounceOnline (edm);
75                         // attempt to close the client causes another CommunicationObjectFaultedException - looks like it fails to allow Close() at faulted state unlike other objects.
76                 }
77
78                 [Test]
79                 // looks like EndpointAddress is *ignored*
80                 public void AnnonceOnlineOfflineAddressSchemeMismatch ()
81                 {
82                         var ac = new AnnouncementClient (new UdpAnnouncementEndpoint () { Address = new EndpointAddress ("http://localhost:37564")});
83                         var edm = new EndpointDiscoveryMetadata ();
84                         try {
85                                 ac.AnnounceOnline (edm);
86                                 ac.AnnounceOffline (edm);
87                         } finally {
88                                 ac.Close ();
89                         }
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void AnnonceOnlineOfflineAddressSchemeMismatch2 ()
95                 {
96                         var ac = new AnnouncementClient (new AnnouncementEndpoint () { Binding = new BasicHttpBinding (), Address = new EndpointAddress ("soap.udp://localhost:37564")});
97                         var edm = new EndpointDiscoveryMetadata ();
98                         try {
99                                 ac.AnnounceOnline (edm);
100                         } finally {
101                                 ac.Close ();
102                         }
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (InvalidOperationException))]
107                 public void AnnonceOnlineOfflineHttpMessageVersionMismatch ()
108                 {
109                         var ac = new AnnouncementClient (new AnnouncementEndpoint () { Binding = new BasicHttpBinding () { SendTimeout = TimeSpan.FromSeconds (10), ReceiveTimeout = TimeSpan.FromSeconds (10) }, Address = http_address });
110                         var edm = new EndpointDiscoveryMetadata ();
111                         try {
112                                 ac.AnnounceOnline (edm);
113                         } finally {
114                                 ac.Close ();
115                         }
116                 }
117
118                 EndpointAddress http_address = new EndpointAddress ("http://localhost:37564");
119
120                 [Test]
121                 [ExpectedException (typeof (EndpointNotFoundException))]
122                 public void AnnonceOnlineOfflineHttpWSA10 ()
123                 {
124                         var binding = new CustomBinding (new HttpTransportBindingElement ()) { SendTimeout = TimeSpan.FromSeconds (10), ReceiveTimeout = TimeSpan.FromSeconds (10) };
125                         var ac = new AnnouncementClient (new AnnouncementEndpoint () { Binding = binding, Address = http_address });
126                         var edm = new EndpointDiscoveryMetadata ();
127                         try {
128                                 ac.AnnounceOnline (edm);
129                         } finally {
130                                 ac.Close ();
131                         }
132                 }
133         }
134 }