2c7509da96cd4aa4b0f5cb860c3a1da5389aeba4
[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 using System.Net;\r
39 \r
40 namespace MonoTests.System.ServiceModel.Web\r
41 {\r
42         [TestFixture]\r
43         public class WebServiceHostTest\r
44         {\r
45                 [Test]\r
46                 [Category("NotWorking")]\r
47                 public void ServiceDebugBehaviorTest () {\r
48 \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
51 \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
54 \r
55                         host.Open ();\r
56 \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
59 \r
60                         host.Close ();\r
61                 }\r
62 \r
63                 [Test]\r
64                 [Category ("NotWorking")]\r
65                 public void WebHttpBehaviorTest1 () {\r
66 \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
70 \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
73 \r
74                         host.Open ();\r
75 \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
79 \r
80                         host.Close ();\r
81                 }\r
82 \r
83                 [Test]\r
84                 [Category("NotWorking")]\r
85                 public void WebHttpBehaviorTest2 () {\r
86 \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
93                         };\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
97                         };\r
98                         webHttp.Behaviors.Add (behavior);\r
99 \r
100                         host.Open ();\r
101                         host.Close ();\r
102                 }\r
103 \r
104                 [Test]\r
105                 public void ServiceBaseUriTest () {\r
106 \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
109                         host.Open ();\r
110                         Assert.AreEqual (1, host.Description.Endpoints.Count, "default endpoint after open");\r
111                         host.Close ();\r
112                 }\r
113 \r
114                 class MyWebHttpBehavior : WebHttpBehavior\r
115                 {\r
116                         public event EventHandler ApplyDispatchBehaviorBegin;\r
117                         public event EventHandler ApplyDispatchBehaviorEnd;\r
118 \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
125                         }\r
126                 }\r
127 \r
128                 [ServiceContract]\r
129                 public class MyService\r
130                 {\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
135                         }\r
136                 }\r
137 \r
138                 [Test]\r
139                 public void Connect ()\r
140                 {\r
141                         var host = new WebServiceHost (typeof (DemoService), new Uri\r
142                                                        ("http://localhost:30158/"));\r
143                         try {\r
144                                 host.Open ();\r
145                                 var wc = new WebClient();\r
146                                 wc.DownloadString("http://localhost:30158/testData");\r
147                                 Console.WriteLine();\r
148                         } finally {\r
149                                 host.Close();\r
150                         }\r
151                 }\r
152                 \r
153                 [ServiceContract]\r
154                 interface IDemoService {\r
155                         [OperationContract]\r
156                         [WebInvoke(UriTemplate = "/{testData}",\r
157                                    Method = "GET",\r
158                                    RequestFormat = WebMessageFormat.Json,\r
159                                    ResponseFormat = WebMessageFormat.Json)]\r
160                         void UpdateAttribute(string testData);\r
161                 }\r
162 \r
163                 public class DemoService : IDemoService {\r
164                         public void UpdateAttribute(string testData)\r
165                         {\r
166                                 Console.WriteLine ("got it: "+testData);\r
167                         }\r
168                 }\r
169         }\r
170 }\r
171 #endif\r