Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.ServiceModel.Web / WebOperationContextTest.cs
1 //
2 // WebOperationContextTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.IO;
30 using System.Runtime.Serialization;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 #if !MOBILE
35 using System.ServiceModel.Syndication;
36 #endif
37 using System.ServiceModel.Web;
38 using System.Xml;
39 using NUnit.Framework;
40
41 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
42
43 namespace MonoTests.System.ServiceModel.Web
44 {
45         [TestFixture]
46         public class WebOperationContextTest
47         {
48 // MonoTouch does not support dynamic proxy code generation.
49 #if !MONOTOUCH
50                 [Test]
51 #endif
52                 public void Current ()
53                 {
54 #if !MOBILE
55                         Assert.IsNull (WebOperationContext.Current, "#1");
56 #endif
57                         var binding = new WebHttpBinding ();
58                         var address = new EndpointAddress ("http://localhost:37564");
59                         var ch = (IContextChannel) WebChannelFactory<IHogeService>.CreateChannel (binding, address);
60                         using (var ocs = new OperationContextScope (ch)) {
61 #if !MOBILE
62                                 Assert.IsNotNull (WebOperationContext.Current, "#2");
63                                 Assert.IsNotNull (WebOperationContext.Current.OutgoingRequest, "#3");
64                                 Assert.IsNotNull (WebOperationContext.Current.IncomingRequest, "#4");
65                                 Assert.IsNotNull (WebOperationContext.Current.IncomingResponse, "#5");
66                                 Assert.IsNotNull (WebOperationContext.Current.OutgoingResponse, "#6"); // pointless though.
67
68                                 Assert.IsNotNull (WebOperationContext.Current.OutgoingRequest.Headers, "#7");
69                                 Assert.AreEqual (0, WebOperationContext.Current.OutgoingRequest.Headers.Count, "#8");
70 #endif
71                         }
72                         ch.Close ();
73                 }
74
75 #if NET_4_0 && !MOBILE
76                 [Test]
77                 public void CreateAtom10Response ()
78                 {
79                         CreateResponseTest (ch => ch.Join ("foo", "bar"));
80                 }
81
82                 [Test]
83                 public void CreateJsonResponse ()
84                 {
85                         CreateResponseTest (ch => ch.TestJson ("foo", "bar"));
86                 }
87
88                 [Test]
89                 [Category ("NotWorking")] // .NET rejects HogeData as an unkown  type.
90                 public void CreateJsonResponse2 ()
91                 {
92                         CreateResponseTest (ch => ch.TestJson2 ("foo", "bar"));
93                 }
94
95                 [Test]
96                 public void CreateJsonResponse3 ()
97                 {
98                         CreateResponseTest (ch => ch.TestJson3 ("foo", "bar"));
99                 }
100
101                 void CreateResponseTest (Action<IHogeService> a)
102                 {
103                         var host = new WebServiceHost (typeof (HogeService));
104                         host.AddServiceEndpoint (typeof (IHogeService), new WebHttpBinding (), new Uri ("http://localhost:37564"));
105                         host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
106                         host.Open ();
107                         try {
108                                 using (var cf = new ChannelFactory<IHogeService> (new WebHttpBinding (), new EndpointAddress ("http://localhost:37564"))) {
109                                         cf.Endpoint.Behaviors.Add (new WebHttpBehavior ());
110                                         cf.Open ();
111                                         var ch = cf.CreateChannel ();
112                                         a (ch);
113                                 }
114                         } finally {
115                                 host.Close ();
116                         }
117                 }
118 #endif
119         }
120
121         [ServiceContract]
122         public interface IHogeService
123         {
124                 [WebGet]
125                 [OperationContract]
126                 string Join (string s1, string s2);
127
128                 [WebGet (ResponseFormat = WebMessageFormat.Json)]
129                 [OperationContract]
130                 string TestJson (string s1, string s2);
131
132                 [WebGet (ResponseFormat = WebMessageFormat.Json)]
133                 [OperationContract]
134                 string TestJson2 (string s1, string s2);
135
136                 [WebGet (ResponseFormat = WebMessageFormat.Json)]
137                 [OperationContract]
138                 string TestJson3 (string s1, string s2);
139         }
140
141 #if NET_4_0 && !MOBILE
142         public class HogeService : IHogeService
143         {
144                 static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
145
146                 static string GetXml (Message msg)
147                 {
148                         var sw = new StringWriter ();
149                         using (var xw = XmlWriter.Create (sw, settings))
150                                 msg.WriteMessage (xw);
151                         return sw.ToString ();
152                 }
153
154                 public string Join (string s1, string s2)
155                 {
156                         try {
157                                 // ServiceDocument
158                                 var woc = WebOperationContext.Current;
159                                 var sd = new ServiceDocument ();
160                                 var msg = woc.CreateAtom10Response (sd);
161                                 var xml = "<service xmlns:a10='http://www.w3.org/2005/Atom' xmlns='http://www.w3.org/2007/app' />";
162                         
163                                 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#1");
164                                 // Feed
165                                 var uid = new UniqueId ().ToString ();
166                                 var updatedTime = DateTime.SpecifyKind (new DateTime (2011, 4, 8, 11, 46, 12), DateTimeKind.Utc);
167                                 var feed = new SyndicationFeed () { Id = uid, LastUpdatedTime = updatedTime };
168                                 msg = woc.CreateAtom10Response (feed);
169                                 xml = @"<feed xmlns='http://www.w3.org/2005/Atom'><title type='text'></title><id>" + uid + @"</id><updated>2011-04-08T11:46:12Z</updated></feed>";
170                                 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#2");
171
172                                 // Item
173                                 var item = new SyndicationItem () { Id = uid, LastUpdatedTime = updatedTime };
174                                 msg = woc.CreateAtom10Response (item);
175                                 xml = @"<entry xmlns='http://www.w3.org/2005/Atom'><id>" + uid + "</id><title type='text'></title><updated>2011-04-08T11:46:12Z</updated></entry>";
176                                 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#2");
177                         } catch (Exception ex) {
178                                 Console.Error.WriteLine (ex);
179                                 throw;
180                         }
181                         return s1 + s2;
182                 }
183
184                 public string TestJson (string s1, string s2)
185                 {
186                         try {
187                                 var woc = WebOperationContext.Current;
188                                 var msg = woc.CreateJsonResponse<HogeData> (new HogeData () {Foo = "foo", Bar = "bar" });
189                                 Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
190                         } catch (Exception ex) {
191                                 Console.Error.WriteLine (ex);
192                                 throw;
193                         }
194                         return s1 + s2;
195                 }
196                 
197                 public string TestJson2 (string s1, string s2)
198                 {
199                         try {
200                                 var woc = WebOperationContext.Current;
201                                 // passed <object> -> unknown type error
202                                 var msg = woc.CreateJsonResponse<object> (new HogeData () {Foo = "foo", Bar = "bar" });
203                                 Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
204
205                                 Assert.Fail ("Test2 server should fail");
206                         } catch (SerializationException ex) {
207                         } catch (Exception ex) {
208                                 Console.Error.WriteLine (ex);
209                                 throw;
210                         }
211                         return s1 + s2;
212                 }
213                 
214                 public string TestJson3 (string s1, string s2)
215                 {
216                         try {
217                                 var woc = WebOperationContext.Current;
218                                 var msg = woc.CreateJsonResponse<HogeData2> (new HogeData2 () {Foo = "foo", Bar = "bar" });
219                                 Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
220                         } catch (Exception ex) {
221                                 Console.Error.WriteLine (ex);
222                                 throw;
223                         }
224                         return s1 + s2;
225                 }
226         }
227
228         [DataContract]
229         public class HogeData
230         {
231                 [DataMember]
232                 public string Foo { get; set; }
233                 [DataMember]
234                 public string Bar { get; set; }
235         }
236
237         // non-contract
238         public class HogeData2
239         {
240                 public string Foo { get; set; }
241                 public string Bar { get; set; }
242         }
243 #endif
244 }