2 // WebServiceHostTest.cs
\r
5 // Igor Zelmanovich <igorz@mainsoft.com>
\r
7 // Copyright (C) 2008 Mainsoft, Inc (http://www.mainsoft.com)
\r
9 // Permission is hereby granted, free of charge, to any person obtaining
\r
10 // a copy of this software and associated documentation files (the
\r
11 // "Software"), to deal in the Software without restriction, including
\r
12 // without limitation the rights to use, copy, modify, merge, publish,
\r
13 // distribute, sublicense, and/or sell copies of the Software, and to
\r
14 // permit persons to whom the Software is furnished to do so, subject to
\r
15 // the following conditions:
\r
17 // The above copyright notice and this permission notice shall be
\r
18 // included in all copies or substantial portions of the Software.
\r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
30 using System.Collections.Generic;
\r
33 using NUnit.Framework;
\r
34 using System.ServiceModel;
\r
35 using System.ServiceModel.Web;
\r
36 using System.ServiceModel.Description;
\r
37 using System.ServiceModel.Dispatcher;
\r
40 using MonoTests.Helpers;
\r
42 namespace MonoTests.System.ServiceModel.Web
\r
45 public class WebServiceHostTest
\r
48 [Category("NotWorking")]
\r
49 public void ServiceDebugBehaviorTest () {
\r
51 var host = new WebServiceHost (typeof (MyService), new Uri ("http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString()));
\r
52 ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");
\r
54 Assert.AreEqual (true, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled, "HttpHelpPageEnabled #1");
\r
55 Assert.AreEqual (true, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpsHelpPageEnabled, "HttpsHelpPageEnabled #1");
\r
59 Assert.AreEqual (false, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled, "HttpHelpPageEnabled #2");
\r
60 Assert.AreEqual (false, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpsHelpPageEnabled, "HttpsHelpPageEnabled #2");
\r
66 [Category ("NotWorking")]
\r
67 public void WebHttpBehaviorTest1 () {
\r
69 var host = new WebServiceHost (typeof (MyService), new Uri ("http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString()));
\r
70 ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");
\r
71 ServiceEndpoint basicHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new BasicHttpBinding (), "BasicHttpBinding");
\r
73 Assert.AreEqual (0, webHttp.Behaviors.Count, "webHttp.Behaviors.Count #1");
\r
74 Assert.AreEqual (0, basicHttp.Behaviors.Count, "basicHttp.Behaviors.Count #1");
\r
78 Assert.AreEqual (1, webHttp.Behaviors.Count, "webHttp.Behaviors.Count #2");
\r
79 Assert.AreEqual (typeof (WebHttpBehavior), webHttp.Behaviors [0].GetType (), "behavior type");
\r
80 Assert.AreEqual (0, basicHttp.Behaviors.Count, "basicHttp.Behaviors.Count #2");
\r
86 [Category("NotWorking")]
\r
87 public void WebHttpBehaviorTest2 () {
\r
89 var host = new WebServiceHost (typeof (MyService), new Uri ("http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString()));
\r
90 ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");
\r
91 MyWebHttpBehavior behavior = new MyWebHttpBehavior ();
\r
92 behavior.ApplyDispatchBehaviorBegin += delegate {
\r
93 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].AddressFilter.GetType (), "AddressFilter.GetType #1");
\r
94 Assert.AreEqual (typeof (ActionMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].ContractFilter.GetType (), "ContractFilter.GetType #1");
\r
96 behavior.ApplyDispatchBehaviorEnd += delegate {
\r
97 Assert.AreEqual (typeof (PrefixEndpointAddressMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].AddressFilter.GetType (), "AddressFilter.GetType #2");
\r
98 Assert.AreEqual (typeof (MatchAllMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].ContractFilter.GetType (), "ContractFilter.GetType #2");
\r
100 webHttp.Behaviors.Add (behavior);
\r
107 public void ServiceBaseUriTest () {
\r
109 var host = new WebServiceHost (typeof (MyService), new Uri ("http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString()));
\r
110 Assert.AreEqual (0, host.Description.Endpoints.Count, "no endpoints yet");
\r
112 Assert.AreEqual (1, host.Description.Endpoints.Count, "default endpoint after open");
\r
116 class MyWebHttpBehavior : WebHttpBehavior
\r
118 public event EventHandler ApplyDispatchBehaviorBegin;
\r
119 public event EventHandler ApplyDispatchBehaviorEnd;
\r
121 public override void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) {
\r
122 if (ApplyDispatchBehaviorBegin != null)
\r
123 ApplyDispatchBehaviorBegin (this, EventArgs.Empty);
\r
124 base.ApplyDispatchBehavior (endpoint, endpointDispatcher);
\r
125 if (ApplyDispatchBehaviorEnd != null)
\r
126 ApplyDispatchBehaviorEnd (this, EventArgs.Empty);
\r
131 public class MyService
\r
133 [OperationContract]
\r
134 [WebGet (RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
\r
135 public string Greet (string input) {
\r
136 return "huh? " + input;
\r
141 public void Connect ()
\r
143 var url = "http://" + NetworkHelpers.LocalEphemeralEndPoint().ToString();
\r
144 var host = new WebServiceHost (typeof (DemoService), new Uri
\r
148 var wc = new WebClient();
\r
149 wc.DownloadString(url + "/testData");
\r
150 Console.WriteLine();
\r
157 interface IDemoService {
\r
158 [OperationContract]
\r
159 [WebInvoke(UriTemplate = "/{testData}",
\r
161 RequestFormat = WebMessageFormat.Json,
\r
162 ResponseFormat = WebMessageFormat.Json)]
\r
163 void UpdateAttribute(string testData);
\r
166 public class DemoService : IDemoService {
\r
167 public void UpdateAttribute(string testData)
\r
169 Console.WriteLine ("got it: "+testData);
\r