[Profiler] Use the correct function to increment the refcount
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Description / ServiceMetadataBehaviorTest.cs
1 //
2 // ServiceMetadataBehaviorTest.cs
3 //
4 // Author:
5 //      Igor Zelmanovich <igorz@mainsoft.com>
6 //
7 // Copyright (C) 2008 Mainsoft, Inc.  http://www.mainsoft.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
29 using System;
30 using System.Collections.Generic;
31 using System.Text;
32 using NUnit.Framework;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using System.ServiceModel.Channels;
37
38 namespace MonoTests.System.ServiceModel.Description
39 {
40         [TestFixture]
41         public class ServiceMetadataBehaviorTest
42         {
43                 [ServiceContract]
44                 interface IMyContract
45                 {
46                         [OperationContract]
47                         string GetData ();
48                 }
49
50                 class MyService : IMyContract
51                 {
52                         public string GetData () {
53                                 return "Hello World";
54                         }
55                 }
56
57                 [Test]
58                 public void InitializeRuntime1 () {
59                         using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:30158"))) {
60                                 host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "e1");
61                                 host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true });
62
63                                 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
64
65                                 host.Open ();
66
67                                 Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
68
69                                 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
70                                 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
71                                 Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
72                                 Assert.AreEqual (host, cd.Host, "Host");
73                                 //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
74                                 //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
75
76                                 EndpointDispatcher ed = cd.Endpoints [0];
77                                 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
78                                 Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
79                                 Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
80                                 Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
81                                 Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
82                                 Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
83
84                                 EndpointAddress ea = ed.EndpointAddress;
85                                 // TODO
86
87                                 DispatchRuntime dr = ed.DispatchRuntime;
88                                 // TODO
89
90                                 host.Close ();
91                         }
92                 }
93
94                 [Test]
95                 public void InitializeRuntime2 () {
96                         using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:30158"))) {
97                                 host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
98                                 host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:30158/mex_and_help") });
99                                 host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:30158/mex_and_help");
100
101                                 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
102
103                                 host.Open ();
104
105                                 Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
106
107                                 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
108                                 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
109                                 Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
110                                 Assert.AreEqual (host, cd.Host, "Host");
111                                 //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
112                                 //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
113
114                                 EndpointDispatcher ed = cd.Endpoints [0];
115                                 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
116                                 Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
117                                 Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
118                                 Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
119                                 Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
120                                 Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
121
122                                 host.Close ();
123                         }
124                 }
125
126                 [Test]
127                 public void InitializeRuntime3 () {
128                         using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:30158"))) {
129                                 host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
130                                 host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:30158/mex") });
131                                 host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageUrl = new Uri ("http://localhost:30158/help");
132
133                                 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
134
135                                 host.Open ();
136
137                                 Assert.AreEqual (3, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
138
139                                 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
140                                 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
141
142                                 EndpointDispatcher ed = cd.Endpoints [0];
143                                 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter #1");
144                                 Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher #1");
145                                 Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter #1");
146                                 Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName #1");
147                                 Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace #1");
148                                 Assert.AreEqual (0, ed.FilterPriority, "FilterPriority #1");
149
150                                 EndpointAddress ea = ed.EndpointAddress;
151                                 // TODO
152
153                                 DispatchRuntime dr = ed.DispatchRuntime;
154                                 // TODO
155
156                                 cd = (ChannelDispatcher) host.ChannelDispatchers [2];
157                                 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
158
159                                 ed = cd.Endpoints [0];
160                                 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter #2");
161                                 Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher #2");
162                                 Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter #2");
163                                 Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName #2");
164                                 Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace #2");
165                                 Assert.AreEqual (0, ed.FilterPriority, "FilterPriority #2");
166
167                                 ea = ed.EndpointAddress;
168                                 // TODO
169
170                                 dr = ed.DispatchRuntime;
171                                 // TODO
172
173                                 host.Close ();
174                         }
175                 }
176
177                 [Test]
178                 public void InitializeRuntime4 () {
179                         using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:30158"))) {
180                                 host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
181                                 host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:30158/mex") });
182                                 host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
183
184                                 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
185
186                                 host.Open ();
187
188                                 Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
189
190                                 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
191                                 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
192                                 Assert.AreEqual ("ServiceMetadataBehaviorHttpGetBinding", cd.BindingName, "BindingName");
193                                 Assert.AreEqual (host, cd.Host, "Host");
194                                 //Assert.AreEqual (false, cd.IsTransactedAccept, "IsTransactedAccept");
195                                 //Assert.AreEqual (false, cd.IsTransactedReceive, "IsTransactedReceive");
196                                 Assert.AreEqual (MessageVersion.None, cd.MessageVersion, "MessageVersion");
197
198                                 EndpointDispatcher ed = cd.Endpoints [0];
199                                 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ed.AddressFilter.GetType (), "AddressFilter");
200                                 Assert.AreEqual (cd, ed.ChannelDispatcher, "ChannelDispatcher");
201                                 Assert.AreEqual (typeof (MatchAllMessageFilter), ed.ContractFilter.GetType (), "ContractFilter");
202                                 Assert.AreEqual ("IHttpGetHelpPageAndMetadataContract", ed.ContractName, "ContractName");
203                                 Assert.AreEqual ("http://schemas.microsoft.com/2006/04/http/metadata", ed.ContractNamespace, "ContractNamespace");
204                                 Assert.AreEqual (0, ed.FilterPriority, "FilterPriority");
205
206                                 EndpointAddress ea = ed.EndpointAddress;
207                                 Assert.AreEqual (new Uri ("http://localhost:30158/mex"), ea.Uri, "Uri");
208
209                                 DispatchRuntime dr = ed.DispatchRuntime;
210                                 Assert.AreEqual (1, dr.Operations.Count, "Operations.Count");
211
212                                 DispatchOperation dispOp = dr.Operations [0];
213                                 Assert.AreEqual ("*", dispOp.Action, "Operation.Action");
214                                 Assert.AreEqual ("*", dispOp.ReplyAction, "Operation.ReplyAction");
215                                 Assert.AreEqual ("Get", dispOp.Name, "Operation.Name");
216                                 //Assert.IsNotNull (dispOp.Invoker, "Operation.Invoker");
217
218                                 host.Close ();
219                         }
220                 }
221
222                 [Test]
223                 public void ServiceMetadataExtension1 () {
224                         using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:30158"))) {
225                                 host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
226                                 host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:30158/mex") });
227                                 host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
228
229                                 host.Open ();
230
231                                 Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
232                                 Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
233
234                                 host.Close ();
235                         }
236                 }
237
238                 [Test]
239                 public void ServiceMetadataExtension2 () {
240                         using (ServiceHost host = new ServiceHost (typeof (MyService), new Uri ("http://localhost:30158"))) {
241                                 host.AddServiceEndpoint (typeof (IMyContract), new BasicHttpBinding (), "");
242                                 host.Description.Behaviors.Add (new ServiceMetadataBehavior () { HttpGetEnabled = true, HttpGetUrl = new Uri ("http://localhost:30158/mex") });
243                                 host.Description.Behaviors.Remove<ServiceDebugBehavior> ();
244
245                                 ServiceMetadataExtension extension = new ServiceMetadataExtension ();
246                                 host.Extensions.Add (extension);
247
248                                 host.Open ();
249
250                                 Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
251                                 Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
252                                 Assert.AreEqual (extension, host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #3");
253
254                                 host.Close ();
255                         }
256                 }
257
258                 [Test]
259                 public void Defaults () {
260                         ServiceMetadataBehavior behavior = new ServiceMetadataBehavior ();
261                         Assert.IsNull (behavior.ExternalMetadataLocation, "ExternalMetadataLocation");
262                         Assert.AreEqual (false, behavior.HttpGetEnabled, "HttpGetEnabled");
263                         Assert.IsNull (behavior.HttpGetUrl, "HttpGetUrl");
264                         Assert.AreEqual (false, behavior.HttpsGetEnabled, "HttpsGetEnabled");
265                         Assert.IsNull (behavior.HttpsGetUrl, "HttpsGetUrl");
266                         Assert.IsNotNull (behavior.MetadataExporter, "MetadataExporter #1");
267                         Assert.AreEqual (typeof (WsdlExporter), behavior.MetadataExporter.GetType (), "MetadataExporter #2");
268
269                         Assert.AreEqual ("IMetadataExchange", ServiceMetadataBehavior.MexContractName, "MexContractName");
270                 }
271         }
272 }