2 // ServiceMetadataBehaviorTest.cs
5 // Igor Zelmanovich <igorz@mainsoft.com>
7 // Copyright (C) 2008 Mainsoft, Inc. http://www.mainsoft.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using System.ServiceModel.Channels;
38 namespace MonoTests.System.ServiceModel.Description
41 public class ServiceMetadataBehaviorTest
50 class MyService : IMyContract
52 public string GetData () {
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 });
63 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
67 Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
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");
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");
84 EndpointAddress ea = ed.EndpointAddress;
87 DispatchRuntime dr = ed.DispatchRuntime;
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");
101 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
105 Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
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");
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");
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");
133 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
137 Assert.AreEqual (3, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
139 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [1];
140 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
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");
150 EndpointAddress ea = ed.EndpointAddress;
153 DispatchRuntime dr = ed.DispatchRuntime;
156 cd = (ChannelDispatcher) host.ChannelDispatchers [2];
157 Assert.AreEqual (1, cd.Endpoints.Count, "Endpoints.Count");
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");
167 ea = ed.EndpointAddress;
170 dr = ed.DispatchRuntime;
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> ();
184 Assert.AreEqual (0, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #1");
188 Assert.AreEqual (2, host.ChannelDispatchers.Count, "ChannelDispatchers.Count #2");
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");
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");
206 EndpointAddress ea = ed.EndpointAddress;
207 Assert.AreEqual (new Uri ("http://localhost:30158/mex"), ea.Uri, "Uri");
209 DispatchRuntime dr = ed.DispatchRuntime;
210 Assert.AreEqual (1, dr.Operations.Count, "Operations.Count");
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");
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> ();
231 Assert.IsNotNull (host.Extensions.Find<ServiceMetadataExtension> (), "ServiceMetadataExtension #1");
232 Assert.AreEqual (1, host.Extensions.FindAll<ServiceMetadataExtension> ().Count, "ServiceMetadataExtension #2");
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> ();
245 ServiceMetadataExtension extension = new ServiceMetadataExtension ();
246 host.Extensions.Add (extension);
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");
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");
269 Assert.AreEqual ("IMetadataExchange", ServiceMetadataBehavior.MexContractName, "MexContractName");