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
48 // MonoTouch does not support dynamic proxy code generation.
52 public void Current ()
55 Assert.IsNull (WebOperationContext.Current, "#1");
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)) {
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.
68 Assert.IsNotNull (WebOperationContext.Current.OutgoingRequest.Headers, "#7");
69 Assert.AreEqual (0, WebOperationContext.Current.OutgoingRequest.Headers.Count, "#8");
75 #if NET_4_0 && !MOBILE
77 public void CreateAtom10Response ()
79 CreateResponseTest (ch => ch.Join ("foo", "bar"));
83 public void CreateJsonResponse ()
85 CreateResponseTest (ch => ch.TestJson ("foo", "bar"));
89 [Category ("NotWorking")] // .NET rejects HogeData as an unkown type.
90 public void CreateJsonResponse2 ()
92 CreateResponseTest (ch => ch.TestJson2 ("foo", "bar"));
96 public void CreateJsonResponse3 ()
98 CreateResponseTest (ch => ch.TestJson3 ("foo", "bar"));
101 void CreateResponseTest (Action<IHogeService> a)
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;
108 using (var cf = new ChannelFactory<IHogeService> (new WebHttpBinding (), new EndpointAddress ("http://localhost:37564"))) {
109 cf.Endpoint.Behaviors.Add (new WebHttpBehavior ());
111 var ch = cf.CreateChannel ();
122 public interface IHogeService
126 string Join (string s1, string s2);
128 [WebGet (ResponseFormat = WebMessageFormat.Json)]
130 string TestJson (string s1, string s2);
132 [WebGet (ResponseFormat = WebMessageFormat.Json)]
134 string TestJson2 (string s1, string s2);
136 [WebGet (ResponseFormat = WebMessageFormat.Json)]
138 string TestJson3 (string s1, string s2);
141 #if NET_4_0 && !MOBILE
142 public class HogeService : IHogeService
144 static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
146 static string GetXml (Message msg)
148 var sw = new StringWriter ();
149 using (var xw = XmlWriter.Create (sw, settings))
150 msg.WriteMessage (xw);
151 return sw.ToString ();
154 public string Join (string s1, string s2)
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' />";
163 Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#1");
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");
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);
184 public string TestJson (string s1, string s2)
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);
197 public string TestJson2 (string s1, string s2)
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");
205 Assert.Fail ("Test2 server should fail");
206 } catch (SerializationException ex) {
207 } catch (Exception ex) {
208 Console.Error.WriteLine (ex);
214 public string TestJson3 (string s1, string s2)
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);
229 public class HogeData
232 public string Foo { get; set; }
234 public string Bar { get; set; }
238 public class HogeData2
240 public string Foo { get; set; }
241 public string Bar { get; set; }