WebHttpDispatchOperationSelectorTest.cs 8.8 KB

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