/mcs/class/System.ServiceModel.Web/Test/System.ServiceModel.Description/WebHttpBehaviorTest.cs

https://github.com/iainlane/mono · C# · 315 lines · 263 code · 37 blank · 15 comment · 2 complexity · b99bcc955cb55dc8c87dc7194bcca5c3 MD5 · raw file

  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 DefaultValues ()
  49. {
  50. var b = new WebHttpBehavior ();
  51. Assert.AreEqual (WebMessageBodyStyle.Bare, b.DefaultBodyStyle, "#1");
  52. Assert.AreEqual (WebMessageFormat.Xml, b.DefaultOutgoingRequestFormat, "#2");
  53. Assert.AreEqual (WebMessageFormat.Xml, b.DefaultOutgoingResponseFormat, "#3");
  54. #if NET_4_0
  55. Assert.IsFalse (b.AutomaticFormatSelectionEnabled, "#11");
  56. Assert.IsFalse (b.FaultExceptionEnabled, "#12");
  57. Assert.IsFalse (b.HelpEnabled, "#13");
  58. #endif
  59. }
  60. [Test]
  61. public void AddBiningParameters ()
  62. {
  63. var se = CreateEndpoint ();
  64. var b = new WebHttpBehavior ();
  65. var pl = new BindingParameterCollection ();
  66. b.AddBindingParameters (se, pl);
  67. Assert.AreEqual (0, pl.Count, "#1");
  68. }
  69. [Test]
  70. public void ApplyDispatchBehavior ()
  71. {
  72. var se = CreateEndpoint ();
  73. var od = se.Contract.Operations [0];
  74. // in .NET 3.5 it adds "OperationSelectorBehavior"
  75. int initCB = ContractDescription.GetContract (typeof (IMyService)).Behaviors.Count;
  76. // in .NET 3.5 it adds
  77. // - OperationInvokeBehavior,
  78. // - OperationBehaviorAttribute,
  79. // - DataContractSerializerOperationBehavior and
  80. // - DataContractSerializerOperationGenerator
  81. int initOB = od.Behaviors.Count;
  82. // Assert.AreEqual (1, initCB, "#0-1");
  83. // Assert.AreEqual (4, initOB, "#0-2");
  84. var b = new WebHttpBehavior ();
  85. se.Behaviors.Add (b);
  86. var ed = new EndpointDispatcher (se.Address, se.Contract.Name, se.Contract.Namespace);
  87. IChannelListener l = new WebHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  88. var cd = new ChannelDispatcher (l);
  89. cd.Endpoints.Add (ed); // without it this test results in NRE (it blindly adds IErrorHandler).
  90. Assert.AreEqual (0, cd.ErrorHandlers.Count, "#1-1");
  91. Assert.IsNull (ed.DispatchRuntime.OperationSelector, "#1-2");
  92. Assert.AreEqual (1, se.Behaviors.Count, "#1-3-1");
  93. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#1-3-2");
  94. Assert.AreEqual (initOB, od.Behaviors.Count, "#1-3-3");
  95. Assert.IsTrue (ed.AddressFilter is EndpointAddressMessageFilter, "#1-4");
  96. b.ApplyDispatchBehavior (se, ed);
  97. // FIXME: implement and enable it later
  98. //Assert.AreEqual (1, cd.ErrorHandlers.Count, "#2-1");
  99. Assert.AreEqual (typeof (WebHttpDispatchOperationSelector),
  100. ed.DispatchRuntime.OperationSelector.GetType (), "#2-2");
  101. Assert.AreEqual (1, se.Behaviors.Count, "#3-1");
  102. Assert.AreEqual (initCB, se.Contract.Behaviors.Count, "#3-2");
  103. Assert.AreEqual (initOB, od.Behaviors.Count, "#3-3");
  104. // ... i.e. nothing is added.
  105. Assert.IsTrue (ed.AddressFilter is PrefixEndpointAddressMessageFilter, "#3-4");
  106. Assert.AreEqual (0, ed.DispatchRuntime.Operations.Count, "#4-0"); // hmm... really?
  107. }
  108. [Test]
  109. public void GetMessageFormatters ()
  110. {
  111. var se = CreateEndpoint ();
  112. var od = se.Contract.Operations [0];
  113. var b = new WebHttpBehaviorExt ();
  114. Assert.IsNotNull (b.DoGetRequestClientFormatter (od, se), "#1");
  115. Assert.IsNotNull (b.DoGetReplyClientFormatter (od, se), "#2");
  116. Assert.IsNotNull (b.DoGetRequestDispatchFormatter (od, se), "#3");
  117. Assert.IsNotNull (b.DoGetReplyDispatchFormatter (od, se), "#4");
  118. }
  119. [Test]
  120. public void RequestClientFormatter ()
  121. {
  122. var se = CreateEndpoint ();
  123. var od = se.Contract.Operations [0];
  124. var b = new WebHttpBehaviorExt ();
  125. var rcf = b.DoGetRequestClientFormatter (od, se);
  126. var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
  127. var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  128. Assert.IsNotNull (hp, "#1");
  129. Assert.IsTrue (msg.IsEmpty, "#2");
  130. Assert.AreEqual (String.Empty, hp.QueryString, "#3");
  131. var mb = msg.CreateBufferedCopy (1000);
  132. try {
  133. rcf.DeserializeReply (mb.CreateMessage (), new object [0]);
  134. Assert.Fail ("It should not support reply deserialization");
  135. } catch (NotSupportedException) {
  136. }
  137. }
  138. [Test]
  139. public void RequestClientFormatter2 ()
  140. {
  141. var se = CreateEndpoint ();
  142. var od = se.Contract.Operations [0];
  143. var b = new WebHttpBehaviorExt ();
  144. IClientMessageFormatter rcf = null;
  145. b.ApplyClientBehaviorInvoked += delegate (ServiceEndpoint e, ClientRuntime cr) {
  146. rcf = cr.Operations [0].Formatter;
  147. };
  148. se.Behaviors.Add (b);
  149. var ch = new WebChannelFactory<IMyServiceClient> (se).CreateChannel ();
  150. var msg = rcf.SerializeRequest (MessageVersion.None, new object [] {"foo"});
  151. var hp = msg.Properties [HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  152. Assert.IsNotNull (hp, "#1");
  153. Assert.IsTrue (msg.IsEmpty, "#2");
  154. Assert.AreEqual (String.Empty, hp.QueryString, "#3");
  155. //var mb = msg.CreateBufferedCopy (1000);
  156. // TODO: test DeserializeReply too (it is supported unlike above).
  157. }
  158. [ServiceContract]
  159. public interface IMyService
  160. {
  161. [OperationContract]
  162. [WebGet]
  163. string Echo (string input);
  164. }
  165. public interface IMyServiceClient : IMyService, IClientChannel
  166. {
  167. }
  168. public class MyService: IMyService
  169. {
  170. [OperationBehavior]
  171. public string Echo (string input)
  172. {
  173. return input;
  174. }
  175. }
  176. [Test]
  177. public void TestWebGetExists()
  178. {
  179. ContractDescription cd = ContractDescription.GetContract (typeof(IMyService), typeof (MyService));
  180. OperationDescription od = cd.Operations[0];
  181. Assert.IsTrue (od.Behaviors.Contains (typeof (WebGetAttribute)), "Operation is recognized as WebGet");
  182. }
  183. [Test]
  184. public void MessageFormatterSupportsRaw ()
  185. {
  186. // serializing reply
  187. var ms = new MemoryStream ();
  188. var bytes = new byte [] {0, 1, 2, 0xFF};
  189. ms.Write (bytes, 0, bytes.Length);
  190. ms.Position = 0;
  191. var cd = ContractDescription.GetContract (typeof (ITestService));
  192. var od = cd.Operations [0];
  193. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:37564/"));
  194. var formatter = new WebHttpBehaviorExt ().DoGetReplyDispatchFormatter (od, se);
  195. var msg = formatter.SerializeReply (MessageVersion.None, null, ms);
  196. var wp = msg.Properties ["WebBodyFormatMessageProperty"] as WebBodyFormatMessageProperty;
  197. Assert.IsNotNull (wp, "#1");
  198. Assert.AreEqual (WebContentFormat.Raw, wp.Format, "#2");
  199. var wmebe = new WebMessageEncodingBindingElement ();
  200. var wme = wmebe.CreateMessageEncoderFactory ().Encoder;
  201. var ms2 = new MemoryStream ();
  202. wme.WriteMessage (msg, ms2);
  203. Assert.AreEqual (bytes, ms2.ToArray (), "#3");
  204. }
  205. [Test]
  206. public void MessageFormatterSupportsRaw2 ()
  207. {
  208. // deserializing request
  209. var ms = new MemoryStream ();
  210. ms.Write (new byte [] {0, 1, 2, 0xFF}, 0, 4);
  211. ms.Position = 0;
  212. var cd = ContractDescription.GetContract (typeof (ITestService));
  213. var od = cd.Operations [0];
  214. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:8080/"));
  215. var wmebe = new WebMessageEncodingBindingElement ();
  216. var wme = wmebe.CreateMessageEncoderFactory ().Encoder;
  217. var msg = wme.ReadMessage (ms, 100, null); // "application/xml" causes error.
  218. var formatter = new WebHttpBehaviorExt ().DoGetRequestDispatchFormatter (od, se);
  219. object [] pars = new object [1];
  220. formatter.DeserializeRequest (msg, pars);
  221. Assert.IsTrue (pars [0] is Stream, "ret");
  222. }
  223. [ServiceContract]
  224. public interface IMultipleParametersGet
  225. {
  226. [OperationContract]
  227. [WebGet (UriTemplate = "get")]
  228. void Get (string p1, string p2);
  229. }
  230. [ServiceContract]
  231. public interface IMultipleParameters
  232. {
  233. [OperationContract]
  234. [WebInvoke (UriTemplate = "posturi?p1={p1}&p2={p2}")]
  235. string PostUri (string p1, string p2);
  236. [OperationContract]
  237. [WebInvoke (UriTemplate = "postone?p1={p1}")]
  238. string PostOne (string p1, string p2);
  239. [OperationContract]
  240. [WebInvoke (UriTemplate = "postwrapped", BodyStyle=WebMessageBodyStyle.WrappedRequest)]
  241. string PostWrapped (string p1, string p2);
  242. [OperationContract]
  243. [WebInvoke (UriTemplate = "out?p1={p1}&p2={p2}", BodyStyle=WebMessageBodyStyle.WrappedResponse)]
  244. void PostOut (string p1, string p2, out string ret);
  245. }
  246. [Test]
  247. public void MultipleParameters ()
  248. {
  249. var behavior = new WebHttpBehaviorExt ();
  250. var cd = ContractDescription.GetContract (typeof (IMultipleParameters));
  251. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:8080/"));
  252. behavior.Validate (se);
  253. foreach (var od in cd.Operations)
  254. behavior.DoGetRequestClientFormatter (od, se);
  255. }
  256. [Test]
  257. [Category ("NotWorking")]
  258. public void MultipleParameters2 ()
  259. {
  260. var behavior = new WebHttpBehaviorExt ();
  261. var cd = ContractDescription.GetContract (typeof (IMultipleParametersGet));
  262. var se = new ServiceEndpoint (cd, new WebHttpBinding (), new EndpointAddress ("http://localhost:8080/"));
  263. behavior.Validate (se);
  264. try {
  265. foreach (var od in cd.Operations)
  266. behavior.DoGetRequestClientFormatter (od, se);
  267. Assert.Fail ("Should result in invalid operation");
  268. } catch (InvalidOperationException) {
  269. }
  270. }
  271. }
  272. [ServiceContract]
  273. public interface ITestService
  274. {
  275. [OperationContract]
  276. Stream Receive (Stream input);
  277. }
  278. }