WebHttpBehaviorTest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.ServiceModel.Description;
  7. using System.ServiceModel.Dispatcher;
  8. using System.ServiceModel.Web;
  9. using System.Text;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.ServiceModel.Description
  12. {
  13. public class WebHttpBehaviorExt : WebHttpBehavior
  14. {
  15. public IClientMessageFormatter DoGetReplyClientFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  16. {
  17. return GetReplyClientFormatter (operationDescription, endpoint);
  18. }
  19. public IClientMessageFormatter DoGetRequestClientFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  20. {
  21. return GetRequestClientFormatter (operationDescription, endpoint);
  22. }
  23. public IDispatchMessageFormatter DoGetReplyDispatchFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  24. {
  25. return GetReplyDispatchFormatter (operationDescription, endpoint);
  26. }
  27. public IDispatchMessageFormatter DoGetRequestDispatchFormatter (OperationDescription operationDescription, ServiceEndpoint endpoint)
  28. {
  29. return GetRequestDispatchFormatter (operationDescription, endpoint);
  30. }
  31. public event Action<ServiceEndpoint, ClientRuntime> ApplyClientBehaviorInvoked;
  32. public override void ApplyClientBehavior (ServiceEndpoint endpoint, ClientRuntime client)
  33. {
  34. base.ApplyClientBehavior (endpoint, client);
  35. if (ApplyClientBehaviorInvoked != null)
  36. ApplyClientBehaviorInvoked (endpoint, client);
  37. }
  38. }
  39. [TestFixture]
  40. public class WebHttpBehaviorTest
  41. {
  42. ServiceEndpoint CreateEndpoint ()
  43. {
  44. return new ServiceEndpoint (ContractDescription.GetContract (typeof (IMyService)), new WebHttpBinding (),
  45. new EndpointAddress ("http://localhost:37564"));
  46. }
  47. [Test]
  48. public void AddBiningParameters ()
  49. {
  50. var se = CreateEndpoint ();
  51. var b = new WebHttpBehavior ();
  52. var pl = new BindingParameterCollection ();
  53. b.AddBindingParameters (se, pl);
  54. Assert.AreEqual (0, pl.Count, "#1");
  55. }
  56. [Test]
  57. public void ApplyDispatchBehavior ()
  58. {
  59. var se = CreateEndpoint ();
  60. var od = se.Contract.Operations [0];
  61. // in .NET 3.5 it adds "OperationSelectorBehavior"
  62. int initCB = ContractDescription.GetContract (typeof (IMyService)).Behaviors.Count;
  63. // in .NET 3.5 it adds
  64. // - OperationInvokeBehavior,
  65. // - OperationBehaviorAttribute,
  66. // - DataContractSerializerOperationBehavior and
  67. // - DataContractSerializerOperationGenerator
  68. int initOB = od.Behaviors.Count;
  69. // Assert.AreEqual (1, initCB, "#0-1");
  70. // Assert.AreEqual (4, initOB, "#0-2");
  71. var b = new WebHttpBehavior ();
  72. se.Behaviors.Add (b);
  73. var ed = new EndpointDispatcher (se.Address, se.Contract.Name, se.Contract.Namespace);
  74. IChannelListener l = new WebHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  75. var cd = new ChannelDispatcher (l);
  76. cd.Endpoints.Add (ed); // without it this test results in NRE (it blindly adds IErrorHandler).
  77. Assert.AreEqual (0, cd.ErrorHandlers.Count, "#1-1");
  78. Assert.IsNull (ed.DispatchRuntime.OperationSelector, "#1-2");
  79. Assert.AreEqual (1, se.Behaviors.Count, "#1-3-1");
  80. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#1-3-2");
  81. Assert.AreEqual (initOB, od.Behaviors.Count, "#1-3-3");
  82. Assert.IsTrue (ed.AddressFilter is EndpointAddressMessageFilter, "#1-4");
  83. b.ApplyDispatchBehavior (se, ed);
  84. // FIXME: implement and enable it later
  85. //Assert.AreEqual (1, cd.ErrorHandlers.Count, "#2-1");
  86. Assert.AreEqual (typeof (WebHttpDispatchOperationSelector),
  87. ed.DispatchRuntime.OperationSelector.GetType (), "#2-2");
  88. Assert.AreEqual (1, se.Behaviors.Count, "#3-1");
  89. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#3-2");
  90. Assert.AreEqual (initOB, od.Behaviors.Count, "#3-3");
  91. // ... i.e. nothing is added.
  92. Assert.IsTrue (ed.AddressFilter is PrefixEndpointAddressMessageFilter, "#3-4");
  93. Assert.AreEqual (0, ed.DispatchRuntime.Operations.Count, "#4-0"); // hmm... really?
  94. }
  95. [Test]
  96. public void GetMessageFormatters ()
  97. {
  98. var se = CreateEndpoint ();
  99. var od = se.Contract.Operations [0];
  100. var b = new WebHttpBehaviorExt ();
  101. Assert.IsNotNull (b.DoGetRequestClientFormatter (od, se), "#1");
  102. Assert.IsNotNull (b.DoGetReplyClientFormatter (od, se), "#2");
  103. Assert.IsNotNull (b.DoGetRequestDispatchFormatter (od, se), "#3");
  104. Assert.IsNotNull (b.DoGetReplyDispatchFormatter (od, se), "#4");
  105. }
  106. [Test]
  107. public void RequestClientFormatter ()
  108. {
  109. var se = CreateEndpoint ();
  110. var od = se.Contract.Operations [0];
  111. var b = new WebHttpBehaviorExt ();
  112. var rcf = b.DoGetRequestClientFormatter (od, se);
  113. var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
  114. var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  115. Assert.IsNotNull (hp, "#1");
  116. Assert.IsTrue (msg.IsEmpty, "#2");
  117. Assert.AreEqual (String.Empty, hp.QueryString, "#3");
  118. var mb = msg.CreateBufferedCopy (1000);
  119. try {
  120. rcf.DeserializeReply (mb.CreateMessage (), new object [0]);
  121. Assert.Fail ("It should not support reply deserialization");
  122. } catch (NotSupportedException) {
  123. }
  124. }
  125. [Test]
  126. public void RequestClientFormatter2 ()
  127. {
  128. var se = CreateEndpoint ();
  129. var od = se.Contract.Operations [0];
  130. var b = new WebHttpBehaviorExt ();
  131. IClientMessageFormatter rcf = null;
  132. b.ApplyClientBehaviorInvoked += delegate (ServiceEndpoint e, ClientRuntime cr) {
  133. rcf = cr.Operations [0].Formatter;
  134. };
  135. se.Behaviors.Add (b);
  136. var ch = new WebChannelFactory<IMyServiceClient> (se).CreateChannel ();
  137. var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
  138. var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  139. Assert.IsNotNull (hp, "#1");
  140. Assert.IsTrue (msg.IsEmpty, "#2");
  141. Assert.AreEqual (String.Empty, hp.QueryString, "#3");
  142. //var mb = msg.CreateBufferedCopy (1000);
  143. // TODO: test DeserializeReply too (it is supported unlike above).
  144. }
  145. [ServiceContract]
  146. public interface IMyService
  147. {
  148. [OperationContract]
  149. [WebGet]
  150. string Echo (string input);
  151. }
  152. public interface IMyServiceClient : IMyService, IClientChannel
  153. {
  154. }
  155. public class MyService: IMyService
  156. {
  157. [OperationBehavior]
  158. public string Echo (string input)
  159. {
  160. return input;
  161. }
  162. }
  163. [Test]
  164. public void TestWebGetExists()
  165. {
  166. ContractDescription cd = ContractDescription.GetContract (typeof(IMyService), typeof (MyService));
  167. OperationDescription od = cd.Operations[0];
  168. Assert.IsTrue (od.Behaviors.Contains (typeof (WebGetAttribute)), "Operation is recognized as WebGet");
  169. }
  170. [Test]
  171. public void MessageFormatterSupportsRaw ()
  172. {
  173. var ms = new MemoryStream ();
  174. var bytes = new byte [] {0, 1, 2, 0xFF};
  175. ms.Write (bytes, 0, bytes.Length);
  176. ms.Position = 0;
  177. var cd = ContractDescription.GetContract (typeof (ITestService));
  178. var od = cd.Operations [0];
  179. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:37564/"));
  180. var formatter = new WebHttpBehaviorExt ().DoGetReplyDispatchFormatter (od, se);
  181. var msg = formatter.SerializeReply (MessageVersion.None, null, ms);
  182. var wp = msg.Properties ["WebBodyFormatMessageProperty"] as WebBodyFormatMessageProperty;
  183. Assert.IsNotNull (wp, "#1");
  184. Assert.AreEqual (WebContentFormat.Raw, wp.Format, "#2");
  185. var wmebe = new WebMessageEncodingBindingElement ();
  186. var wme = wmebe.CreateMessageEncoderFactory ().Encoder;
  187. var ms2 = new MemoryStream ();
  188. wme.WriteMessage (msg, ms2);
  189. Assert.AreEqual (bytes, ms2.ToArray (), "#3");
  190. }
  191. }
  192. [ServiceContract]
  193. public interface ITestService
  194. {
  195. [OperationContract]
  196. Stream Receive (Stream input);
  197. }
  198. }