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 namespace MonoTests.System.ServiceModel.Web
\r
43 public class WebServiceHostTest
\r
46 [Category("NotWorking")]
\r
47 public void ServiceDebugBehaviorTest () {
\r
49 var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));
\r
50 ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");
\r
52 Assert.AreEqual (true, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled, "HttpHelpPageEnabled #1");
\r
53 Assert.AreEqual (true, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpsHelpPageEnabled, "HttpsHelpPageEnabled #1");
\r
57 Assert.AreEqual (false, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled, "HttpHelpPageEnabled #2");
\r
58 Assert.AreEqual (false, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpsHelpPageEnabled, "HttpsHelpPageEnabled #2");
\r
64 [Category ("NotWorking")]
\r
65 public void WebHttpBehaviorTest1 () {
\r
67 var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));
\r
68 ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");
\r
69 ServiceEndpoint basicHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new BasicHttpBinding (), "BasicHttpBinding");
\r
71 Assert.AreEqual (0, webHttp.Behaviors.Count, "webHttp.Behaviors.Count #1");
\r
72 Assert.AreEqual (0, basicHttp.Behaviors.Count, "basicHttp.Behaviors.Count #1");
\r
76 Assert.AreEqual (1, webHttp.Behaviors.Count, "webHttp.Behaviors.Count #2");
\r
77 Assert.AreEqual (typeof (WebHttpBehavior), webHttp.Behaviors [0].GetType (), "behavior type");
\r
78 Assert.AreEqual (0, basicHttp.Behaviors.Count, "basicHttp.Behaviors.Count #2");
\r
84 [Category("NotWorking")]
\r
85 public void WebHttpBehaviorTest2 () {
\r
87 var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));
\r
88 ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");
\r
89 MyWebHttpBehavior behavior = new MyWebHttpBehavior ();
\r
90 behavior.ApplyDispatchBehaviorBegin += delegate {
\r
91 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].AddressFilter.GetType (), "AddressFilter.GetType #1");
\r
92 Assert.AreEqual (typeof (ActionMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].ContractFilter.GetType (), "ContractFilter.GetType #1");
\r
94 behavior.ApplyDispatchBehaviorEnd += delegate {
\r
95 Assert.AreEqual (typeof (PrefixEndpointAddressMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].AddressFilter.GetType (), "AddressFilter.GetType #2");
\r
96 Assert.AreEqual (typeof (MatchAllMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].ContractFilter.GetType (), "ContractFilter.GetType #2");
\r
98 webHttp.Behaviors.Add (behavior);
\r
105 public void ServiceBaseUriTest () {
\r
107 var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));
\r
108 Assert.AreEqual (0, host.Description.Endpoints.Count, "no endpoints yet");
\r
110 Assert.AreEqual (1, host.Description.Endpoints.Count, "default endpoint after open");
\r
114 class MyWebHttpBehavior : WebHttpBehavior
\r
116 public event EventHandler ApplyDispatchBehaviorBegin;
\r
117 public event EventHandler ApplyDispatchBehaviorEnd;
\r
119 public override void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) {
\r
120 if (ApplyDispatchBehaviorBegin != null)
\r
121 ApplyDispatchBehaviorBegin (this, EventArgs.Empty);
\r
122 base.ApplyDispatchBehavior (endpoint, endpointDispatcher);
\r
123 if (ApplyDispatchBehaviorEnd != null)
\r
124 ApplyDispatchBehaviorEnd (this, EventArgs.Empty);
\r
129 public class MyService
\r
131 [OperationContract]
\r
132 [WebGet (RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
\r
133 public string Greet (string input) {
\r
134 return "huh? " + input;
\r
139 public void Connect ()
\r
141 var host = new WebServiceHost (typeof (DemoService), new Uri
\r
142 ("http://localhost:30158/"));
\r
145 var wc = new WebClient();
\r
146 wc.DownloadString("http://localhost:30158/testData");
\r
147 Console.WriteLine();
\r
154 interface IDemoService {
\r
155 [OperationContract]
\r
156 [WebInvoke(UriTemplate = "/{testData}",
\r
158 RequestFormat = WebMessageFormat.Json,
\r
159 ResponseFormat = WebMessageFormat.Json)]
\r
160 void UpdateAttribute(string testData);
\r
163 public class DemoService : IDemoService {
\r
164 public void UpdateAttribute(string testData)
\r
166 Console.WriteLine ("got it: "+testData);
\r