WebHttpBehaviorTest.cs 6.6 KB

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