2 // WebOperationContextTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Runtime.Serialization;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
35 using System.ServiceModel.Syndication;
37 using System.ServiceModel.Web;
39 using NUnit.Framework;
41 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
43 namespace MonoTests.System.ServiceModel.Web
46 public class WebOperationContextTest
49 public void Current ()
52 Assert.IsNull (WebOperationContext.Current, "#1");
54 var binding = new WebHttpBinding ();
55 var address = new EndpointAddress ("http://localhost:37564");
56 var ch = (IContextChannel) WebChannelFactory<IHogeService>.CreateChannel (binding, address);
57 using (var ocs = new OperationContextScope (ch)) {
59 Assert.IsNotNull (WebOperationContext.Current, "#2");
60 Assert.IsNotNull (WebOperationContext.Current.OutgoingRequest, "#3");
61 Assert.IsNotNull (WebOperationContext.Current.IncomingRequest, "#4");
62 Assert.IsNotNull (WebOperationContext.Current.IncomingResponse, "#5");
63 Assert.IsNotNull (WebOperationContext.Current.OutgoingResponse, "#6"); // pointless though.
69 #if NET_4_0 && !MOBILE
71 public void CreateAtom10Response ()
73 CreateResponseTest (ch => ch.Join ("foo", "bar"));
77 public void CreateJsonResponse ()
79 CreateResponseTest (ch => ch.TestJson ("foo", "bar"));
83 [Category ("NotWorking")] // .NET rejects HogeData as an unkown type.
84 public void CreateJsonResponse2 ()
86 CreateResponseTest (ch => ch.TestJson2 ("foo", "bar"));
90 public void CreateJsonResponse3 ()
92 CreateResponseTest (ch => ch.TestJson3 ("foo", "bar"));
95 void CreateResponseTest (Action<IHogeService> a)
97 var host = new WebServiceHost (typeof (HogeService));
98 host.AddServiceEndpoint (typeof (IHogeService), new WebHttpBinding (), new Uri ("http://localhost:37564"));
99 host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
102 using (var cf = new ChannelFactory<IHogeService> (new WebHttpBinding (), new EndpointAddress ("http://localhost:37564"))) {
103 cf.Endpoint.Behaviors.Add (new WebHttpBehavior ());
105 var ch = cf.CreateChannel ();
116 public interface IHogeService
120 string Join (string s1, string s2);
122 [WebGet (ResponseFormat = WebMessageFormat.Json)]
124 string TestJson (string s1, string s2);
126 [WebGet (ResponseFormat = WebMessageFormat.Json)]
128 string TestJson2 (string s1, string s2);
130 [WebGet (ResponseFormat = WebMessageFormat.Json)]
132 string TestJson3 (string s1, string s2);
135 #if NET_4_0 && !MOBILE
136 public class HogeService : IHogeService
138 static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
140 static string GetXml (Message msg)
142 var sw = new StringWriter ();
143 using (var xw = XmlWriter.Create (sw, settings))
144 msg.WriteMessage (xw);
145 return sw.ToString ();
148 public string Join (string s1, string s2)
152 var woc = WebOperationContext.Current;
153 var sd = new ServiceDocument ();
154 var msg = woc.CreateAtom10Response (sd);
155 var xml = "<service xmlns:a10='http://www.w3.org/2005/Atom' xmlns='http://www.w3.org/2007/app' />";
157 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#1");
159 var uid = new UniqueId ().ToString ();
160 var updatedTime = DateTime.SpecifyKind (new DateTime (2011, 4, 8, 11, 46, 12), DateTimeKind.Utc);
161 var feed = new SyndicationFeed () { Id = uid, LastUpdatedTime = updatedTime };
162 msg = woc.CreateAtom10Response (feed);
163 xml = @"<feed xmlns='http://www.w3.org/2005/Atom'><title type='text'></title><id>" + uid + @"</id><updated>2011-04-08T11:46:12Z</updated></feed>";
164 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#2");
167 var item = new SyndicationItem () { Id = uid, LastUpdatedTime = updatedTime };
168 msg = woc.CreateAtom10Response (item);
169 xml = @"<entry xmlns='http://www.w3.org/2005/Atom'><id>" + uid + "</id><title type='text'></title><updated>2011-04-08T11:46:12Z</updated></entry>";
170 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#2");
171 } catch (Exception ex) {
172 Console.Error.WriteLine (ex);
178 public string TestJson (string s1, string s2)
181 var woc = WebOperationContext.Current;
182 var msg = woc.CreateJsonResponse<HogeData> (new HogeData () {Foo = "foo", Bar = "bar" });
183 Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
184 } catch (Exception ex) {
185 Console.Error.WriteLine (ex);
191 public string TestJson2 (string s1, string s2)
194 var woc = WebOperationContext.Current;
195 // passed <object> -> unknown type error
196 var msg = woc.CreateJsonResponse<object> (new HogeData () {Foo = "foo", Bar = "bar" });
197 Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
199 Assert.Fail ("Test2 server should fail");
200 } catch (SerializationException ex) {
201 } catch (Exception ex) {
202 Console.Error.WriteLine (ex);
208 public string TestJson3 (string s1, string s2)
211 var woc = WebOperationContext.Current;
212 var msg = woc.CreateJsonResponse<HogeData2> (new HogeData2 () {Foo = "foo", Bar = "bar" });
213 Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
214 } catch (Exception ex) {
215 Console.Error.WriteLine (ex);
223 public class HogeData
226 public string Foo { get; set; }
228 public string Bar { get; set; }
232 public class HogeData2
234 public string Foo { get; set; }
235 public string Bar { get; set; }