WebHttpDispatchOperationSelectorTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. [Test]
  58. public void SelectOperation ()
  59. {
  60. SelectOperationCore (Create ());
  61. }
  62. [Test]
  63. [Category("NotWorking")]
  64. public void SelectOperation2 ()
  65. {
  66. SelectOperationCore (Create2 ());
  67. }
  68. [Test]
  69. [Category("NotWorking")]
  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. [Category ("NotWorking")]
  125. public void SelectOperationCheckExistingProperty ()
  126. {
  127. var d = Create ();
  128. var msg = Message.CreateMessage (MessageVersion.None, "http://temuri.org/MyService/Echo");
  129. d.SelectOperation (ref msg);
  130. d.SelectOperation (ref msg);
  131. }
  132. // Types
  133. class MySelector : WebHttpDispatchOperationSelector
  134. {
  135. public MySelector (ServiceEndpoint se)
  136. : base (se)
  137. {
  138. }
  139. public bool SelectOperation (ref Message msg, out string name)
  140. {
  141. bool ret;
  142. name = SelectOperation (ref msg, out ret);
  143. return ret;
  144. }
  145. }
  146. class MyBehavior : WebHttpBehavior
  147. {
  148. public WebHttpDispatchOperationSelector GetPublicOperationSelector (ServiceEndpoint se)
  149. {
  150. return GetOperationSelector (se);
  151. }
  152. protected override WebHttpDispatchOperationSelector GetOperationSelector (ServiceEndpoint se)
  153. {
  154. return new MySelector (se);
  155. }
  156. }
  157. [ServiceContract]
  158. public interface MyService
  159. {
  160. [OperationContract]
  161. // [WebGet (UriTemplate = "MyService/Echo?input={input}")]
  162. // [WebGet] // UriTemplate = "Echo?input={input}"
  163. string Echo (string input);
  164. }
  165. [ServiceContract (Name = "MyService")]
  166. public interface MyService2
  167. {
  168. [OperationContract (Name = "Echo")]
  169. [WebGet] // UriTemplate = "Echo?input={input}"
  170. string Echo (string input);
  171. }
  172. MySelector Create ()
  173. {
  174. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService)));
  175. se.Address = new EndpointAddress ("http://localhost:8080");
  176. se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
  177. return new MySelector (se);
  178. //var b = new MyBehavior ();
  179. //se.Behaviors.Add (b);
  180. //return (MySelector) b.GetPublicOperationSelector (se);
  181. }
  182. MySelector Create2 ()
  183. {
  184. ServiceEndpoint se = new ServiceEndpoint (ContractDescription.GetContract (typeof (MyService2)));
  185. se.Address = new EndpointAddress ("http://localhost:8080");
  186. //se.Contract.Operations [0].Behaviors.Add (new WebGetAttribute ());
  187. return new MySelector (se);
  188. }
  189. }
  190. }