WebHttpBehaviorTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.ServiceModel;
  3. using System.ServiceModel.Channels;
  4. using System.ServiceModel.Description;
  5. using System.ServiceModel.Dispatcher;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. using NUnit.Framework;
  9. namespace MonoTests.System.ServiceModel.Description
  10. {
  11. [TestFixture]
  12. public class WebHttpBehaviorTest
  13. {
  14. ServiceEndpoint CreateEndpoint ()
  15. {
  16. return new ServiceEndpoint (ContractDescription.GetContract (typeof (IMyService)), new WebHttpBinding (),
  17. new EndpointAddress ("http://localhost:37564"));
  18. }
  19. [Test]
  20. [Category("NotWorking")]
  21. public void AddBiningParameters ()
  22. {
  23. var se = CreateEndpoint ();
  24. var b = new WebHttpBehavior ();
  25. var pl = new BindingParameterCollection ();
  26. b.AddBindingParameters (se, pl);
  27. Assert.AreEqual (0, pl.Count, "#1");
  28. }
  29. [Test]
  30. [Category ("NotWorking")]
  31. public void ApplyDispatchBehavior ()
  32. {
  33. var se = CreateEndpoint ();
  34. var od = se.Contract.Operations [0];
  35. // in .NET 3.5 it adds "OperationSelectorBehavior"
  36. int initCB = ContractDescription.GetContract (typeof (IMyService)).Behaviors.Count;
  37. // in .NET 3.5 it adds
  38. // - OperationInvokeBehavior,
  39. // - OperationBehaviorAttribute,
  40. // - DataContractSerializerOperationBehavior and
  41. // - DataContractSerializerOperationGenerator
  42. int initOB = od.Behaviors.Count;
  43. // Assert.AreEqual (1, initCB, "#0-1");
  44. // Assert.AreEqual (4, initOB, "#0-2");
  45. var b = new WebHttpBehavior ();
  46. se.Behaviors.Add (b);
  47. var ed = new EndpointDispatcher (se.Address, se.Contract.Name, se.Contract.Namespace);
  48. IChannelListener l = new WebHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  49. var cd = new ChannelDispatcher (l);
  50. cd.Endpoints.Add (ed); // without it this test results in NRE (it blindly adds IErrorHandler).
  51. Assert.AreEqual (0, cd.ErrorHandlers.Count, "#1-1");
  52. Assert.IsNull (ed.DispatchRuntime.OperationSelector, "#1-2");
  53. Assert.AreEqual (1, se.Behaviors.Count, "#1-3-1");
  54. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#1-3-2");
  55. Assert.AreEqual (initOB, od.Behaviors.Count, "#1-3-3");
  56. Assert.IsTrue (ed.AddressFilter is EndpointAddressMessageFilter, "#1-4");
  57. b.ApplyDispatchBehavior (se, ed);
  58. // FIXME: implement and enable it later
  59. //Assert.AreEqual (1, cd.ErrorHandlers.Count, "#2-1");
  60. Assert.AreEqual (typeof (WebHttpDispatchOperationSelector),
  61. ed.DispatchRuntime.OperationSelector.GetType (), "#2-2");
  62. Assert.AreEqual (1, se.Behaviors.Count, "#3-1");
  63. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#3-2");
  64. Assert.AreEqual (initOB, od.Behaviors.Count, "#3-3");
  65. // ... i.e. nothing is added.
  66. Assert.IsTrue (ed.AddressFilter is PrefixEndpointAddressMessageFilter, "#3-4");
  67. Assert.AreEqual (0, ed.DispatchRuntime.Operations.Count, "#4-0"); // hmm... really?
  68. }
  69. [ServiceContract]
  70. public interface IMyService
  71. {
  72. [OperationContract]
  73. [WebGet]
  74. string Echo (string input);
  75. }
  76. public class MyService: IMyService
  77. {
  78. [OperationBehavior]
  79. public string Echo (string input)
  80. {
  81. return input;
  82. }
  83. }
  84. [Test]
  85. public void TestWebGetExists()
  86. {
  87. ContractDescription cd = ContractDescription.GetContract (typeof(IMyService), typeof (MyService));
  88. OperationDescription od = cd.Operations[0];
  89. Assert.IsTrue (od.Behaviors.Contains (typeof (WebGetAttribute)), "Operation is recognized as WebGet");
  90. }
  91. }
  92. }