WebHttpDispatchOperationSelectorTest.cs 8.9 KB

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