WebHttpDispatchOperationSelectorTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. namespace MonoTests.System.ServiceModel.Dispatcher
  42. {
  43. [TestFixture]
  44. public class WebHttpDispatchOperationSelectorTest
  45. {
  46. [Test]
  47. [ExpectedException (typeof (ArgumentNullException))]
  48. public void ConstructorNullEndpoint ()
  49. {
  50. new WebHttpDispatchOperationSelector (null);
  51. }
  52. [Test]
  53. [ExpectedException (typeof (InvalidOperationException))]
  54. public void ConstructorEndpointNullAddress ()
  55. {
  56. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
  57. new WebHttpDispatchOperationSelector (se);
  58. }
  59. #region SelectOperation
  60. [Test]
  61. public void SelectOperation ()
  62. {
  63. SelectOperationCore (Create ());
  64. }
  65. [Test]
  66. public void SelectOperation2 ()
  67. {
  68. SelectOperationCore (Create2 ());
  69. }
  70. [Test]
  71. public void SelectOperation3 ()
  72. {
  73. ContractDescription cd = ContractDescription.GetContract (typeof (MyService2));
  74. Assert.IsNotNull (cd.Operations [0].Behaviors.Find<WebGetAttribute> (), "#1");
  75. cd = ContractDescription.GetContract (typeof (MyService));
  76. Assert.IsNull (cd.Operations [0].Behaviors.Find<WebGetAttribute> (), "#2");
  77. }
  78. void SelectOperationCore (MySelector d)
  79. {
  80. string name;
  81. var msg = Message.CreateMessage (MessageVersion.Soap12, "http://temuri.org/MyService/Echo"); // looks like Version is not checked
  82. Assert.IsNull (msg.Headers.To, "#1-0");
  83. Assert.IsFalse (d.SelectOperation (ref msg, out name), "#1");
  84. Assert.AreEqual ("", name, "#1-2");
  85. Assert.IsNull (msg.Headers.To, "#1-3"); // no overwrite
  86. Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#1-4");
  87. msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo");
  88. Assert.IsFalse (d.SelectOperation (ref msg, out name), "#2");
  89. Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#2-2");
  90. msg = Message.CreateMessage (MessageVersion.None, "http://foobar.org/MyService/NonExistent");
  91. Assert.IsFalse (d.SelectOperation (ref msg, out name), "#3");
  92. Assert.AreEqual ("", name, "#3-2");
  93. Assert.IsFalse (msg.Properties.ContainsKey ("UriMatched"), "#3-3");
  94. // version and action do not matter
  95. msg = Message.CreateMessage (MessageVersion.Soap12, "http://nonexistent.org/");
  96. var http = new HttpRequestMessageProperty ();
  97. // this mismatch is allowed. Lack of this value is OK.
  98. // http.Method = "POST";
  99. // this mismatch is allowed. Lack of this value is OK.
  100. // http.QueryString = "foo=bar";
  101. // this mismatch is allowed. Lack of this value is OK.
  102. // http.Headers.Add ("Content-Type", "application/json");
  103. // so, the http property can be empty, but is required.
  104. msg.Properties.Add (HttpRequestMessageProperty.Name, http);
  105. msg.Headers.To = new Uri ("http://localhost:8080/Echo?input=hoge");
  106. Assert.IsTrue (d.SelectOperation (ref msg, out name), "#4");
  107. // FIXME: hmm... isn'y "Echo" expected?
  108. // Assert.AreEqual ("", name, "#4-2");
  109. }
  110. [Test]
  111. [Category ("NotWorking")]
  112. public void SelectOperationOnlyMessage ()
  113. {
  114. // This test shows strange result ... it adds UriMatched property while it does not really match the URI.
  115. var d = Create ();
  116. var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo"); // looks like Version is not checked
  117. Assert.AreEqual ("", d.SelectOperation (ref msg), "#1");
  118. Assert.IsTrue (msg.Properties.ContainsKey ("UriMatched"), "#1-2");
  119. msg = Message.CreateMessage (MessageVersion.None, "http://foobar.org/MyService/NonExistent");
  120. Assert.AreEqual ("", d.SelectOperation (ref msg), "#2");
  121. Assert.IsNotNull (msg.Properties ["UriMatched"], "#2-2");
  122. }
  123. [Test]
  124. [ExpectedException (typeof (ArgumentException))]
  125. public void SelectOperationCheckExistingProperty ()
  126. {
  127. var d = Create ();
  128. var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo"); // heh, I mistyped it and turned to prove that Action does not matter.
  129. msg.Headers.To = new Uri ("http://localhost:8080/Echo");
  130. // LAMESPEC: .NET does returns String.Empty, while we return the name of the operation (as IOperationSelector.SelectOperation is expected to do!)
  131. // Assert.AreEqual (String.Empty, d.SelectOperation (ref msg), "#1");
  132. d.SelectOperation (ref msg);
  133. // The second invocation should raise the expected exception
  134. d.SelectOperation (ref msg);
  135. }
  136. // Types
  137. class MySelector : WebHttpDispatchOperationSelector
  138. {
  139. public MySelector (ServiceEndpoint se)
  140. : base (se)
  141. {
  142. }
  143. public bool SelectOperation (ref Message msg, out string name)
  144. {
  145. bool ret;
  146. name = SelectOperation (ref msg, out ret);
  147. return ret;
  148. }
  149. }
  150. class MyBehavior : WebHttpBehavior
  151. {
  152. public WebHttpDispatchOperationSelector GetPublicOperationSelector (ServiceEndpoint se)
  153. {
  154. return GetOperationSelector (se);
  155. }
  156. protected override WebHttpDispatchOperationSelector GetOperationSelector (ServiceEndpoint se)
  157. {
  158. return new MySelector (se);
  159. }
  160. }
  161. [ServiceContract]
  162. public interface MyService
  163. {
  164. [OperationContract]
  165. // [WebGet (UriTemplate = "MyService/Echo?input={input}")]
  166. // [WebGet] // UriTemplate = "Echo?input={input}"
  167. string Echo (string input);
  168. }
  169. [ServiceContract (Name = "MyService")]
  170. public interface MyService2
  171. {
  172. [OperationContract (Name = "Echo")]
  173. [WebGet] // UriTemplate = "Echo?input={input}"
  174. string Echo (string input);
  175. }
  176. MySelector Create ()
  177. {
  178. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
  179. se.Address = new EndpointAddress ("http://localhost:8080");
  180. se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
  181. return new MySelector (se);
  182. //var b = new MyBehavior ();
  183. //se.Behaviors.Add (b);
  184. //return (MySelector) b.GetPublicOperationSelector (se);
  185. }
  186. MySelector Create2 ()
  187. {
  188. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService2)));
  189. se.Address = new EndpointAddress ("http://localhost:8080");
  190. //se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
  191. return new MySelector (se);
  192. }
  193. #endregion
  194. #region "bug #656020"
  195. [DataContract]
  196. public class User
  197. {
  198. [DataMember]
  199. public string Name { get; set; }
  200. }
  201. [ServiceContract]
  202. interface IHello
  203. {
  204. [WebGet(UriTemplate = "{name}?age={age}&blah={blah}")]
  205. [OperationContract]
  206. string SayHi (string name, int age, string blah);
  207. [WebInvoke(UriTemplate = "blah")]
  208. [OperationContract]
  209. string SayHi2 (User user);
  210. }
  211. class Hello : IHello
  212. {
  213. public string SayHi (string name, int age, string blah)
  214. {
  215. return string.Format ("Hi {0}: {1}, {2}", name, age, blah == null);
  216. }
  217. public string SayHi2 (User user)
  218. {
  219. return string.Format ("Hi {0}.", user.Name);
  220. }
  221. }
  222. [Test]
  223. public void WebMessageFormats ()
  224. {
  225. var host = new WebServiceHost (typeof (Hello));
  226. host.AddServiceEndpoint (typeof (IHello), new WebHttpBinding (), "http://localhost:37564/");
  227. host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
  228. host.Open ();
  229. try {
  230. // run client
  231. using (ChannelFactory<IHello> factory = new ChannelFactory<IHello> (new WebHttpBinding (), "http://localhost:37564/"))
  232. {
  233. factory.Endpoint.Behaviors.Add (new WebHttpBehavior ());
  234. IHello h = factory.CreateChannel ();
  235. //Console.WriteLine(h.SayHi("Joe", 42, null));
  236. Assert.AreEqual ("Hi Joe.", h.SayHi2 (new User { Name = "Joe" }), "#1");
  237. }
  238. } finally {
  239. host.Close ();
  240. }
  241. }
  242. #endregion
  243. }
  244. }
  245. #endif