WebHttpDispatchOperationSelectorTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // WebHttpDispatchOperationSelectorTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if !MOBILE
  30. using System;
  31. using System.Globalization;
  32. using System.Runtime.Serialization;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Channels;
  35. using System.ServiceModel.Description;
  36. using System.ServiceModel.Dispatcher;
  37. using System.ServiceModel.Web;
  38. using System.Text;
  39. using System.Xml;
  40. using NUnit.Framework;
  41. using MonoTests.Helpers;
  42. namespace MonoTests.System.ServiceModel.Dispatcher
  43. {
  44. [TestFixture]
  45. public class WebHttpDispatchOperationSelectorTest
  46. {
  47. [Test]
  48. [ExpectedException (typeof (ArgumentNullException))]
  49. public void ConstructorNullEndpoint ()
  50. {
  51. new WebHttpDispatchOperationSelector (null);
  52. }
  53. [Test]
  54. [ExpectedException (typeof (InvalidOperationException))]
  55. public void ConstructorEndpointNullAddress ()
  56. {
  57. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
  58. new WebHttpDispatchOperationSelector (se);
  59. }
  60. #region SelectOperation
  61. [Test]
  62. public void SelectOperation ()
  63. {
  64. SelectOperationCore (Create ());
  65. }
  66. [Test]
  67. public void SelectOperation2 ()
  68. {
  69. SelectOperationCore (Create2 ());
  70. }
  71. [Test]
  72. public void SelectOperation3 ()
  73. {
  74. ContractDescription cd = ContractDescription.GetContract (typeof (MyService2));
  75. Assert.IsNotNull (cd.Operations [0].Behaviors.Find<WebGetAttribute> (), "#1");
  76. cd = ContractDescription.GetContract (typeof (MyService));
  77. Assert.IsNull (cd.Operations [0].Behaviors.Find<WebGetAttribute> (), "#2");
  78. }
  79. void SelectOperationCore (MySelector d)
  80. {
  81. string name;
  82. var msg = Message.CreateMessage (MessageVersion.Soap12, "http://temuri.org/MyService/Echo"); // looks like Version is not checked
  83. Assert.IsNull (msg.Headers.To, "#1-0");
  84. Assert.IsFalse (d.SelectOperation (ref msg, out name), "#1");
  85. Assert.AreEqual ("", name, "#1-2");
  86. Assert.IsNull (msg.Headers.To, "#1-3"); // no overwrite
  87. Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#1-4");
  88. msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo");
  89. Assert.IsFalse (d.SelectOperation (ref msg, out name), "#2");
  90. Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#2-2");
  91. msg = Message.CreateMessage (MessageVersion.None, "http://foobar.org/MyService/NonExistent");
  92. Assert.IsFalse (d.SelectOperation (ref msg, out name), "#3");
  93. Assert.AreEqual ("", name, "#3-2");
  94. Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#3-3");
  95. // version and action do not matter
  96. msg = Message.CreateMessage (MessageVersion.Soap12, "http://nonexistent.org/");
  97. var http = new HttpRequestMessageProperty ();
  98. // this mismatch is allowed. Lack of this value is OK.
  99. // http.Method = "POST";
  100. // this mismatch is allowed. Lack of this value is OK.
  101. // http.QueryString = "foo=bar";
  102. // this mismatch is allowed. Lack of this value is OK.
  103. // http.Headers.Add ("Content-Type", "application/json");
  104. // so, the http property can be empty, but is required.
  105. msg.Properties.Add (HttpRequestMessageProperty.Name, http);
  106. msg.Headers.To = new Uri ("http://localhost:8080/Echo?input=hoge");
  107. Assert.IsTrue (d.SelectOperation (ref msg, out name), "#4");
  108. // FIXME: hmm... isn'y "Echo" expected?
  109. // Assert.AreEqual ("", name, "#4-2");
  110. }
  111. [Test]
  112. [Category ("NotWorking")]
  113. public void SelectOperationOnlyMessage ()
  114. {
  115. // This test shows strange result ... it adds UriMatched property while it does not really match the URI.
  116. var d = Create ();
  117. var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo"); // looks like Version is not checked
  118. Assert.AreEqual ("", d.SelectOperation (ref msg), "#1");
  119. Assert.IsTrue (msg.Properties.ContainsKey ("UriMatched"), "#1-2");
  120. msg = Message.CreateMessage (MessageVersion.None, "http://foobar.org/MyService/NonExistent");
  121. Assert.AreEqual ("", d.SelectOperation (ref msg), "#2");
  122. Assert.IsNotNull (msg.Properties ["UriMatched"], "#2-2");
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentException))]
  126. public void SelectOperationCheckExistingProperty ()
  127. {
  128. var d = Create ();
  129. var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo"); // heh, I mistyped it and turned to prove that Action does not matter.
  130. msg.Headers.To = new Uri ("http://localhost:8080/Echo");
  131. // LAMESPEC: .NET does returns String.Empty, while we return the name of the operation (as IOperationSelector.SelectOperation is expected to do!)
  132. // Assert.AreEqual (String.Empty, d.SelectOperation (ref msg), "#1");
  133. d.SelectOperation (ref msg);
  134. // The second invocation should raise the expected exception
  135. d.SelectOperation (ref msg);
  136. }
  137. // Types
  138. class MySelector : WebHttpDispatchOperationSelector
  139. {
  140. public MySelector (ServiceEndpoint se)
  141. : base (se)
  142. {
  143. }
  144. public bool SelectOperation (ref Message msg, out string name)
  145. {
  146. bool ret;
  147. name = SelectOperation (ref msg, out ret);
  148. return ret;
  149. }
  150. }
  151. class MyBehavior : WebHttpBehavior
  152. {
  153. public WebHttpDispatchOperationSelector GetPublicOperationSelector (ServiceEndpoint se)
  154. {
  155. return GetOperationSelector (se);
  156. }
  157. protected override WebHttpDispatchOperationSelector GetOperationSelector (ServiceEndpoint se)
  158. {
  159. return new MySelector (se);
  160. }
  161. }
  162. [ServiceContract]
  163. public interface MyService
  164. {
  165. [OperationContract]
  166. // [WebGet (UriTemplate = "MyService/Echo?input={input}")]
  167. // [WebGet] // UriTemplate = "Echo?input={input}"
  168. string Echo (string input);
  169. }
  170. [ServiceContract (Name = "MyService")]
  171. public interface MyService2
  172. {
  173. [OperationContract (Name = "Echo")]
  174. [WebGet] // UriTemplate = "Echo?input={input}"
  175. string Echo (string input);
  176. }
  177. MySelector Create ()
  178. {
  179. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
  180. se.Address = new EndpointAddress ("http://localhost:8080");
  181. se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
  182. return new MySelector (se);
  183. //var b = new MyBehavior ();
  184. //se.Behaviors.Add (b);
  185. //return (MySelector) b.GetPublicOperationSelector (se);
  186. }
  187. MySelector Create2 ()
  188. {
  189. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService2)));
  190. se.Address = new EndpointAddress ("http://localhost:8080");
  191. //se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
  192. return new MySelector (se);
  193. }
  194. #endregion
  195. #region "bug #656020"
  196. [DataContract]
  197. public class User
  198. {
  199. [DataMember]
  200. public string Name { get; set; }
  201. }
  202. [ServiceContract]
  203. interface IHello
  204. {
  205. [WebGet(UriTemplate = "{name}?age={age}&blah={blah}")]
  206. [OperationContract]
  207. string SayHi (string name, int age, string blah);
  208. [WebInvoke(UriTemplate = "blah")]
  209. [OperationContract]
  210. string SayHi2 (User user);
  211. }
  212. class Hello : IHello
  213. {
  214. public string SayHi (string name, int age, string blah)
  215. {
  216. return string.Format ("Hi {0}: {1}, {2}", name, age, blah == null);
  217. }
  218. public string SayHi2 (User user)
  219. {
  220. return string.Format ("Hi {0}.", user.Name);
  221. }
  222. }
  223. [Test]
  224. public void WebMessageFormats ()
  225. {
  226. var host = new WebServiceHost (typeof (Hello));
  227. var port = NetworkHelpers.FindFreePort ();
  228. host.AddServiceEndpoint (typeof (IHello), new WebHttpBinding (), "http://localhost:" + port + "/");
  229. host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
  230. host.Open ();
  231. try {
  232. // run client
  233. using (ChannelFactory<IHello> factory = new ChannelFactory<IHello> (new WebHttpBinding (), "http://localhost:" + port + "/"))
  234. {
  235. factory.Endpoint.Behaviors.Add (new WebHttpBehavior ());
  236. IHello h = factory.CreateChannel ();
  237. //Console.WriteLine(h.SayHi("Joe", 42, null));
  238. Assert.AreEqual ("Hi Joe.", h.SayHi2 (new User { Name = "Joe" }), "#1");
  239. }
  240. } finally {
  241. host.Close ();
  242. }
  243. }
  244. #endregion
  245. }
  246. }
  247. #endif