WebHttpBehaviorTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. [Category ("NotWorking")]
  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. }
  171. }