summaryrefslogtreecommitdiff
path: root/mcs/class/System.ServiceModel.Web/Test/System.ServiceModel.Web/WebOperationContextTest.cs
blob: 392a9cf54ec233b9cd8614e41bcec6020a475828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// WebOperationContextTest.cs
//
// Author:
//	Atsushi Enomoto  <atsushi@ximian.com>
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
#if !MOBILE
using System.ServiceModel.Syndication;
#endif
using System.ServiceModel.Web;
using System.Xml;
using NUnit.Framework;

using CategoryAttribute = NUnit.Framework.CategoryAttribute;

namespace MonoTests.System.ServiceModel.Web
{
	[TestFixture]
	public class WebOperationContextTest
	{
		[Test]
		public void Current ()
		{
#if !MOBILE
			Assert.IsNull (WebOperationContext.Current, "#1");
#endif
			var binding = new WebHttpBinding ();
			var address = new EndpointAddress ("http://localhost:37564");
			var ch = (IContextChannel) WebChannelFactory<IHogeService>.CreateChannel (binding, address);
			using (var ocs = new OperationContextScope (ch)) {
#if !MOBILE
				Assert.IsNotNull (WebOperationContext.Current, "#2");
				Assert.IsNotNull (WebOperationContext.Current.OutgoingRequest, "#3");
				Assert.IsNotNull (WebOperationContext.Current.IncomingRequest, "#4");
				Assert.IsNotNull (WebOperationContext.Current.IncomingResponse, "#5");
				Assert.IsNotNull (WebOperationContext.Current.OutgoingResponse, "#6"); // pointless though.

				Assert.IsNotNull (WebOperationContext.Current.OutgoingRequest.Headers, "#7");
				Assert.AreEqual (0, WebOperationContext.Current.OutgoingRequest.Headers.Count, "#8");
#endif
			}
			ch.Close ();
		}

#if NET_4_0 && !MOBILE
		[Test]
		public void CreateAtom10Response ()
		{
			CreateResponseTest (ch => ch.Join ("foo", "bar"));
		}

		[Test]
		public void CreateJsonResponse ()
		{
			CreateResponseTest (ch => ch.TestJson ("foo", "bar"));
		}

		[Test]
		[Category ("NotWorking")] // .NET rejects HogeData as an unkown  type.
		public void CreateJsonResponse2 ()
		{
			CreateResponseTest (ch => ch.TestJson2 ("foo", "bar"));
		}

		[Test]
		public void CreateJsonResponse3 ()
		{
			CreateResponseTest (ch => ch.TestJson3 ("foo", "bar"));
		}

		void CreateResponseTest (Action<IHogeService> a)
		{
			var host = new WebServiceHost (typeof (HogeService));
			host.AddServiceEndpoint (typeof (IHogeService), new WebHttpBinding (), new Uri ("http://localhost:37564"));
			host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
			host.Open ();
			try {
				using (var cf = new ChannelFactory<IHogeService> (new WebHttpBinding (), new EndpointAddress ("http://localhost:37564"))) {
					cf.Endpoint.Behaviors.Add (new WebHttpBehavior ());
					cf.Open ();
					var ch = cf.CreateChannel ();
					a (ch);
				}
			} finally {
				host.Close ();
			}
		}
#endif
	}

	[ServiceContract]
	public interface IHogeService
	{
		[WebGet]
		[OperationContract]
		string Join (string s1, string s2);

		[WebGet (ResponseFormat = WebMessageFormat.Json)]
		[OperationContract]
		string TestJson (string s1, string s2);

		[WebGet (ResponseFormat = WebMessageFormat.Json)]
		[OperationContract]
		string TestJson2 (string s1, string s2);

		[WebGet (ResponseFormat = WebMessageFormat.Json)]
		[OperationContract]
		string TestJson3 (string s1, string s2);
	}

#if NET_4_0 && !MOBILE
	public class HogeService : IHogeService
	{
		static XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true };

		static string GetXml (Message msg)
		{
			var sw = new StringWriter ();
			using (var xw = XmlWriter.Create (sw, settings))
				msg.WriteMessage (xw);
			return sw.ToString ();
		}

		public string Join (string s1, string s2)
		{
			try {
				// ServiceDocument
				var woc = WebOperationContext.Current;
				var sd = new ServiceDocument ();
				var msg = woc.CreateAtom10Response (sd);
				var xml = "<service xmlns:a10='http://www.w3.org/2005/Atom' xmlns='http://www.w3.org/2007/app' />";
			
				Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#1");
				// Feed
				var uid = new UniqueId ().ToString ();
				var updatedTime = DateTime.SpecifyKind (new DateTime (2011, 4, 8, 11, 46, 12), DateTimeKind.Utc);
				var feed = new SyndicationFeed () { Id = uid, LastUpdatedTime = updatedTime };
				msg = woc.CreateAtom10Response (feed);
				xml = @"<feed xmlns='http://www.w3.org/2005/Atom'><title type='text'></title><id>" + uid + @"</id><updated>2011-04-08T11:46:12Z</updated></feed>";
				Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#2");

				// Item
				var item = new SyndicationItem () { Id = uid, LastUpdatedTime = updatedTime };
				msg = woc.CreateAtom10Response (item);
				xml = @"<entry xmlns='http://www.w3.org/2005/Atom'><id>" + uid + "</id><title type='text'></title><updated>2011-04-08T11:46:12Z</updated></entry>";
				Assert.AreEqual (xml.Replace ('\'', '"'), GetXml (msg), "#2");
			} catch (Exception ex) {
				Console.Error.WriteLine (ex);
				throw;
			}
			return s1 + s2;
		}

		public string TestJson (string s1, string s2)
		{
			try {
				var woc = WebOperationContext.Current;
				var msg = woc.CreateJsonResponse<HogeData> (new HogeData () {Foo = "foo", Bar = "bar" });
				Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
			} catch (Exception ex) {
				Console.Error.WriteLine (ex);
				throw;
			}
			return s1 + s2;
		}
		
		public string TestJson2 (string s1, string s2)
		{
			try {
				var woc = WebOperationContext.Current;
				// passed <object> -> unknown type error
				var msg = woc.CreateJsonResponse<object> (new HogeData () {Foo = "foo", Bar = "bar" });
				Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");

				Assert.Fail ("Test2 server should fail");
			} catch (SerializationException ex) {
			} catch (Exception ex) {
				Console.Error.WriteLine (ex);
				throw;
			}
			return s1 + s2;
		}
		
		public string TestJson3 (string s1, string s2)
		{
			try {
				var woc = WebOperationContext.Current;
				var msg = woc.CreateJsonResponse<HogeData2> (new HogeData2 () {Foo = "foo", Bar = "bar" });
				Assert.AreEqual ("<root type=\"object\"><Bar>bar</Bar><Foo>foo</Foo></root>", GetXml (msg), "#1");
			} catch (Exception ex) {
				Console.Error.WriteLine (ex);
				throw;
			}
			return s1 + s2;
		}
	}

	[DataContract]
	public class HogeData
	{
		[DataMember]
		public string Foo { get; set; }
		[DataMember]
		public string Bar { get; set; }
	}

	// non-contract
	public class HogeData2
	{
		public string Foo { get; set; }
		public string Bar { get; set; }
	}
#endif
}