Merge pull request #946 from akoeplinger/fix-mono-parallel
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.ServiceModel.Web / WebServiceHostTest.cs
1 //\r
2 // WebServiceHostTest.cs\r
3 //\r
4 // Author:\r
5 //      Igor Zelmanovich  <igorz@mainsoft.com>\r
6 //\r
7 // Copyright (C) 2008 Mainsoft, Inc (http://www.mainsoft.com)\r
8 //\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
16 // \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
19 // \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
27 //\r
28 #if !MOBILE\r
29 using System;\r
30 using System.Collections.Generic;\r
31 using System.Linq;\r
32 using System.Text;\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
38 \r
39 namespace MonoTests.System.ServiceModel.Web\r
40 {\r
41         [TestFixture]\r
42         public class WebServiceHostTest\r
43         {\r
44                 [Test]\r
45                 [Category("NotWorking")]\r
46                 public void ServiceDebugBehaviorTest () {\r
47 \r
48                         var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));\r
49                         ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");\r
50 \r
51                         Assert.AreEqual (true, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled, "HttpHelpPageEnabled #1");\r
52                         Assert.AreEqual (true, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpsHelpPageEnabled, "HttpsHelpPageEnabled #1");\r
53 \r
54                         host.Open ();\r
55 \r
56                         Assert.AreEqual (false, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpHelpPageEnabled, "HttpHelpPageEnabled #2");\r
57                         Assert.AreEqual (false, host.Description.Behaviors.Find<ServiceDebugBehavior> ().HttpsHelpPageEnabled, "HttpsHelpPageEnabled #2");\r
58 \r
59                         host.Close ();\r
60                 }\r
61 \r
62                 [Test]\r
63                 [Category ("NotWorking")]\r
64                 public void WebHttpBehaviorTest1 () {\r
65 \r
66                         var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));\r
67                         ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");\r
68                         ServiceEndpoint basicHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new BasicHttpBinding (), "BasicHttpBinding");\r
69 \r
70                         Assert.AreEqual (0, webHttp.Behaviors.Count, "webHttp.Behaviors.Count #1");\r
71                         Assert.AreEqual (0, basicHttp.Behaviors.Count, "basicHttp.Behaviors.Count #1");\r
72 \r
73                         host.Open ();\r
74 \r
75                         Assert.AreEqual (1, webHttp.Behaviors.Count, "webHttp.Behaviors.Count #2");\r
76                         Assert.AreEqual (typeof (WebHttpBehavior), webHttp.Behaviors [0].GetType (), "behavior type");\r
77                         Assert.AreEqual (0, basicHttp.Behaviors.Count, "basicHttp.Behaviors.Count #2");\r
78 \r
79                         host.Close ();\r
80                 }\r
81 \r
82                 [Test]\r
83                 [Category("NotWorking")]\r
84                 public void WebHttpBehaviorTest2 () {\r
85 \r
86                         var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));\r
87                         ServiceEndpoint webHttp = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.Web.WebServiceHostTest+MyService", new WebHttpBinding (), "WebHttpBinding");\r
88                         MyWebHttpBehavior behavior = new MyWebHttpBehavior ();\r
89                         behavior.ApplyDispatchBehaviorBegin += delegate {\r
90                                 Assert.AreEqual (typeof (EndpointAddressMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].AddressFilter.GetType (), "AddressFilter.GetType #1");\r
91                                 Assert.AreEqual (typeof (ActionMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].ContractFilter.GetType (), "ContractFilter.GetType #1");\r
92                         };\r
93                         behavior.ApplyDispatchBehaviorEnd += delegate {\r
94                                 Assert.AreEqual (typeof (PrefixEndpointAddressMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].AddressFilter.GetType (), "AddressFilter.GetType #2");\r
95                                 Assert.AreEqual (typeof (MatchAllMessageFilter), ((ChannelDispatcher) host.ChannelDispatchers [0]).Endpoints [0].ContractFilter.GetType (), "ContractFilter.GetType #2");\r
96                         };\r
97                         webHttp.Behaviors.Add (behavior);\r
98 \r
99                         host.Open ();\r
100                         host.Close ();\r
101                 }\r
102 \r
103                 [Test]\r
104                 public void ServiceBaseUriTest () {\r
105 \r
106                         var host = new WebServiceHost (typeof (MyService), new Uri ("http://localhost:30158/"));\r
107                         Assert.AreEqual (0, host.Description.Endpoints.Count, "no endpoints yet");\r
108                         host.Open ();\r
109                         Assert.AreEqual (1, host.Description.Endpoints.Count, "default endpoint after open");\r
110                         host.Close ();\r
111                 }\r
112 \r
113                 class MyWebHttpBehavior : WebHttpBehavior\r
114                 {\r
115                         public event EventHandler ApplyDispatchBehaviorBegin;\r
116                         public event EventHandler ApplyDispatchBehaviorEnd;\r
117 \r
118                         public override void ApplyDispatchBehavior (ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) {\r
119                                 if (ApplyDispatchBehaviorBegin != null)\r
120                                         ApplyDispatchBehaviorBegin (this, EventArgs.Empty);\r
121                                 base.ApplyDispatchBehavior (endpoint, endpointDispatcher);\r
122                                 if (ApplyDispatchBehaviorEnd != null)\r
123                                         ApplyDispatchBehaviorEnd (this, EventArgs.Empty);\r
124                         }\r
125                 }\r
126 \r
127                 [ServiceContract]\r
128                 public class MyService\r
129                 {\r
130                         [OperationContract]\r
131                         [WebGet (RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]\r
132                         public string Greet (string input) {\r
133                                 return "huh? " + input;\r
134                         }\r
135                 }\r
136 \r
137         }\r
138 }\r
139 #endif\r