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